如何使用VTreeFS在错误安装时返回干净错误?

时间:2012-01-17 22:07:58

标签: c minix

当尝试使用一组参数挂载VTreeFS文件系统时(通过在安装时使用选项-o),如果用户没有正确使用预定义的参数,我们希望让它干净​​地失败。当我们不挂载文件系统并让main返回0时,我们得到这个讨厌的错误消息。如果参数不正确,我们基本上希望不挂载文件系统

当前情况

  

mount -t filesystemtest -o testarguments none / mnt / filesystemtest

     

参数无效

     

RS:服务'fs_00021'退出uring初始化

     

filesystemtest 109710 0xab6e 0x65f1 0x618d 0x6203 0x98ba 0x1010

     

请求RS失败:未知错误(错误302)

     

mount:无法运行/ bin / sercie up / sbin / filesystemtest -label'fs_00021'-args''

     

mount:无法挂载/ mnt / filesystemtest /:未知错误

首选情况

  

mount -t filesystemtest -o testarguments none / mnt / filesystemtest

     

参数无效

基本上我们不知道如何在不调用start_vtreefs时返回干净的错误消息。下面的例子不是我们的实际代码,实际上并没有使用参数,但作为一个例子,应该有一种方法让这段代码总是失败。 (对不起):

#include <minix/drivers.h>
#include <minix/vtreefs.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>

static void my_init_hook(void)
{       
        /* This hook will be called once, after VTreeFS has initialized.
         */
        struct inode_stat file_stat;
        struct inode *inode;

        /* We create one regular file in the root directory. The file is
         * readable by everyone, and owned by root. Its size as returned by for
         * example stat() will be zero, but that does not mean it is empty.
         * For files with dynamically generated content, the file size is
         * typically set to zero.
         */
        file_stat.mode = S_IFREG | 0444;
        file_stat.uid = 0;
        file_stat.gid = 0;
        file_stat.size = 0;
        file_stat.dev = NO_DEV;

        /* Now create the actual file. It is called "test" and does not have an
         * index number. Its callback data value is set to 1, allowing it to be
         * identified with this number later.
         */
        inode = add_inode(get_root_inode(), "test", NO_INDEX, &file_stat, 0,
                (cbdata_t) 1);

        assert(inode != NULL);
}

static int my_read_hook(struct inode *inode, off_t offset, char **ptr,
        size_t *len, cbdata_t cbdata)
{
        /* This hook will be called every time a regular file is read. We use
         * it to dyanmically generate the contents of our file.
         */
        static char data[26];
        const char *str;
        time_t now;

        /* We have only a single file. With more files, cbdata may help
         * distinguishing between them.
         */
        assert((int) cbdata == 1);

        /* Generate the contents of the file into the 'data' buffer. We could
         * use the return value of ctime() directly, but that would make for a
         * lousy example.
         */
        time(&now);

        str = ctime(&now);

        strcpy(data, str);

        /* If the offset is beyond the end of the string, return EOF. */
        if (offset > strlen(data)) {
                *len = 0;

                return OK;
        }

        /* Otherwise, return a pointer into 'data'. If necessary, bound the
         * returned length to the length of the rest of the string. Note that
         * 'data' has to be static, because it will be used after this function
         * returns.
         */
        *ptr = data + offset;

        if (*len > strlen(data) - offset)
                *len = strlen(data) - offset;

        return OK;
}

/* The table with callback hooks. */
struct fs_hooks my_hooks = {
        my_init_hook,
        NULL, /* cleanup_hook */
        NULL, /* lookup_hook */
        NULL, /* getdents_hook */
        my_read_hook,
        NULL, /* rdlink_hook */
        NULL  /* message_hook */
};
int main(int argc, char* argv[])
{
    /* The call above never returns. This just keeps the compiler happy. */
    if (argc == 1) {
        // We want it to fail right now!!!!
        printf("Arguments invalid. (pass option with -o)");
    }
    else {
        struct inode_stat root_stat;

        /* Fill in the details to be used for the root inode. It will be a
         * directory, readable and searchable by anyone, and owned by root.
         */
        root_stat.mode = S_IFDIR | 0555;
        root_stat.uid = 0;
        root_stat.gid = 0;
        root_stat.size = 0;
        root_stat.dev = NO_DEV;

        /* Now start VTreeFS. Preallocate 10 inodes, which is more than we'll
         * need for this example. No indexed entries are used.
         */
        start_vtreefs(&my_hooks, 10, &root_stat, 0);
    }
    return 0;
}

0 个答案:

没有答案