📄 ramdisk.c
字号:
return 0;}/*****************************************************************************//* *//* RAM_READ() - Behaves just like read(): reads *at most* COUNT bytes *//* *//*****************************************************************************/int RAM_read(int dev_fd, char *buf, unsigned count){ int avail; /*-----------------------------------------------------------------------*/ /* If the given file descriptor is invalid in some way, return failure. */ /*-----------------------------------------------------------------------*/ if (dev_fd < 0 || dev_fd > _NRAMFDS || ram_fds[dev_fd].file == NULL) return -1; /*-----------------------------------------------------------------------*/ /* If the file was opened write-only, don't allow reads. */ /*-----------------------------------------------------------------------*/ if ((ram_fds[dev_fd].flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_WRONLY) return -1; /*-----------------------------------------------------------------------*/ /* Check how many bytes are left in the file past the current position. */ /* If there are not enough to satisfy the request, truncate the */ /* request. */ /*-----------------------------------------------------------------------*/ avail = (ram_fds[dev_fd].file->size > ram_fds[dev_fd].pos ? ram_fds[dev_fd].file->size - ram_fds[dev_fd].pos : 0); if (count == 0) return 0; count = avail < count ? avail : count; if (count > 0) { /*-------------------------------------------------------------------*/ /* Perform the read. */ /*-------------------------------------------------------------------*/ memcpy(buf, ram_fds[dev_fd].file->data + ram_fds[dev_fd].pos, count); /*-------------------------------------------------------------------*/ /* Advance the file position pointer. */ /*-------------------------------------------------------------------*/ ram_fds[dev_fd].pos += count; } return count;}/*****************************************************************************//* *//* RAM_WRITE() - Behaves just like write() *//* *//*****************************************************************************/int RAM_write(int dev_fd, const char *buf, unsigned count){ /*-----------------------------------------------------------------------*/ /* If the given file descriptor is invalid in some way, return failure. */ /*-----------------------------------------------------------------------*/ if (dev_fd < 0 || dev_fd > _NRAMFDS || ram_fds[dev_fd].file == NULL) return -1; /*-----------------------------------------------------------------------*/ /* If the file was opened read-only, don't allow writes. */ /*-----------------------------------------------------------------------*/ if ((ram_fds[dev_fd].flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) return -1; /*-----------------------------------------------------------------------*/ /* Honor O_APPEND (seek to end of file on writes) */ /*-----------------------------------------------------------------------*/ if (ram_fds[dev_fd].flags & O_APPEND) ram_fds[dev_fd].pos = ram_fds[dev_fd].file->size; if (count == 0) return 0; /*-----------------------------------------------------------------------*/ /* If there is not enough space in the file after the current position, */ /* increase the size of the file so that the write request will fit. */ /*-----------------------------------------------------------------------*/ if (ram_fds[dev_fd].pos + count > ram_fds[dev_fd].file->size) if (ram_set_file_size(ram_fds[dev_fd].file, ram_fds[dev_fd].pos + count) < 0) return -1; /*-----------------------------------------------------------------------*/ /* Perform the write. */ /*-----------------------------------------------------------------------*/ memcpy(ram_fds[dev_fd].file->data + ram_fds[dev_fd].pos, buf, count); /*-----------------------------------------------------------------------*/ /* Advance the file position pointer. */ /*-----------------------------------------------------------------------*/ ram_fds[dev_fd].pos += count; return count;}/*****************************************************************************//* *//* RAM_LSEEK() - Behaves just like lseek() *//* *//*****************************************************************************/off_t RAM_lseek(int dev_fd, off_t offset, int origin){ off_t newpos; /*-----------------------------------------------------------------------*/ /* If the given file descriptor is invalid in some way, return failure. */ /*-----------------------------------------------------------------------*/ if (dev_fd < 0 || dev_fd > _NRAMFDS || ram_fds[dev_fd].file == NULL) return -1; switch (origin) { case SEEK_SET: { newpos = offset; break; } case SEEK_CUR: { newpos = ram_fds[dev_fd].pos + offset; break; } case SEEK_END: { newpos = ram_fds[dev_fd].file->size + offset; break; } default: return -1; } /*-----------------------------------------------------------------------*/ /* Don't allow a seek to before the beginning of the file. We do, */ /* however, support seeks to past the end of the file. Such seeks do */ /* not change the size of the file, but if it is followed by a write, */ /* we will increase the size of the file and pad with zeros. */ /*-----------------------------------------------------------------------*/ if (newpos < 0) return -1; /*-----------------------------------------------------------------------*/ /* The actual effect of a seek is surprisingly simple... */ /*-----------------------------------------------------------------------*/ return ram_fds[dev_fd].pos = newpos;}/*****************************************************************************//* *//* RAM_UNLINK() - Behaves just like unlink() *//* *//*****************************************************************************/int RAM_unlink(const char *path){ struct ram_file_info *file = ram_find_file(path, 0); /*-----------------------------------------------------------------------*/ /* If the file doesn't exist, return an error. */ /*-----------------------------------------------------------------------*/ if (!file) return -1; /*-----------------------------------------------------------------------*/ /* Mark the file as unlinked, which makes it invisible to OPEN and */ /* RENAME, but not actually removed from the disk. */ /*-----------------------------------------------------------------------*/ file->unlinked = 1; /*-----------------------------------------------------------------------*/ /* If there are no open file descriptors, actually delete the file. */ /*-----------------------------------------------------------------------*/ ram_maybe_unlink_file(file); return 0;}/*****************************************************************************//* *//* RAM_RENAME() - Behaves just like rename() *//* *//*****************************************************************************//* rename is unusual among the low-level functions in that it is also *//* specified in the ISO C standard. It has the same prototype and *//* semantics, so it's not really an issue. *//*****************************************************************************/int RAM_rename(const char *old_name, const char *new_name){ struct ram_file_info *old_file = ram_find_file(old_name, 0); struct ram_file_info *new_file = ram_find_file(new_name, 0); char *space; /*-----------------------------------------------------------------------*/ /* Check that the old file actually exists. */ /*-----------------------------------------------------------------------*/ if (!old_file) return -1; /*-----------------------------------------------------------------------*/ /* Shortcut: Don't do anything if the new name is the same as the old. */ /*-----------------------------------------------------------------------*/ if (old_file == new_file) return 0; /*-----------------------------------------------------------------------*/ /* Allocate space for the new name. This is a bit funny, in that we */ /* could fail to rename a file simply because we ran out of memory for */ /* its name, but handling this odd case would complicate this function. */ /*-----------------------------------------------------------------------*/ space = malloc(strlen(new_name) + 1); if (!space) return -1; /*-----------------------------------------------------------------------*/ /* If a file with the new name already exists, unlink it. */ /*-----------------------------------------------------------------------*/ if (new_file) { new_file->unlinked = 1; ram_maybe_unlink_file(new_file); } /*-----------------------------------------------------------------------*/ /* Discard the old name and take the new name. */ /*-----------------------------------------------------------------------*/ free(old_file->name); old_file->name = strcpy(space, new_name); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -