Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a simple filesystem representation in C using a sort of bin

ID: 3664203 • Letter: I

Question

I'm trying to write a simple filesystem representation in C using a sort of binary tree only where the nodes can be either files or directories (in this case the fist index(zero) of the char * in the node struct would be "F" for file or "D" for directory, also it would be just "yourname" for your name and "root" for root. The filesystem already initializes the user (owner name) and root node (as in "yourname/root/", all of the same node type). The "yourname" node should not be accesed and no directories can be written unless under "root" (files cant be written in root directory, so can directories, but you may not have a directory that is "yourname/something/") . I have a struct with three pointers, One is left, one is right, and one is previous. Included in the struct is a char * for a file or directory name as described above. I'm having difficulty being able to simulate essentially shell commands (which is the goal). These commands would be cd, cd .., ls, mkdir <filename>, del <file/dir name>, find <filename>, etc. Any help on being able to implement this design in C would be greatly appreciated.

Explanation / Answer

This is the rough User defined implementation of all the commands

int bb_mknod(const char *path, mode_t mode, dev_t dev)
{
    int retstat = 0;
    char fpath[PATH_MAX];
   
    log_msg(" bb_mknod(path="%s", mode=0%3o, dev=%lld) ",path, mode, dev);
    bb_fullpath(fpath, path);
   
    // On Linux this could just be 'mknod(path, mode, rdev)' but this
    // is more portable
    if (S_ISREG(mode)) {
        retstat = open(fpath, O_CREAT | O_EXCL | O_WRONLY, mode);
    if (retstat < 0)
        retstat = bb_error("bb_mknod open");
        else {
            retstat = close(retstat);
        if (retstat < 0)
        retstat = bb_error("bb_mknod close");
    }
    } else
    if (S_ISFIFO(mode)) {
        retstat = mkfifo(fpath, mode);
        if (retstat < 0)
        retstat = bb_error("bb_mknod mkfifo");
    } else {
        retstat = mknod(fpath, mode, dev);
        if (retstat < 0)
        retstat = bb_error("bb_mknod mknod");
    }
   
    return retstat;
}

/** Create a directory */
int bb_mkdir(const char *path, mode_t mode)
{
    int retstat = 0;
    char fpath[PATH_MAX];
   
    log_msg(" bb_mkdir(path="%s", mode=0%3o) ",path, mode);
    bb_fullpath(fpath, path);   
    retstat = mkdir(fpath, mode);
    if (retstat < 0)
    retstat = bb_error("bb_mkdir mkdir");   
    return retstat;
}

/** Remove a file */
int bb_unlink(const char *path)
{
    int retstat = 0;
    char fpath[PATH_MAX];
   
    log_msg("bb_unlink(path="%s") ",path);
    bb_fullpath(fpath, path);
   
    retstat = unlink(fpath);
    if (retstat < 0)
    retstat = bb_error("bb_unlink unlink");
   
    return retstat;
}

/** Remove a directory */
int bb_rmdir(const char *path)
{
    int retstat = 0;
    char fpath[PATH_MAX];
   
    log_msg("bb_rmdir(path="%s") ",path);
    bb_fullpath(fpath, path);
   
    retstat = rmdir(fpath);
    if (retstat < 0)
    retstat = bb_error("bb_rmdir rmdir");
   
    return retstat;
}