Welcome! Log In Create A New Profile

Advanced

smb_devoptab with write support

Posted by DrTwox 
smb_devoptab with write support
February 15, 2009 03:23AM
Thought I'd share this, hoping someone would find it useful, and perhaps improve on it!
If you use TinySMB with smb_devoptab developed by rodries and scip, put the following code into smb_devoptab.c to enable write support.
/* Replace the existing function with this one */
int __smb_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
    SMBFILESTRUCT *file = (SMBFILESTRUCT*)fileStruct;

    /* check connection ? can't get here? */
    if (SMBCONNECTED == false) {
	r->_errno = ENODEV;
	return -1;
    }

    /* Strip of the device prefix */
    char fixedpath[ MAXPATH ];
    if (absolute_path_no_device(path, fixedpath) == NULL) {
	r->_errno = EINVAL;
	return -1;
    }

    SMBDIRENTRY dentry;

    if ((flags & 0x03) == O_RDONLY) {
	if (SMB_PathInfo(fixedpath, &dentry, smbconn) == SMB_SUCCESS) {
	    if (!(dentry.attributes & SMB_SRCH_DIRECTORY)) {
                file->handle = SMB_OpenFile(fixedpath, SMB_OPEN_READING, SMB_OF_OPEN, smbconn);
	    } else {
		/* Path points to a directory, not a file */
		r->_errno = EISDIR;
		return -1;
	    }
	} else {
	    /* File doesn't exist */
	    r->_errno = ENOENT;
	    return -1;
	}
    } else if ((flags & 0x03) == O_WRONLY) {
	SMB_PathInfo(fixedpath, &dentry, smbconn);
	if (!(dentry.attributes & SMB_SRCH_DIRECTORY)) {
	    file->handle = SMB_OpenFile(fixedpath, SMB_OPEN_WRITING, SMB_OF_CREATE | SMB_OF_TRUNCATE, smbconn);
	} else {
	    /* Path points to directory, not a file */
	    r->_errno = EISDIR;
	    return -1;
	}
    } else {
	r->_errno = ENOENT;
	return -1;
    }

    if (!file->handle) {
        r->_errno = ENOENT;
	return -1;
    }

    file->offset = 0;
    file->len = dentry.size_low;

    return 0;
}

/* Add this function under __smb_read */
int __smb_write(struct _reent *r, int fd, char *ptr, int len) {
    SMBFILESTRUCT *file = (SMBFILESTRUCT*)fd;

    if (file == NULL) {
        r->_errno = EBADF;
        return -1;
    }

    int bytesWritten = SMB_WriteFile(ptr, len, file->offset, file->handle);
    file->offset += bytesWritten;   
    if (bytesWritten != len) {
	r->_errno = EIO;
    }

    return byesWritten;
}

/* Modify this structure to include the new write function */
const devoptab_t dotab_smb = {
    "smb",
    sizeof(SMBFILESTRUCT),
    __smb_open,
    __smb_close,
    __smb_write, /* New function */
    __smb_read,
    __smb_seek,
    __smb_fstat,
    __smb_stat,
    NULL,
    NULL,
    __smb_chdir,
    NULL,
    NULL,
    sizeof(SMBDIRSTATESTRUCT),
    __smb_diropen,
    __smb_dirreset,
    __smb_dirnext,
    __smb_dirclose,
    NULL
};

There's a problem with the open code though; Sometimes when opening a file for writing, if that file doesn't already exist, smb_open will fail with a EISDIR error. It doesn't happen with all file creations, which is confusing! Any suggestions or ideas?
Re: smb_devoptab with write support
February 15, 2009 06:47PM
Umm...did you check on DevkitPro SVN? rodries has finished the devoptab, and he and I submitted a new TinySMB, which is now there. It has write support, amongst other things.



Edited 1 time(s). Last edit at 02/15/2009 06:48PM by Tantric.
Re: smb_devoptab with write support
February 15, 2009 10:27PM
D'oh... no, I didn't check the SVN repo! Feel free to nuke this post....
Sorry, only registered users may post in this forum.

Click here to login