Exemplo n.º 1
0
int ext2_remove_file(uint8_t *fs, char *path) {
    uint32_t inum = get_inode_by_path(fs, path);
    if (!inum) {
        DEBUG("Bad path");
        return 0;
    }
    struct ext2_inode *inode = get_inode(fs, inum);
    // check if file
    if (!ext2_inode_has_mode(inode, EXT2_S_IFREG)) {
        DEBUG("FILE DELETE: Not a file %x", inode->i_mode);
        return 0;
    }
    // get directory path
    int num_parts = 0;
    char **parts = split_path(path, &num_parts);
    if (!num_parts) {
        DEBUG("No parts");
        return 0;
    }
    int newstring_size = 0;
    for (int i=0; i < num_parts-1; i++) {
        newstring_size += strlen(parts[i]) + 1;
    }

    char* newstring = malloc(newstring_size);
    strcpy(newstring, "/");
    for (int i=0; i < num_parts-1; i++) {
        strcat(newstring,parts[i]);
        if (i != num_parts-2) {
            strcat(newstring, "/");
        }
    }

    // get inode of directory
    int dir_num = 0;
    if(strcmp(newstring, "/")) {
        dir_num = get_inode_by_path(fs, newstring);
    }
    else {
        dir_num = EXT2_ROOT_INO;
    }

    // create dentry
    if (dir_num) {
        dentry_remove(fs, dir_num, inum);
    }
    else {
        DEBUG("Bad dir path");
        return 0;
    }

    return 1;
}
Exemplo n.º 2
0
int main(int argc, char ** argv) {
    // Extract the file given in the second argument from the filesystem image
    // given in the first.
    if (argc != 3) {
        printf("usage: ext2cat <filesystem_image> <path_within_filesystem>\n");
        exit(1);
    }
    char * fs_path = argv[1];
    char * file_path = argv[2];
    void * fs = mmap_fs(fs_path);

    // Get the inode for our target file.
    __u32 target_ino_num = get_inode_by_path(fs, file_path);
    if (target_ino_num == 0) {
        printf("cat: %s does not exist\n", file_path);
        return 1;
    }
    struct ext2_inode * target_ino = get_inode(fs, target_ino_num);

    __u32 block_size = get_block_size(fs);
    __u32 size = target_ino->i_size;
    __u32 bytes_read = 0;
    void * buf = calloc(size, 1);

    // Read the file one block at a time. In the real world, there would be a
    // lot of error-handling code here.
    __u32 bytes_left;
    for (int i = 0; i < EXT2_NDIR_BLOCKS; i++) {
        bytes_left = size - bytes_read;
        if (bytes_left == 0) break;
        __u32 bytes_to_read = bytes_left > block_size ? block_size : bytes_left;
        void * block = get_block(fs, target_ino->i_block[i]);
        memcpy(buf + bytes_read, block, bytes_to_read);
        bytes_read += bytes_to_read;
    }

    if (bytes_read < size) {
        /*printf("%s: file uses indirect blocks. output was truncated!\n",
               argv[0]);*/

        /* Get the number of the block that stores the array of indirect block numbers */
        __u32 * indir_block = get_block(fs, target_ino->i_block[EXT2_NDIR_BLOCKS]);

        /* Calculate the limit of the number of the indirect blocks*/
        int limit = block_size / sizeof(__u32);

        for (int i = 0; i < limit; i++) {
            bytes_left = size - bytes_read;
            if (bytes_left == 0) break;
            __u32 bytes_to_read = bytes_left > block_size ? block_size : bytes_left;
            void * block = get_block(fs, indir_block[i]);
            memcpy(buf + bytes_read, block, bytes_to_read);
            bytes_read += bytes_to_read;
        }
    }

    write(1, buf, bytes_read);

    return 0;
}
Exemplo n.º 3
0
// return new inode number; 0 if failed
uint32_t ext2_create_file(uint8_t *fs, char *path) {
    uint32_t free_inode = alloc_inode(fs);
    DEBUG("EXT2 CREATE: Allocated node %d", free_inode);

    if (free_inode) {
        struct ext2_inode *inode_pointer = get_inode(fs, free_inode);

        // get directory path
        int num_parts = 0;
        char **parts = split_path(path, &num_parts);
        if (!num_parts) {
            return 0;
        }
        int newstring_size = 0;
        for (int i=0; i < num_parts-1; i++) {
            newstring_size += strlen(parts[i]) + 1;
        }
        char *newstring = malloc(newstring_size);
        strcpy(newstring, "/");
        for (int i=0; i < num_parts-1; i++) {
            strcat(newstring,parts[i]);
            if (i != num_parts-2) {
                strcat(newstring, "/");
            }
        }

        // get inode of directory
        char *name = parts[num_parts-1];
        int dir_num = 0;
        if(strcmp(newstring, "/")) {
            dir_num = get_inode_by_path(fs, newstring);
        }
        else {
            dir_num = EXT2_ROOT_INO;
        }

        // create dentry
        if (dir_num) {
            dentry_add(fs, dir_num, free_inode, name, 1);
            //fill in inode with stuff
            inode_pointer->i_mode = EXT2_S_IFREG;
            inode_pointer->i_size = 0;
            //set bitmap to taken
        }
        else {
            return 0;
        }
    }
    return free_inode;
}
Exemplo n.º 4
0
int ext2_exists(uint8_t *fs, char *path) {
    uint32_t inode_num = (uint32_t)get_inode_by_path(fs, path);
    return inode_num != 0;
}
Exemplo n.º 5
0
uint32_t ext2_open(uint8_t *fs, char *path, int access) {
    uint32_t inode_num = (uint32_t) get_inode_by_path(fs, path);
    return inode_num;
}