From 2944f5cc43b975011dcb686855f5b518b7ddf77e Mon Sep 17 00:00:00 2001 From: "Enrico Fraccaroli (Galfurian)" Date: Thu, 27 Jan 2022 11:29:12 -0500 Subject: [PATCH] Clean un legacy files. --- initscp/CMakeLists.txt | 30 -- initscp/README.md | 20 -- initscp/src/initfscp.c | 404 ----------------------- libc/src/unistd/exec.c | 2 +- mentos/CMakeLists.txt | 1 - mentos/inc/fs/initrd.h | 14 - mentos/src/fs/initrd.c | 689 --------------------------------------- mentos/src/fs/procfs.c | 2 +- mentos/src/fs/stat.c | 1 - mentos/src/kernel.c | 17 - mentos/src/klib/libgen.c | 1 - scripts/replace_links.sh | 3 - 12 files changed, 2 insertions(+), 1182 deletions(-) delete mode 100644 initscp/CMakeLists.txt delete mode 100644 initscp/README.md delete mode 100755 initscp/src/initfscp.c delete mode 100644 mentos/inc/fs/initrd.h delete mode 100644 mentos/src/fs/initrd.c delete mode 100644 scripts/replace_links.sh diff --git a/initscp/CMakeLists.txt b/initscp/CMakeLists.txt deleted file mode 100644 index 3dfcec9..0000000 --- a/initscp/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -# ------------------------------------------------------------------------------ -# Initialize the project. -project(initfscp C) - -# ------------------------------------------------------------------------------ -# Set the project name. -set(PROJECT_NAME initfscp) - -if (CMAKE_CROSSCOMPILING) - set(CMAKE_C_COMPILER gcc) - set(CMAKE_CXX_COMPILER g++) - set(CMAKE_EXE_LINKER_FLAGS "") -endif (CMAKE_CROSSCOMPILING) - -# ------------------------------------------------------------------------------ -# Set the compiler options (original). -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") - -# ------------------------------------------------------------------------------ -# Add the source files. -set(SOURCE_FILES src/initfscp.c) - -# ------------------------------------------------------------------------------ -# Add the executable. -add_executable(initfscp ${SOURCE_FILES}) - - diff --git a/initscp/README.md b/initscp/README.md deleted file mode 100644 index 6cf5568..0000000 --- a/initscp/README.md +++ /dev/null @@ -1,20 +0,0 @@ ----- -README -initfscp - Autore di questa guida: Aleksej ----- - -initfscp e' un programma che serve a creare un'immagine contenente il file system utilizzato da MentOS. -Consente di inserirci dei files e/o dei mountpoint (massimo 32) all'interno. -Per far ciò digitare il comando "initfscp [-m mountpoint1 -m mountpoint2 ... -m mountpoint n] file1 file2 ... nomefs" - -dove: -- -m mountpoint1 -m mountpoint2 ... -m mountpoint n (facoltativo) indicano i mountpoint da inserire nel filesystem appena creato. -- "file1 file2 ..." sono i files che inseriremo nel filesystem, -- "nomefs" è il nome che daremo all'immagine. -Ad esempio vogliamo inserire i file topolino, pippo e paperino nell'immagine che chiameremo disney, quindi digitiamo: - initfscp topolino pippo paperino disney -così facendo initfscp genererà l'immagine disney. - -Altri comandi disponibili: ---help (-h) per visualizzare una guida più sintetica. ---version (-v) per visualizzare la versione utilizzata attualmente di initfscp. diff --git a/initscp/src/initfscp.c b/initscp/src/initfscp.c deleted file mode 100755 index 086e129..0000000 --- a/initscp/src/initfscp.c +++ /dev/null @@ -1,404 +0,0 @@ -/// @file initfscp.c -/// @brief - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define INITFSCP_VER "0.3.0" - -#define INITRD_NAME_MAX 255 -#define INITRD_MAX_FILES 128 -#define INITRD_MAX_FS_SIZE 1048576 - -#define RESET "\033[00m" -#define BLACK "\033[30m" -#define RED "\033[31m" -#define GREEN "\033[32m" -#define YELLOW "\033[33m" -#define BLUE "\033[34m" -#define MAGENTA "\033[35m" -#define CYAN "\033[36m" -#define WHITE "\033[37m" - -static const char dt_char_array[] = { - '?', // DT_UNKNOWN = 0, - 'p', //DT_FIFO = 1, - 'c', //DT_CHR = 2, - '*', - 'd', //DT_DIR = 4, - '*', - 'b', //DT_BLK = 6, - '*', - '-', //DT_REG = 8, - '*', - 'l', //DT_LNK = 10, - '*', - 's', //DT_SOCK = 12, - '*', - '?', //DT_WHT = 14 -}; - -/// @brief Information concerning a file. -typedef struct initrd_file_t { - /// Number used as delimiter, it must be set to 0xBF. - int magic; - /// The inode of the file. - unsigned int inode; - /// The name of the file. - char name[INITRD_NAME_MAX]; - /// The type of the file. - short int file_type; - /// The permissions mask. - unsigned int mask; - /// The id of the owner. - unsigned int uid; - /// The id of the group. - unsigned int gid; - /// Time of last access. - unsigned int atime; - /// Time of last data modification. - unsigned int mtime; - /// Time of last status change. - unsigned int ctime; - /// Offset of the starting address. - unsigned int offset; - /// Dimension of the file. - unsigned int length; -} __attribute__((aligned(16))) initrd_file_t; - -/// @brief The details regarding the filesystem. -/// @brief Contains the number of files inside the initrd filesystem. -typedef struct initrd_t { - /// Number of files. - unsigned int nfiles; - /// List of headers. - initrd_file_t headers[INITRD_MAX_FILES]; -} __attribute__((aligned(16))) initrd_t; - -// Prepare a variable to keep track of the offsets inside the FS. By -// default skip the initial space for the FS details structure. -static int fs_offset = sizeof(initrd_t); - -static inline void usage(char *prgname) -{ - printf("Usage:\n"); - printf(" %s --help For this screen\n", prgname); - printf(" %s --source [-s] The source directory.\n", prgname); - printf(" %s --target [-t] The target file for the initrd.\n", prgname); -} - -static inline void version(char *prgname) -{ - printf("%s version: %s\n", prgname, INITFSCP_VER); -} - -static inline int is_option_mount_point(char *arg) -{ - return ((strcmp(arg, "-m") == 0) || (strcmp(arg, "--mountpoint") == 0)); -} - -static inline int is_option_source(char *arg) -{ - return ((strcmp(arg, "-s") == 0) || (strcmp(arg, "--source") == 0)); -} - -static inline int is_option_target(char *arg) -{ - return ((strcmp(arg, "-t") == 0) || (strcmp(arg, "--target") == 0)); -} - -static int open_target_fs(int argc, char *argv[]) -{ - printf("%-64s", "Opening target filesystem..."); - int fd = -1; - for (int i = 1; i < argc; ++i) { - if (is_option_target(argv[i]) && ((i + 1) < argc)) { - fd = open(argv[i + 1], O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO); - break; - } - } - if (fd == -1) - printf("[" RED "FAILED" RESET "]\n\n"); - else - printf("[" GREEN "DONE" RESET "]\n\n"); - return fd; -} - -static void init_headers(initrd_t *initrd) -{ - printf("%-64s", "Initializing headers structures..."); - for (size_t i = 0; i < INITRD_MAX_FILES; i++) { - initrd->headers[i].magic = 0xBF; - initrd->headers[i].inode = i; - initrd->headers[i].file_type = 0; - initrd->headers[i].mask = 0; - initrd->headers[i].uid = 0; - initrd->headers[i].gid = 0; - initrd->headers[i].atime = time(NULL); - initrd->headers[i].mtime = time(NULL); - initrd->headers[i].ctime = time(NULL); - initrd->headers[i].offset = 0; - initrd->headers[i].length = 0; - - memset(initrd->headers[i].name, 0, INITRD_NAME_MAX); - } - printf("[" GREEN "DONE" RESET "]\n\n"); -} - -static int create_file_headers(initrd_t *initrd, char *mountpoint, char *directory) -{ - assert(mountpoint && "Received null mountpoint."); - assert(directory && "Received null directory."); - - // ------------------------------------------------------------------------ - char directory_path_abs[PATH_MAX], entry_path_abs[PATH_MAX], fs_abs_path[PATH_MAX]; - memset(directory_path_abs, 0, PATH_MAX); - memset(entry_path_abs, 0, PATH_MAX); - memset(fs_abs_path, 0, PATH_MAX); - strcpy(directory_path_abs, mountpoint); - strcat(directory_path_abs, directory); - - int dir_fd = open(directory_path_abs, O_RDONLY); - if (dir_fd == -1) { - printf("[" RED "FAILED" RESET "]\n"); - printf("Could not open source directory %s.\n", directory_path_abs); - return 0; - } - - // ------------------------------------------------------------------------ - char buffer[BUFSIZ], *ebuf; - off_t basep; - int nbytes; - while ((nbytes = getdirentries(dir_fd, buffer, BUFSIZ, &basep)) > 0) { - ebuf = buffer + nbytes; - char *cp = buffer; - while (cp < ebuf) { - struct dirent *entry = (struct dirent *)cp; - if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { - cp += entry->d_reclen; - continue; - } - // Create the host machine absolute path. - strcpy(entry_path_abs, directory_path_abs); - strcat(entry_path_abs, "/"); - strcat(entry_path_abs, entry->d_name); - // Create the filesystem absolute path. - strcpy(fs_abs_path, directory); - strcat(fs_abs_path, "/"); - strcat(fs_abs_path, entry->d_name); - // Print the entry. - printf("[%3d] ENTRY: %-52s", initrd->nfiles, fs_abs_path); - // Stat the file. - struct stat _stat; - if (stat(entry_path_abs, &_stat) == -1) { - printf("[" RED "FAILED" RESET "]\n"); - printf("Error while executing stat on file : %s\n", entry_path_abs); - return 0; - } - printf("[%c%c%c%c%c%c%c%c%c%c]", - dt_char_array[entry->d_type], - (_stat.st_mode & S_IRUSR) != 0 ? 'r' : '-', - (_stat.st_mode & S_IWUSR) != 0 ? 'w' : '-', - (_stat.st_mode & S_IXUSR) != 0 ? 'x' : '-', - (_stat.st_mode & S_IRGRP) != 0 ? 'r' : '-', - (_stat.st_mode & S_IWGRP) != 0 ? 'w' : '-', - (_stat.st_mode & S_IXGRP) != 0 ? 'x' : '-', - (_stat.st_mode & S_IROTH) != 0 ? 'r' : '-', - (_stat.st_mode & S_IWOTH) != 0 ? 'w' : '-', - (_stat.st_mode & S_IXOTH) != 0 ? 'x' : '-'); - - if (entry->d_type != DT_DIR) { - // ---------------------------------------------------------------- - // Copy the name and the main info. - strcpy(initrd->headers[initrd->nfiles].name, fs_abs_path); - initrd->headers[initrd->nfiles].file_type = DT_REG; - initrd->headers[initrd->nfiles].length = _stat.st_size; - initrd->headers[initrd->nfiles].offset = fs_offset; - initrd->headers[initrd->nfiles].mask = _stat.st_mode; - initrd->headers[initrd->nfiles].uid = _stat.st_uid; - initrd->headers[initrd->nfiles].gid = _stat.st_gid; - initrd->headers[initrd->nfiles].atime = _stat.st_atim.tv_sec; - initrd->headers[initrd->nfiles].mtime = _stat.st_mtim.tv_sec; - initrd->headers[initrd->nfiles].ctime = _stat.st_ctim.tv_sec; - - // ---------------------------------------------------------------- - printf("[OFFSET:%6d]", initrd->headers[initrd->nfiles].offset); - printf("[SIZE:%6d]", initrd->headers[initrd->nfiles].length); - printf("[" GREEN "DONE" RESET "]\n"); - - fs_offset += initrd->headers[initrd->nfiles].length; - ++initrd->nfiles; - } else { - // ---------------------------------------------------------------- - strcpy(initrd->headers[initrd->nfiles].name, fs_abs_path); - initrd->headers[initrd->nfiles].file_type = DT_DIR; - initrd->headers[initrd->nfiles].length = _stat.st_size; - initrd->headers[initrd->nfiles].offset = fs_offset; - initrd->headers[initrd->nfiles].mask = _stat.st_mode; - initrd->headers[initrd->nfiles].uid = _stat.st_uid; - initrd->headers[initrd->nfiles].gid = _stat.st_gid; - initrd->headers[initrd->nfiles].atime = _stat.st_atim.tv_sec; - initrd->headers[initrd->nfiles].mtime = _stat.st_mtim.tv_sec; - initrd->headers[initrd->nfiles].ctime = _stat.st_ctim.tv_sec; - - // ---------------------------------------------------------------- - printf("[OFFSET:%6d]", initrd->headers[initrd->nfiles].offset); - printf("[SIZE:%6d]", initrd->headers[initrd->nfiles].length); - printf("[" GREEN "DONE" RESET "]\n"); - - // ---------------------------------------------------------------- - ++initrd->nfiles; - - // ---------------------------------------------------------------- - if (!create_file_headers(initrd, mountpoint, fs_abs_path)) { - break; - } - } - cp += entry->d_reclen; - } - } - close(dir_fd); - return 1; -} - -static int write_file_system(initrd_t *initrd, int target_fd, char *mountpoint) -{ - printf("Copying data to filesystem...\n"); - for (int i = 0; i < INITRD_MAX_FILES; i++) { - // -------------------------------------------------------------------- - char absolute_path[256]; - memset(absolute_path, 0, 256); - strcpy(absolute_path, mountpoint); - strcat(absolute_path, initrd->headers[i].name); - - // -------------------------------------------------------------------- - if (initrd->headers[i].file_type == DT_REG) { - FILE *fd2 = fopen(absolute_path, "r+"); - if (fd2 == NULL) { - continue; - } - printf("[%3d] FILE: %-92s", i, absolute_path); - char *buffer = (char *)malloc(initrd->headers[i].length); - fread(buffer, 1, initrd->headers[i].length, fd2); - // Write the content. - write(target_fd, buffer, initrd->headers[i].length); - - printf("[" GREEN "DONE" RESET "]\n"); - fclose(fd2); - free(buffer); - } - } - printf("[" GREEN "DONE" RESET "]\n\n"); - return 1; -} - -int main(int argc, char *argv[]) -{ - printf("Welcome to MentOS initrd file copier tool\n\n"); - if (argc <= 1) { - if (argv[0][0] == '.') { - version(argv[0] + 2); - } - usage(argv[0]); - return EXIT_FAILURE; - } - if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v")) { - version(argv[0] + 2); - return EXIT_SUCCESS; - } - if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) { - usage(argv[0]); - return EXIT_SUCCESS; - } - - // Create the filesystem details structure. - initrd_t initrd; - // Target fs file descriptor. - int target_fd; - - // ------------------------------------------------------------------------ - // Open the fs. - if ((target_fd = open_target_fs(argc, argv)) == -1) { - printf("Could not open target FileSystem.\n"); - return EXIT_FAILURE; - } - - // ------------------------------------------------------------------------ - // Initialize the headers. - init_headers(&initrd); - - // ------------------------------------------------------------------------ - // Create file headers. - printf("Creating headers...\n"); - for (unsigned int i = 1; i < argc; ++i) { - if (is_option_source(argv[i]) && ((i + 1) < argc)) { - // ---------------------------------------------------------------- - strcpy(initrd.headers[initrd.nfiles].name, "/"); - initrd.headers[initrd.nfiles].file_type = DT_DIR; - initrd.headers[initrd.nfiles].length = 0; - initrd.headers[initrd.nfiles].offset = fs_offset; - initrd.headers[initrd.nfiles].mask = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; - initrd.headers[initrd.nfiles].uid = 0; - initrd.headers[initrd.nfiles].gid = 0; - initrd.headers[initrd.nfiles].atime = time(NULL); - initrd.headers[initrd.nfiles].mtime = time(NULL); - initrd.headers[initrd.nfiles].ctime = time(NULL); - - printf("[%3d] DIR : %-52s", initrd.nfiles, initrd.headers[initrd.nfiles].name); - printf("[" BLUE "OPEN" RESET "]"); - printf("[OFFSET:%6d]", initrd.headers[initrd.nfiles].offset); - printf("[SIZE:%6d]", initrd.headers[initrd.nfiles].length); - printf("[" GREEN "DONE" RESET "]\n"); - ++initrd.nfiles; - - // ---------------------------------------------------------------- - if (!create_file_headers(&initrd, argv[i + 1], "")) { - printf("Could not create file headers.\n"); - close(target_fd); - return EXIT_FAILURE; - } - } - } - printf("[" GREEN "DONE" RESET "]\n\n"); - - // ------------------------------------------------------------------------ - // Copying information about headers on filesystem. - printf("%-64s", "Copying the filesystem's details structure..."); - write(target_fd, &initrd, sizeof(initrd_t)); - printf("[" GREEN "DONE" RESET "]\n\n"); - - // ------------------------------------------------------------------------ - // Write headers on filesystem. - for (unsigned int i = 1; i < argc; ++i) { - if (is_option_source(argv[i]) && ((i + 1) < argc)) { - if (!write_file_system(&initrd, target_fd, argv[i + 1])) { - printf("Could not write on filesystem.\n"); - close(target_fd); - return EXIT_FAILURE; - } - } - } - - // ------------------------------------------------------------------------ - lseek(target_fd, 0L, SEEK_SET); - size_t fs_size = lseek(target_fd, 0L, SEEK_END); - printf("FS size : %ld\n", fs_size); - if (fs_size < INITRD_MAX_FS_SIZE - 1) { - printf("Extend FS size to : %d\n", INITRD_MAX_FS_SIZE - 1); - lseek(target_fd, INITRD_MAX_FS_SIZE - 1, SEEK_SET); - write(target_fd, 0, 1); - } - close(target_fd); - return 0; -} diff --git a/libc/src/unistd/exec.c b/libc/src/unistd/exec.c index e66ee70..f3e0cd7 100644 --- a/libc/src/unistd/exec.c +++ b/libc/src/unistd/exec.c @@ -63,7 +63,7 @@ static inline int __find_in_path(const char *file, char *buf, size_t buf_len) /// image (argument vector), allows the caller to specify /// the environment of the executed program via `envp`. /// @return Returns -1 only if an error has occurred, and sets errno. -_syscall3(int, execve, const char *, path, char *const *, argv, char *const *, envp); +_syscall3(int, execve, const char *, path, char *const *, argv, char *const *, envp) int execv(const char *path, char *const argv[]) { diff --git a/mentos/CMakeLists.txt b/mentos/CMakeLists.txt index 8ffba3d..586c835 100644 --- a/mentos/CMakeLists.txt +++ b/mentos/CMakeLists.txt @@ -147,7 +147,6 @@ set(KERNEL_SOURCES src/drivers/ps2.c src/drivers/keyboard/keyboard.c src/drivers/keyboard/keymap.c - src/fs/initrd.c src/fs/vfs.c src/fs/read_write.c src/fs/open.c diff --git a/mentos/inc/fs/initrd.h b/mentos/inc/fs/initrd.h deleted file mode 100644 index 54113f2..0000000 --- a/mentos/inc/fs/initrd.h +++ /dev/null @@ -1,14 +0,0 @@ -/// @file initrd.h -/// @brief Headers of functions for initrd filesystem. -/// @copyright (c) 2014-2021 This file is distributed under the MIT License. -/// See LICENSE.md for details. - -#pragma once - -/// @brief Initialize the initrd filesystem. -/// @return 0 if fails, 1 if succeed. -int initrd_init_module(void); - -/// @brief Clean up the initrd filesystem. -/// @return 0 if fails, 1 if succeed. -int initrd_cleanup_module(void); diff --git a/mentos/src/fs/initrd.c b/mentos/src/fs/initrd.c deleted file mode 100644 index 8aeff72..0000000 --- a/mentos/src/fs/initrd.c +++ /dev/null @@ -1,689 +0,0 @@ -/// @file initrd.c -/// @brief Headers of functions for initrd filesystem. -/// @copyright (c) 2014-2021 This file is distributed under the MIT License. -/// See LICENSE.md for details. - -#include "assert.h" -#include "system/syscall.h" -#include "sys/module.h" -#include "system/panic.h" -#include "fs/vfs.h" -#include "sys/errno.h" -#include "io/debug.h" -#include "mem/kheap.h" -#include "fcntl.h" -#include "sys/bitops.h" -#include "fs/initrd.h" -#include "string.h" -#include "stdio.h" -#include "libgen.h" -#include "fcntl.h" - -/// Maximum length of name in INITRD. -#define INITRD_NAME_MAX 255U -/// Maximum number of files in INITRD. -#define INITRD_MAX_FILES 128U -/// Maximum size of files in INITRD. -#define INITRD_MAX_FS_SIZE 1048576 - -/// @brief Information concerning a file. -typedef struct initrd_file_t { - /// Number used as delimiter, it must be set to 0xBF. - int magic; - /// The inode of the file. - unsigned int inode; - /// The name of the file. - char fileName[INITRD_NAME_MAX]; - /// The type of the file. - short int file_type; - /// The permissions mask. - unsigned int mask; - /// The id of the owner. - unsigned int uid; - /// The id of the group. - unsigned int gid; - /// Time of last access. - unsigned int atime; - /// Time of last data modification. - unsigned int mtime; - /// Time of last status change. - unsigned int ctime; - /// Offset of the starting address. - unsigned int offset; - /// Dimension of the file. - unsigned int length; -} __attribute__((aligned(16))) initrd_file_t; - -/// @brief The details regarding the filesystem. -/// @brief Contains the number of files inside the initrd filesystem. -static struct initrd_t { - /// Number of files. - unsigned int nfiles; - /// List of headers. - initrd_file_t headers[INITRD_MAX_FILES]; -} fs_specs __attribute__((aligned(16))); - -static vfs_file_t *initrd_create_file_struct( - ino_t ino, - const char *name, - size_t size, - int flags); - -/// @brief Searches for the file at the given path. -/// @param path The path where to search the file. -/// @return The file if found, NULL otherwise. -static inline initrd_file_t *initrd_find_file(const char *path) -{ - for (unsigned int i = 0; i < INITRD_MAX_FILES; ++i) - if (strcmp(path, fs_specs.headers[i].fileName) == 0) - return &(fs_specs.headers[i]); - return NULL; -} - -/// @brief Searches for the file at the given path. -/// @param path The path where to search the file. -/// @return The file if found, NULL otherwise. -static inline ino_t initrd_find_inode(const char *path) -{ - for (unsigned int i = 0; i < INITRD_MAX_FILES; ++i) - if (strcmp(path, fs_specs.headers[i].fileName) == 0) - return fs_specs.headers[i].inode; - return -1; -} - -static inline initrd_file_t *get_free_header() -{ - for (size_t i = 0; i < INITRD_MAX_FILES; ++i) - if (fs_specs.headers[i].file_type == 0) - return &(fs_specs.headers[i]); - return NULL; -} - -static inline bool_t check_if_occupied(size_t offset) -{ - for (size_t i = 0; i < INITRD_MAX_FILES; ++i) { - initrd_file_t *h = &fs_specs.headers[i]; - if ((h->file_type != 0) && (offset >= h->offset) && (offset <= (h->offset + h->length))) { - return true; - } - } - return false; -} - -static inline int get_free_slot_offset() -{ - int offset = sizeof(struct initrd_t); - for (size_t i = 0; i < INITRD_MAX_FILES; ++i) { - initrd_file_t *h = &fs_specs.headers[i]; - if ((h->file_type != 0) && (offset >= h->offset) && (offset <= (h->offset + h->length))) { - offset = (int)(h->offset + h->length); - continue; - } - return offset; - } - return -1; -} - -// TODO: doxygen comment. -static void dump_initrd_fs(void) -{ - for (size_t i = 0; i < INITRD_MAX_FILES; ++i) { - initrd_file_t *file = &fs_specs.headers[i]; - pr_debug("[%3d][%c%c%c%c%c%c%c%c%c%c] %s\n", - i, - dt_char_array[file->file_type], - (file->mask & S_IRUSR) != 0 ? 'r' : '-', - (file->mask & S_IWUSR) != 0 ? 'w' : '-', - (file->mask & S_IXUSR) != 0 ? 'x' : '-', - (file->mask & S_IRGRP) != 0 ? 'r' : '-', - (file->mask & S_IWGRP) != 0 ? 'w' : '-', - (file->mask & S_IXGRP) != 0 ? 'x' : '-', - (file->mask & S_IROTH) != 0 ? 'r' : '-', - (file->mask & S_IWOTH) != 0 ? 'w' : '-', - (file->mask & S_IXOTH) != 0 ? 'x' : '-', - file->fileName); - } -} - -/// @brief Reads contents of the directories to a dirent buffer, updating -/// the offset and returning the number of written bytes in the buffer, -/// it assumes that all paths are well-formed. -/// @param file The directory handler. -/// @param dirp The buffer where the data should be written. -/// @param doff The offset inside the buffer where the data should be written. -/// @param count The maximum length of the buffer. -/// @return The number of written bytes in the buffer. -static int initrd_getdents(vfs_file_t *file, dirent_t *dirp, off_t doff, size_t count) -{ - if (file->ino >= INITRD_MAX_FILES) { - return -1; - } - - initrd_file_t *tdir = &fs_specs.headers[file->ino]; - int len = strlen(tdir->fileName); - size_t written = 0; - off_t current = 0; - - char *parent = NULL; - for (off_t it = 0; (it < INITRD_MAX_FILES) && (written < count); ++it) { - initrd_file_t *entry = &fs_specs.headers[it]; - if (entry->fileName[0] == '\0') { - continue; - } - // If the entry is the directory itself, skip. - if (strcmp(tdir->fileName, entry->fileName) == 0) { - continue; - } - // Get the parent directory. - parent = dirname(entry->fileName); - // Check if the entry is inside the directory. - if (strcmp(tdir->fileName, parent) != 0) { - continue; - } - // Skip if already provided. - current += sizeof(dirent_t); - if (current <= doff) { - continue; - } - - if (*(entry->fileName + len) == '/') - ++len; - // Write on current dirp. - dirp->d_ino = it; - dirp->d_type = entry->file_type; - strcpy(dirp->d_name, entry->fileName + len); - dirp->d_off = sizeof(dirent_t); - dirp->d_reclen = sizeof(dirent_t); - // Increment the written counter. - written += sizeof(dirent_t); - // Move to next writing position. - dirp += 1; - } - return written; -} - -/// @brief Creates a new directory. -/// @param path The path to the new directory. -/// @param mode The file mode. -/// @return 0 if success. -static int initrd_mkdir(const char *path, mode_t mode) -{ - if ((strcmp(path, ".") == 0) || (strcmp(path, "..") == 0)) { - return -EPERM; - } - initrd_file_t *direntry = initrd_find_file(path); - if (direntry != NULL) { - return -EEXIST; - } - // Check if the directories before it exist. - char *parent = dirname(path); - if ((strcmp(parent, ".") != 0) && (strcmp(parent, "/") != 0)) { - direntry = initrd_find_file(parent); - if (direntry == NULL) { - return -ENOENT; - } - if (direntry->file_type != DT_DIR) { - return -ENOTDIR; - } - } - // Get a free header. - initrd_file_t *initrd_file = get_free_header(); - if (!initrd_file) { - pr_err("Cannot create initrd_file for `%s`...\n", path); - return -ENFILE; - } - int offset = get_free_slot_offset(); - if (offset < 0) { - pr_err("There are no free slot available for `%s`...\n", path); - return -ENFILE; - } - // Create the file. - initrd_file->magic = 0xBF; - strcpy(initrd_file->fileName, path); - initrd_file->file_type = DT_DIR; - initrd_file->uid = scheduler_get_current_process()->uid; - initrd_file->gid = scheduler_get_current_process()->uid; - initrd_file->offset = offset; - initrd_file->length = 0; - initrd_file->atime = sys_time(NULL); - initrd_file->mtime = sys_time(NULL); - initrd_file->ctime = sys_time(NULL); - // Increase the number of files. - ++fs_specs.nfiles; - return 0; -} - -/// @brief Removes a directory. -/// @param path The path to the directory. -/// @return 0 if success. -static int initrd_rmdir(const char *path) -{ - if ((strcmp(path, ".") == 0) || (strcmp(path, "..") == 0)) { - pr_err("initrd_rmdir(%s): Cannot remove `.` or `..`.\n", path); - return -EPERM; - } - // Check if the directory exists. - initrd_file_t *direntry = initrd_find_file(path); - if (direntry == NULL) { - pr_err("initrd_rmdir(%s): Cannot find the directory.\n", path); - return -ENOENT; - } - // Check the type. - if (direntry->file_type != DT_DIR) { - pr_err("initrd_rmdir(%s): The entry is not a directory.\n", path); - return -ENOTDIR; - } - for (int i = 0; i < INITRD_MAX_FILES; ++i) { - initrd_file_t *entry = &fs_specs.headers[i]; - if (entry->fileName[0] == '\0') { - continue; - } - // Get the directory of the file. - char *filedir = dirname(entry->fileName); - // Check if directory path and file directory are the same. - if (strcmp(direntry->fileName, filedir) == 0) { - pr_err("initrd_rmdir(%s): The directory is not empty.\n", path); - return -ENOTEMPTY; - } - } - // Remove the directory. - direntry->magic = 0; - memset(direntry->fileName, 0, NAME_MAX); - direntry->file_type = 0; - direntry->uid = 0; - direntry->offset = 0; - direntry->length = 0; - // Decrease the number of files. - --fs_specs.nfiles; - return 0; -} - -/// @brief Open the file at the given path and returns its file descriptor. -/// @param path The path to the file. -/// @param flags The flags used to determine the behavior of the function. -/// @param mode The mode with which we open the file. -/// @return The file descriptor of the opened file, otherwise returns -1. -static vfs_file_t *initrd_open(const char *path, int flags, mode_t mode) -{ - initrd_file_t *initrd_file = initrd_find_file(path); - if (initrd_file != NULL) { - // Check if it is a directory. - if (flags == (O_RDONLY | O_DIRECTORY)) { - if (initrd_file->file_type != DT_DIR) { - pr_err("Is not a directory `%s`...\n", path); - errno = ENOTDIR; - return NULL; - } - // Create the file structure. - vfs_file_t *vfs_file = initrd_create_file_struct( - initrd_file->inode, - initrd_file->fileName, - initrd_file->length, - DT_DIR); - if (!vfs_file) { - pr_err("Cannot create vfs file for opening directory `%s`...\n", path); - errno = ENOMEM; - return NULL; - } - // Update file access. - initrd_file->atime = sys_time(NULL); - return vfs_file; - } else if (initrd_file->file_type == DT_DIR) { - pr_err("Is a directory `%s`...\n", path); - errno = EISDIR; - return NULL; - } - // Check if the open has to create. - if (flags & O_CREAT) { - pr_err("Cannot create, it exists `%s`...\n", path); - errno = EEXIST; - return NULL; - } - // Create the file structure. - vfs_file_t *vfs_file = initrd_create_file_struct( - initrd_file->inode, - initrd_file->fileName, - initrd_file->length, - DT_REG); - if (!vfs_file) { - pr_err("Cannot create vfs file for opening file `%s`...\n", path); - errno = ENOMEM; - return NULL; - } - // Update file access. - initrd_file->atime = sys_time(NULL); - return vfs_file; - } - if (flags & O_CREAT) { - // Check if the parent directory exists. - char *dir = dirname(path); - if ((strcmp(dir, ".") != 0) && (strcmp(dir, "/") != 0)) { - if (initrd_find_file(dir) == NULL) { - errno = ENOENT; - return NULL; - } - } - // Get a free header. - initrd_file = get_free_header(); - if (!initrd_file) { - pr_err("Cannot create initrd_file for `%s`...\n", path); - errno = ENFILE; - return NULL; - } - int offset = get_free_slot_offset(); - if (offset < 0) { - pr_err("There are no free slot available for `%s`...\n", path); - errno = ENFILE; - return NULL; - } - // Create the file. - initrd_file->magic = 0xBF; - strcpy(initrd_file->fileName, path); - initrd_file->file_type = DT_REG; - initrd_file->mask = S_IRWXU; - initrd_file->uid = scheduler_get_current_process()->uid; - initrd_file->gid = scheduler_get_current_process()->uid; - initrd_file->offset = offset; - initrd_file->length = 0; - initrd_file->atime = sys_time(NULL); - initrd_file->mtime = sys_time(NULL); - initrd_file->ctime = sys_time(NULL); - // Increase the number of files. - ++fs_specs.nfiles; - // Create the file structure. - vfs_file_t *vfs_file = initrd_create_file_struct( - initrd_file->inode, - initrd_file->fileName, - initrd_file->length, - DT_REG); - if (!vfs_file) { - pr_err("Cannot create vfs file for opening file `%s`...\n", path); - errno = ENOMEM; - return NULL; - } - return vfs_file; - } - errno = ENOENT; - return NULL; -} - -/// @brief Deletes the file at the given path. -/// @param path The path to the file. -/// @return On success, zero is returned. On error, -1 is returned. -static int initrd_unlink(const char *path) -{ - if ((strcmp(path, ".") == 0) || (strcmp(path, "..") == 0)) { - return -EPERM; - } - // Check if the directory exists. - initrd_file_t *file = initrd_find_file(path); - if (file == NULL) { - pr_err("initrd_unlink(%s): Cannot find the file.\n", path); - return -ENOENT; - } - if (file->file_type != DT_REG) { - if (file->file_type == DT_DIR) { - pr_err("initrd_unlink(%s): The file is a directory.\n", path); - return -EISDIR; - } - pr_err("initrd_unlink(%s): The file is not a regular file.\n", path); - return -EACCES; - } - // Remove the directory. - file->magic = 0; - memset(file->fileName, 0, NAME_MAX); - file->file_type = 0; - file->uid = 0; - file->offset = 0; - file->length = 0; - // Decrease the number of files. - --fs_specs.nfiles; - return 0; -} - -/// @brief Reads from the file identified by the file descriptor. -/// @param file The file. -/// @param buf Buffer where the read content must be placed. -/// @param offset Offset from which we start reading from the file. -/// @param nbyte The number of bytes to read. -/// @return The number of red bytes. -static ssize_t initrd_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyte) -{ - // If the number of byte to read is zero, skip. - if (nbyte == 0) { - return 0; - } - // Get the file descriptor of the file. - int lfd = file->ino; - // Get the current position. - int read_pos = offset; - // Get the length of the file. - int file_size = (int)fs_specs.headers[lfd].length; - // If we have reached the end of the file, return. - if (read_pos == file_size) { - return EOF; - } - // Get the begin of the file. - char *file_start = (char *)(modules[0].mod_start + fs_specs.headers[lfd].offset); - // Declare an iterator, used afterward to return the number of bytes read. - ssize_t it = 0; - while ((it < nbyte) && (read_pos < file_size)) { - *buf++ = file_start[read_pos]; - ++read_pos; - ++it; - } - return it; -} - -static int _initrd_stat(const initrd_file_t *file, stat_t *stat) -{ - stat->st_dev = 0; - stat->st_ino = file - fs_specs.headers; - stat->st_mode = file->mask; - stat->st_uid = file->uid; - stat->st_gid = file->gid; - stat->st_atime = file->atime; - stat->st_mtime = file->mtime; - stat->st_ctime = file->ctime; - stat->st_size = file->length; - return 0; -} - -/// @brief Retrieves information concerning the file at the given position. -/// @param file The file struct. -/// @param stat The structure where the information are stored. -/// @return 0 if success. -static int initrd_fstat(vfs_file_t *file, stat_t *stat) -{ - return _initrd_stat(&fs_specs.headers[file->ino], stat); -} - -/// @brief Retrieves information concerning the file at the given position. -/// @param path The path where the file resides. -/// @param stat The structure where the information are stored. -/// @return 0 if success. -static int initrd_stat(const char *path, stat_t *stat) -{ - int i; - i = 0; - - while (i < INITRD_MAX_FILES) { - if (!strcmp(path, fs_specs.headers[i].fileName)) { - stat->st_uid = fs_specs.headers[i].uid; - stat->st_size = fs_specs.headers[i].length; - break; - } - i++; - } - - if (i == INITRD_MAX_FILES) { - return -ENOENT; - } else { - return _initrd_stat(&fs_specs.headers[i], stat); - } -} - -/// @brief Writes the given content inside the file. -/// @param file The file descriptor of the file. -/// @param buf The content to write. -/// @param offset Offset from which we start writing in the file. -/// @param nbyte The number of bytes to write. -/// @return The number of written bytes. -static ssize_t initrd_write(vfs_file_t *file, const void *buf, off_t offset, size_t nbyte) -{ - // Get the header. - initrd_file_t *header = &fs_specs.headers[file->ino]; - // If the number of byte to write is zero, skip. - if (nbyte == 0) { - return 0; - } - if (check_if_occupied(offset + nbyte)) { - pr_emerg("We need to move the file.\n"); - TODO("Implement file movement."); - } - // Prepare pointers to the contents. - char *dest = (char *)(&fs_specs + header->offset + offset), *src = (char *)buf; - // Copy the content. - int num = 0; - while ((num < nbyte) && (*dest++ = *src++)) { - ++num; - } - dest[num] = '\0'; - dest[num + 1] = EOF; - // Increment the length of the file. - header->length += num; - return num; -} - -/// @brief Repositions the file offset inside a file. -/// @param file the file we are working with. -/// @param offset the offest to use for the operation. -/// @param whence the type of operation. -/// @return Upon successful completion, returns the resulting offset -/// location as measured in bytes from the beginning of the file. On -/// error, the value (off_t) -1 is returned and errno is set to -/// indicate the error. -static off_t initrd_lseek(vfs_file_t *file, off_t offset, int whence) -{ - // Get the header. - initrd_file_t *header = &fs_specs.headers[file->ino]; - - switch (whence) { - case SEEK_END: - offset += header->length; - break; - case SEEK_CUR: - if (offset == 0) { - return file->f_pos; - } - offset += file->f_pos; - break; - case SEEK_SET: - break; - default: - return -EINVAL; - } - if (offset >= 0) { - if (offset != file->f_pos) { - file->f_pos = offset; - } - return offset; - } - return -EINVAL; -} - -/// @brief Closes the given file. -/// @param file The file structure. -static int initrd_close(vfs_file_t *file) -{ - assert(file && "Received null file."); - // Remove the file from the list of the corresponding entry inside the `header_files`. - list_head_del(&file->siblings); - // Free the memory of the file. - kmem_cache_free(file); - return 0; -} - -/// Filesystem general operations. -static vfs_sys_operations_t initrd_sys_operations = { - .mkdir_f = initrd_mkdir, - .rmdir_f = initrd_rmdir, - .stat_f = initrd_stat -}; - -/// Filesystem file operations. -static vfs_file_operations_t initrd_fs_operations = { - .open_f = initrd_open, - .unlink_f = initrd_unlink, - .close_f = initrd_close, - .read_f = initrd_read, - .write_f = initrd_write, - .lseek_f = initrd_lseek, - .stat_f = initrd_fstat, - .ioctl_f = NULL, - .getdents_f = initrd_getdents -}; - -static vfs_file_t *initrd_create_file_struct( - ino_t ino, - const char *name, - size_t size, - int flags) -{ - vfs_file_t *file = kmem_cache_alloc(vfs_file_cache, GFP_KERNEL); - if (!file) { - pr_err("Failed to allocation memory for the file."); - return NULL; - } - memset(file, 0, sizeof(vfs_file_t)); - - strcpy(file->name, name); - file->device = (void *)file; - file->ino = ino; - file->uid = 0; - file->gid = 0; - file->mask = S_IRUSR | S_IRGRP | S_IROTH; - file->length = size; - file->flags = flags; - file->sys_operations = &initrd_sys_operations; - file->fs_operations = &initrd_fs_operations; - - return file; -} - -static vfs_file_t *initrd_mount_callback(const char *path, const char *device) -{ - dump_initrd_fs(); - // Create the associated file. - vfs_file_t *vfs_file = initrd_create_file_struct(0, path, 0, DT_DIR); - assert(vfs_file && "Failed to create vfs_file."); - // Initialize the proc_root. - return vfs_file; -} - -/// Filesystem information. -static file_system_type initrd_file_system_type = { - .name = "initrd", - .fs_flags = 0, - .mount = initrd_mount_callback -}; - -int initrd_init_module(void) -{ - for (int i = 0; i < MAX_MODULES; ++i) { - if (strcmp((char *)modules[i].cmdline, "initrd") == 0) { - assert(sizeof(struct initrd_t) <= (modules[i].mod_end - modules[i].mod_start)); - // Copy the FS specification. - memcpy(&fs_specs, (void *)modules[i].mod_start, sizeof(struct initrd_t)); - // Register the filesystem. - vfs_register_filesystem(&initrd_file_system_type); - } - } - return 0; -} - -int initrd_cleanup_module(void) -{ - vfs_unregister_filesystem(&initrd_file_system_type); - return 0; -} \ No newline at end of file diff --git a/mentos/src/fs/procfs.c b/mentos/src/fs/procfs.c index 5aea051..629b363 100644 --- a/mentos/src/fs/procfs.c +++ b/mentos/src/fs/procfs.c @@ -63,7 +63,7 @@ typedef struct procfs_file_t { } procfs_file_t; /// @brief The details regarding the filesystem. -/// @brief Contains the number of files inside the initrd filesystem. +/// @brief Contains the number of files inside the procfs filesystem. typedef struct procfs_t { /// Number of files. unsigned int nfiles; diff --git a/mentos/src/fs/stat.c b/mentos/src/fs/stat.c index 1f882bc..e6c9bb8 100644 --- a/mentos/src/fs/stat.c +++ b/mentos/src/fs/stat.c @@ -9,7 +9,6 @@ #include "mem/kheap.h" #include "stdio.h" #include "string.h" -#include "fs/initrd.h" #include "limits.h" int sys_stat(const char *path, stat_t *buf) diff --git a/mentos/src/kernel.c b/mentos/src/kernel.c index f04eeb3..7453155 100644 --- a/mentos/src/kernel.c +++ b/mentos/src/kernel.c @@ -25,7 +25,6 @@ #include "hardware/pic8259.h" #include "io/debug.h" #include "drivers/fdc.h" -#include "fs/initrd.h" #include "fs/ext2.h" #include "klib/irqflags.h" #include "drivers/keyboard/keyboard.h" @@ -251,22 +250,6 @@ int kmain(boot_info_t *boot_informations) return 1; } - //========================================================================== -#if 0 - pr_notice(" Initialize 'initrd'...\n"); - printf(" Initialize 'initrd'..."); - if (initrd_init_module()) { - print_fail(); - pr_emerg("Failed to register `initrd`!\n"); - return 1; - } - print_ok(); - if (do_mount("initrd", "/", "/dev/ram0")) { - pr_emerg("Failed to mount root `/`!\n"); - return 1; - } -#endif - //========================================================================== pr_notice(" Initialize 'procfs'...\n"); printf(" Initialize 'procfs'..."); diff --git a/mentos/src/klib/libgen.c b/mentos/src/klib/libgen.c index ac5114f..220cd46 100644 --- a/mentos/src/klib/libgen.c +++ b/mentos/src/klib/libgen.c @@ -6,7 +6,6 @@ #include "system/syscall.h" #include "libgen.h" #include "string.h" -#include "fs/initrd.h" #include "limits.h" #include "assert.h" #include "mem/paging.h" diff --git a/scripts/replace_links.sh b/scripts/replace_links.sh deleted file mode 100644 index 7a6d452..0000000 --- a/scripts/replace_links.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -find -type l -exec git update-index --assume-unchanged {} \; -find -type l -exec bash -c 'ln -f "$(readlink -m "$0")" "$0"' {} \; \ No newline at end of file