int closeDir(int dirhandle){ // Find the corresponding open file object and remove it int i; struct InCoreINode icn; struct DirEntry d; struct INode in; int inodeNo = isOpen(dirhandle, &icn); if( inodeNo==-1 ){ printf("Directory not open!\n"); return -1; } if( icn.ic_inode.i_nlinks==0 ){ // readDirEntry(icn.ic_inode, ) printf("Not a directory!%d\n", icn.ic_ino); return -1; } for(i=1; i<icn.ic_inode.i_nlinks; i++){ readDirEntry(icn.ic_dev, &icn.ic_inode, i, &d); readINode(icn.ic_dev, d.d_entry.d_inode, &in); if( in.i_nlinks==0 ) closeFile(d.d_entry.d_inode); else closeDir(d.d_entry.d_inode); } removeOpenFile(dirhandle); }
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 ); } } }
//nparams: user_ip -> fd return_type sClose(int nparams, arg_type* a) { // if (nparams != 3) { // r.return_val = NULL; // r.return_size = 0; // return r; // } char *user_ip = (char *)a->arg_val; char *alias = (char *)a->next->arg_val; int fd = *(int *)a->next->next->arg_val; // Check if user is mounted MountedUser *mounted = findMount(user_ip, alias); if (mounted == NULL){ return createReturn(1, -1); } if (removeOpenFile(user_ip, alias, fd) != 0){ return createReturn(1, -1); } return createReturn(0, 0); }
void CheckedFile::privClose() //--------------------------- // close the physical file, logical file stays open // since privClose is called from a destructor, avoid // doing a throw in it. { #ifndef NDEBUG int retVal; #endif if( _isOpen ) { #ifndef NDEBUG retVal = ::close( _handle ); assert( retVal == 0 ); #else ::close( _handle ); #endif if( _openAccess & O_RDONLY ) { removeOpenFile( this ); } } _isOpen = FALSE; }