//params: user_ip -> filepath -> mode return_type sOpen(const int nparams, arg_type* a){ // if (nparams != 4) { // r.return_val = NULL; // r.return_size = 0; // return r; // } char *user_ip = (char *)a->arg_val; char *alias = (char *)a->next->arg_val; char *filepath = (char *)a->next->next->arg_val; char *fullpath = prependRootName(filepath); int mode = *(int *)a->next->next->next->arg_val; if(debug)printf("fullpath on server: %s\n", fullpath); if (checkFileInUse(fullpath) == 1){ if(debug)printf("file in use"); return createReturn(26, -1); } // Check if user is mounted MountedUser *mounted = findMount(user_ip, alias); if (mounted == NULL){ return createReturn(1, -1); } // If the file does not exist or is a folder return error struct stat buffer; int err = stat(fullpath, &buffer); if(debug)printf("errval: %d\n", err); if (err == -1 && mode == 0){ if(debug)printf("cannot open in read mode\n"); return createReturn(err, 0); } if (S_ISDIR(buffer.st_mode)){ if(debug)printf("wtf its a folder\n"); return createReturn(21, 0); } int fd; if (mode == 1) { fd = open(fullpath, O_CREAT | O_RDWR | O_APPEND | O_TRUNC, S_IRUSR | S_IWUSR); }else { fd = open(fullpath, O_RDONLY | S_IRUSR); } if(debug)printf("the fd is %d\n", fd); // Can just add struct because we know it doesn't exist from checkFileInUse if (addOpenFile(user_ip, alias, fd, fullpath) != 0){ if(debug)printf("closing the file descriptor %d because of error\n", fd); close(fd); return createReturn(1, -1); } return createReturn(0, fd); }
void CheckedFile::open( int access, int permission ) //-------------------------------------------------- { CheckedFile * closeIt; // file to be closed when out of handles if( !_isOpen ) { _openAccess = access; _openPermission = permission; while( _openFiles->size() >= MaxOpenReadFiles ) { closeIt = _openFiles->back(); closeIt->privClose(); removeOpenFile( closeIt ); } _handle = ::open( _fileName, _openAccess, _openPermission ); while( _handle == -1 && errno == EMFILE ) { if( _openFiles->size() == 0 ) { FileExcept oops( FileExcept::Open, errno, _fileName ); throw( oops ); } closeIt = _openFiles->back(); closeIt->privClose(); removeOpenFile( closeIt ); _handle = ::open( _fileName, _openAccess, _openPermission ); } if( _handle == -1 ) { // error other than out of handles FileExcept oops( FileExcept::Open, errno, _fileName ); throw( oops ); } _isOpen = TRUE; _logOpen = TRUE; _currOffset = 0; if( !( _openAccess & O_WRONLY ) ) { addOpenFile( this ); } } }
int openDir(int fd, char *dname){ struct INode parent_in; struct InCoreINode* icn; struct DirEntry d; readINode(fd, currDirINode, &parent_in); // Tokenize the name and find its inode no int linkNo = fileExists(fd, dname, parent_in, &d); if( linkNo==-1 ){ printf("No such directory exists!\n"); return -1; } int dirhandle = isOpen(d.d_entry.d_inode, icn); if( dirhandle!=-1 ) return dirhandle; // Add the inode to open file object DLL and return the reference number return addOpenFile(fd, d.d_entry.d_inode); }