void ext2DeinitVolume (ext2_vd *vd) { // Sanity check if (!vd) { errno = ENODEV; return; } // Lock ext2Lock(vd); // Close any directories which are still open (lazy programmers!) ext2_dir_state *nextDir = vd->firstOpenDir; while (nextDir) { ext2CloseDir(nextDir); nextDir = nextDir->nextOpenDir; } // Close any files which are still open (lazy programmers!) ext2_file_state *nextFile = vd->firstOpenFile; while (nextFile) { ext2CloseFile(nextFile); nextFile = nextFile->nextOpenFile; } // Reset open directory and file stats vd->openDirCount = 0; vd->openFileCount = 0; vd->firstOpenDir = NULL; vd->firstOpenFile = NULL; // Force the underlying device to sync ext2Sync(vd, NULL); // Unlock ext2Unlock(vd); // Deinitialise the volume lock //LWP_MutexDestroy(vd->lock); sys_lwmutex_destroy(&vd->lock); }
int ext2_dirclose_r (struct _reent *r, DIR_ITER *dirState) { ext2_log_trace("dirState %p\n", dirState); if(!dirState) { r->_errno = EINVAL; return -1; } ext2_dir_state* dir = STATE(dirState); // Sanity check if (!dir || !dir->vd) { r->_errno = EBADF; return -1; } // Lock ext2Lock(dir->vd); // Close the directory ext2CloseDir(dir); // Remove the directory from the double-linked FILO list of open directories dir->vd->openDirCount--; if (dir->nextOpenDir) dir->nextOpenDir->prevOpenDir = dir->prevOpenDir; if (dir->prevOpenDir) dir->prevOpenDir->nextOpenDir = dir->nextOpenDir; else dir->vd->firstOpenDir = dir->nextOpenDir; // Unlock ext2Unlock(dir->vd); return 0; }
DIR_ITER *ext2_diropen_r (struct _reent *r, DIR_ITER *dirState, const char *path) { ext2_log_trace("dirState %p, path %s\n", dirState, path); if(!dirState) { r->_errno = EINVAL; return NULL; } ext2_dir_state* dir = STATE(dirState); if(!dir) { r->_errno = EINVAL; return NULL; } // Get the volume descriptor for this path dir->vd = ext2GetVolume(path); if (!dir->vd) { r->_errno = ENODEV; return NULL; } // Lock ext2Lock(dir->vd); // Find the directory dir->ni = ext2OpenEntry(dir->vd, path); if (!dir->ni) { ext2Unlock(dir->vd); r->_errno = ENOENT; return NULL; } // Ensure that this directory is indeed a directory if (!LINUX_S_ISDIR(dir->ni->ni.i_mode)) { ext2CloseEntry(dir->vd, dir->ni); ext2Unlock(dir->vd); r->_errno = ENOTDIR; return NULL; } // Read the directory dir->first = dir->current = NULL; if (ext2fs_dir_iterate(dir->vd->fs, dir->ni->ino, 0, 0, DirIterateCallback, dirState) != EXT2_ET_OK) { ext2CloseDir(dir); ext2Unlock(dir->vd); r->_errno = errno; return NULL; } // Move to the first entry in the directory dir->current = dir->first; // Update directory times ext2UpdateTimes(dir->vd, dir->ni, EXT2_UPDATE_ATIME); // Insert the directory into the double-linked FILO list of open directories if (dir->vd->firstOpenDir) { dir->nextOpenDir = dir->vd->firstOpenDir; dir->vd->firstOpenDir->prevOpenDir = dir; } else { dir->nextOpenDir = NULL; } dir->prevOpenDir = NULL; dir->vd->cwd_ni = dir->ni; dir->vd->firstOpenDir = dir; dir->vd->openDirCount++; // Unlock ext2Unlock(dir->vd); return dirState; }