void ext2CloseFile (ext2_file_state *file) { // Sanity check if (!file || !file->vd) return; ext2fs_file_close(file->fd); // Sync the file (and its attributes) to disc if(file->write) ext2UpdateTimes(file->vd, file->ni, EXT2_UPDATE_ACTIME); if (file->read) ext2UpdateTimes(file->vd, file->ni, EXT2_UPDATE_ATIME); ext2Sync(file->vd, file->ni); // Close the file (if open) if (file->ni) ext2CloseEntry(file->vd, file->ni); // Reset the file state file->ni = NULL; file->fd = NULL; file->flags = 0; file->read = false; file->write = false; file->append = false; return; }
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_ftruncate_r (struct _reent *r, int fd, off_t len) { ext2_log_trace("fd %p, len %Li\n", (void *) fd, len); ext2_file_state* file = STATE(fd); errcode_t err = 0; // Sanity check if (!file || !file->vd || !file->ni || !file->fd) { r->_errno = EINVAL; return -1; } // Lock ext2Lock(file->vd); // Check that we are allowed to write to this file if (!file->write) { ext2Unlock(file->vd); r->_errno = EACCES; return -1; } err = ext2fs_file_set_size2(file->fd, len); // Sync the file (and its attributes) to disc if(!err) ext2Sync(file->vd, file->ni); // update times ext2UpdateTimes(file->vd, file->ni, EXT2_UPDATE_AMTIME); // Unlock ext2Unlock(file->vd); return err; }
int ext2_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode) { //ext2_log_trace("fileStruct %p, path %s, flags %i, mode %i\n", fileStruct, path, flags, mode); ext2_file_state* file = STATE(fileStruct); // Get the volume descriptor for this path file->vd = ext2GetVolume(path); if (!file->vd) { r->_errno = ENODEV; return -1; } // Lock ext2Lock(file->vd); // Determine which mode the file is opened for file->flags = flags; if ((flags & 0x03) == O_RDONLY) { file->read = true; file->write = false; file->append = false; } else if ((flags & 0x03) == O_WRONLY) { file->read = false; file->write = true; file->append = (flags & O_APPEND); } else if ((flags & 0x03) == O_RDWR) { file->read = true; file->write = true; file->append = (flags & O_APPEND); } else { r->_errno = EACCES; ext2Unlock(file->vd); return -1; } // Try and find the file and (if found) ensure that it is not a directory file->ni = ext2OpenEntry(file->vd, path); if (file->ni && LINUX_S_ISDIR(file->ni->ni.i_mode)) { ext2CloseEntry(file->vd, file->ni); ext2Unlock(file->vd); r->_errno = EISDIR; return -1; } // Are we creating this file? if ((flags & O_CREAT) && !file->ni) // Create the file file->ni = ext2Create(file->vd, path, S_IFREG, NULL); // exit(0); // Sanity check, the file should be open by now if (!file->ni) { ext2Unlock(file->vd); r->_errno = ENOENT; return -1; } // Make sure we aren't trying to write to a read-only file if (!(file->vd->fs->flags & EXT2_FLAG_RW) && file->write) { ext2CloseEntry(file->vd, file->ni); ext2Unlock(file->vd); r->_errno = EROFS; return -1; } errcode_t err = ext2fs_file_open2(file->vd->fs, file->ni->ino, &file->ni->ni, file->write ? EXT2_FLAG_RW : 0, &file->fd); if(err != 0) { ext2CloseEntry(file->vd, file->ni); ext2Unlock(file->vd); r->_errno = ENOENT; return -1; } // Truncate the file if requested if ((flags & O_TRUNC) && file->write) { if (ext2fs_file_set_size2(file->fd, 0) != 0) { ext2CloseEntry(file->vd, file->ni); ext2Unlock(file->vd); r->_errno = errno; return -1; } file->ni->ni.i_size = file->ni->ni.i_size_high = 0; } // Set the files current position ext2fs_file_llseek(file->fd, file->append ? EXT2_I_SIZE(&file->ni->ni) : 0, SEEK_SET, 0); //ext2_log_trace("file->len %lld\n", EXT2_I_SIZE(&file->ni->ni)); // Update file times ext2UpdateTimes(file->vd, file->ni, EXT2_UPDATE_ATIME); // Insert the file into the double-linked FILO list of open files if (file->vd->firstOpenFile) { file->nextOpenFile = file->vd->firstOpenFile; file->vd->firstOpenFile->prevOpenFile = file; } else { file->nextOpenFile = NULL; } file->prevOpenFile = NULL; file->vd->firstOpenFile = file; file->vd->openFileCount++; // Sync access time ext2Sync(file->vd, file->ni); file->is_ntfs = 0; // Unlock ext2Unlock(file->vd); return (int)(s64)fileStruct; }
int ext2_file_to_sectors (struct _reent *r,const char *path,uint32_t *sec_out,uint32_t *size_out,int max,int phys) { ntfs_file_state fileStruct; ext2_file_state* file = STATE(&fileStruct); // Get the volume descriptor for this path file->vd = ext2GetVolume(path); if (!file->vd) { r->_errno = ENODEV; return -1; } // Lock ext2Lock(file->vd); file->flags = 0; file->read = true; file->write = false; file->append = false; // Try and find the file and (if found) ensure that it is not a directory file->ni = ext2OpenEntry(file->vd, path); if (file->ni && LINUX_S_ISDIR(file->ni->ni.i_mode)) { ext2CloseEntry(file->vd, file->ni); ext2Unlock(file->vd); r->_errno = EISDIR; return -1; } // Sanity check, the file should be open by now if (!file->ni) { ext2Unlock(file->vd); r->_errno = ENOENT; return -1; } errcode_t err = ext2fs_file_open2(file->vd->fs, file->ni->ino, &file->ni->ni, 0, &file->fd); if(err != 0) { ext2CloseEntry(file->vd, file->ni); ext2Unlock(file->vd); r->_errno = ENOENT; return -1; } //ext2_log_trace("file->len %lld\n", EXT2_I_SIZE(&file->ni->ni)); // Update file times ext2UpdateTimes(file->vd, file->ni, EXT2_UPDATE_ATIME); // Insert the file into the double-linked FILO list of open files if (file->vd->firstOpenFile) { file->nextOpenFile = file->vd->firstOpenFile; file->vd->firstOpenFile->prevOpenFile = file; } else { file->nextOpenFile = NULL; } file->prevOpenFile = NULL; file->vd->firstOpenFile = file; file->vd->openFileCount++; // Sync access time ext2Sync(file->vd, file->ni); file->is_ntfs = 0; u64 read = 0; err = 0; // Read from the files data attribute //err = ext2fs_file_read(file->fd, ptr, len, &read); u64 len = 0; ext2fs_file_llseek(file->fd, 0, SEEK_END, (__u64 *) &len); // Set the files current position ext2fs_file_llseek(file->fd, 0, SEEK_SET, 0); u32 current_block = 0; err = ext2fs_file_read_sectors(file->fd, len, &read, sec_out, size_out, ¤t_block, max); if (err || read <= 0 || read > len) { ext2Unlock(file->vd); r->_errno = errno; current_block = err ? err : -1; goto end; } end: // Close the file ext2CloseFile(file); // Remove the file from the double-linked FILO list of open files file->vd->openFileCount--; if (file->nextOpenFile) file->nextOpenFile->prevOpenFile = file->prevOpenFile; if (file->prevOpenFile) file->prevOpenFile->nextOpenFile = file->nextOpenFile; else file->vd->firstOpenFile = file->nextOpenFile; ext2Unlock(file->vd); return current_block; }
int ext2Link(ext2_vd *vd, const char *old_path, const char *new_path) { ext2_inode_t *dir_ni = NULL, *ni = NULL; char *dir = NULL; char *name = NULL; errcode_t err = 0; // Sanity check if (!vd || !vd->fs) { errno = ENODEV; return -1; } if(!(vd->fs->flags & EXT2_FLAG_RW)) { errno = EACCES; return -1; } // You cannot link between devices if(vd != ext2GetVolume(new_path)) { errno = EXDEV; return -1; } // Get the actual paths of the entry old_path = ext2RealPath(old_path); new_path = ext2RealPath(new_path); if (!old_path || !new_path) return -1; // Lock ext2Lock(vd); //check for existing in new path ni = ext2OpenEntry(vd, new_path); if (ni) { errno = EEXIST; goto cleanup; } dir = strdup(new_path); if (!dir) { errno = ENOMEM; err = -1; goto cleanup; } char * ptr = strrchr(dir, '/'); if (ptr) { name = strdup(ptr+1); *ptr = 0; } else name = strdup(dir); // Find the entry ni = ext2OpenEntry(vd, old_path); if (!ni) { err = -1; goto cleanup; } // Open the entries new parent directory dir_ni = ext2OpenEntry(vd, dir); if (!dir_ni) { err = -1; goto cleanup; } do { // Link the entry to its new parent err = ext2fs_link(vd->fs, dir_ni->ino, name, ni->ino, ext2_file_type(ni->ni.i_mode)); if (err == EXT2_ET_DIR_NO_SPACE) { if (ext2fs_expand_dir(vd->fs, dir_ni->ino) != 0) goto cleanup; } else if(err != EXT2_ET_OK) { errno = EMLINK; goto cleanup; } } while(err == EXT2_ET_DIR_NO_SPACE); ni->ni.i_links_count++; // Update entry times ext2UpdateTimes(vd, ni, EXT2_UPDATE_MCTIME); // Sync the entry to disc ext2Sync(vd, ni); cleanup: if(dir_ni) ext2CloseEntry(vd, dir_ni); if(ni) ext2CloseEntry(vd, ni); if(dir) mem_free(dir); if(name) mem_free(name); // Unlock ext2Unlock(vd); return err; }
ext2_inode_t *ext2Create(ext2_vd *vd, const char *path, mode_t type, const char *target) { ext2_inode_t *dir_ni = NULL, *ni = NULL; char *dir = NULL; char *targetdir = NULL; char *name = NULL; ext2_ino_t newentry = 0; // Sanity check if (!vd || !vd->fs) { errno = ENODEV; return NULL; } if(!(vd->fs->flags & EXT2_FLAG_RW)) { errno = EACCES; return NULL; } // You cannot link between devices if(target) { if(vd != ext2GetVolume(target)) { errno = EXDEV; return NULL; } // Check if existing dir_ni = ext2OpenEntry(vd, target); if (dir_ni) { errno = EEXIST; goto cleanup; } targetdir = strdup(target); if (!targetdir) { errno = ENOMEM; goto cleanup; } target = ext2RealPath(target); } // Get the actual paths of the entry path = ext2RealPath(path); if (!path) goto cleanup; // Lock ext2Lock(vd); // Clean me // NOTE: this looks horrible right now and need a cleanup dir = strdup(path); if (!dir) { errno = ENOMEM; goto cleanup; } char * tmp_path = (targetdir && (type == S_IFLNK)) ? targetdir : dir; if (strrchr(tmp_path, '/') != NULL) { char * ptr = strrchr(tmp_path, '/'); name = strdup(ptr+1); *ptr = '\0'; } else name = strdup(tmp_path); // Open the entries parent directory dir_ni = ext2OpenEntry(vd, dir); if (!dir_ni) goto cleanup; // If not yet read, read the inode and block bitmap if((!vd->fs->inode_map || !vd->fs->block_map)) ext2fs_read_bitmaps(vd->fs); // Symbolic link if(type == S_IFLNK) { if (!target) { errno = EINVAL; goto cleanup; } newentry = ext2CreateSymlink(vd, path, targetdir, name, type); } // Directory else if(type == S_IFDIR) { newentry = ext2CreateMkDir(vd, dir_ni, LINUX_S_IFDIR | (0755 & ~vd->fs->umask), name); } // File else if(type == S_IFREG) { newentry = ext2CreateFile(vd, dir_ni, LINUX_S_IFREG | (0755 & ~vd->fs->umask), name); } // If the entry was created if (newentry != 0) { // Sync the entry to disc ext2Sync(vd, NULL); ni = ext2OpenEntry(vd, target ? target : path); } cleanup: if(dir_ni) ext2CloseEntry(vd, dir_ni); if(name) mem_free(name); if(dir) mem_free(dir); if(targetdir) mem_free(targetdir); // Unlock ext2Unlock(vd); return ni; }