fileio.c

来自「开放源码实时操作系统源码.」· C语言 代码 · 共 767 行 · 第 1/2 页

C
767
字号
         return;
     }
     
    if (!scan_opts(argc, argv, 1, NULL, 0, &dir_str, OPTION_ARG_TYPE_STR, "directory") ||
        dir_str == NULL)
    {
        fs_usage("invalid arguments");
        return;
    }

    err = mkdir( dir_str, 0 );

    if( err != 0 )
        err_printf("fs mkdir: failed to create directory %s\n",dir_str);
}

local_cmd_entry("mkdir", 
                "create directory",
                "<directory>",
                do_mkdir,
                FS_cmds
    );

//==========================================================================

static void 
do_deldir(int argc, char * argv[])
{
    char *dir_str;
    int err;
    
     if( mount_count == 0 )
     {
         err_printf("fs: No filesystems mounted\n");
         return;
     }
     
    if (!scan_opts(argc, argv, 1, NULL, 0, &dir_str, OPTION_ARG_TYPE_STR, "directory") ||
        dir_str == NULL)
    {
        fs_usage("invalid arguments");
        return;
    }

    err = rmdir( dir_str );

    if( err != 0 )
        err_printf("fs deldir: failed to remove directory %s\n",dir_str);
}

local_cmd_entry("deldir", 
                "delete directory",
                "<directory>",
                do_deldir,
                FS_cmds
    );

//==========================================================================

static void 
do_del(int argc, char * argv[])
{
    char *name_str = NULL;
    int err;
    
     if( mount_count == 0 )
     {
         err_printf("fs: No filesystems mounted\n");
         return;
     }
     
    if (!scan_opts(argc, argv, 1, NULL, 0, &name_str, OPTION_ARG_TYPE_STR, "file") ||
        name_str == NULL)
    {
        fs_usage("invalid arguments");
        return;
    }

    err = unlink( name_str );

    if( err != 0 )
        err_printf("fs del: failed to delete file %s\n",name_str);
}

local_cmd_entry("del", 
                "delete file",
                "<file>",
                do_del,
                FS_cmds
    );

//==========================================================================

static void 
do_move(int argc, char * argv[])
{
    int err;
    __externC int rename( const char *oldname, const char *newname );
    if( mount_count == 0 )
    {
        err_printf("fs: No filesystems mounted\n");
        return;
    }

    if( argc != 3 )
        fs_usage("bad arguments to move command\n");

    err = rename( argv[1], argv[2] );

    if( err != 0 )
        err_printf("fs move: failed to move file %s to %s\n",argv[1],argv[2]);
}

local_cmd_entry("move", 
                "move file",
                "<from> <to>",
                do_move,
                FS_cmds
    );

//==========================================================================

static void 
do_cd(int argc, char * argv[])
{
    char *dir_str;
    int err;
    
     if( mount_count == 0 )
     {
         err_printf("fs: No filesystems mounted\n");
         return;
     }
     
    if (!scan_opts(argc, argv, 1, NULL, 0, &dir_str, OPTION_ARG_TYPE_STR, "directory"))
        return;

    if( dir_str == NULL )
        dir_str = "/";
    
    err = chdir( dir_str );

    if( err != 0 )
        err_printf("fs cd: failed to change directory %s\n",dir_str);
}

local_cmd_entry("cd", 
                "change directory",
                "[<directory>]",
                do_cd,
                FS_cmds
    );

//==========================================================================

static void 
do_write(int argc, char * argv[])
{
    char *name_str = NULL;
    int err;
    struct option_info opts[2];    
    CYG_ADDRESS mem_addr = 0;
    unsigned long length = 0;
    bool mem_addr_set = false;
    bool length_set = false;
    int fd;
    
     if( mount_count == 0 )
     {
         err_printf("fs: No filesystems mounted\n");
         return;
     }

     init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM, 
               (void *)&mem_addr, (bool *)&mem_addr_set, "memory base address");
     init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM, 
               (void *)&length, (bool *)&length_set, "image length");
     
    if (!scan_opts(argc, argv, 1, opts, 2, &name_str, OPTION_ARG_TYPE_STR, "file name") ||
        name_str == NULL)
    {
        fs_usage("invalid arguments");
        return;
    }

//    diag_printf("load_address %08x %08x\n",load_address,load_address_end);
//    diag_printf("ram %08x %08x\n",ram_start, ram_end);
//    diag_printf("file name %08x >%s<\n",name_str,name_str);
    
    if (!mem_addr_set &&
        (load_address >= (CYG_ADDRESS)ram_start) &&
	((load_address_end) < (CYG_ADDRESS)ram_end))
    {
	mem_addr = load_address;
	mem_addr_set = true;
	if (!length_set)
        {
	    length = load_address_end - load_address;
	    length_set = true;
            // maybe get length from existing file size if no loaded
            // image?
        }
    }
    
    fd = open( name_str, O_WRONLY|O_CREAT|O_TRUNC );

    if( fd < 0 )
    {
        err_printf("fs write: Cannot open %s\n", name_str );
        return;
    }

//    diag_printf("write %08x %08x\n",mem_addr, length );
    
    err = write( fd, (void *)mem_addr, length );

    if( err != length )
    {
        err_printf("fs write: failed to write to file %d(%d) %d\n",err,length,errno);
    }

    err = close( fd );

    if( err != 0 )
        err_printf("fs write: close failed\n");
}

local_cmd_entry("write",
                "write data to file",
                "-b <mem_base> -l <image_length> <file_name>",
                do_write,
                FS_cmds
    );

//==========================================================================

__externC cyg_fstab_entry cyg_fstab[];
__externC cyg_fstab_entry cyg_fstab_end;
__externC cyg_mtab_entry cyg_mtab[];
__externC cyg_mtab_entry cyg_mtab_end;

static void 
do_info(int argc, char * argv[])
{
    cyg_bool found = false;
    cyg_fstab_entry *f;
    cyg_devtab_entry_t *t;

    for( f = &cyg_fstab[0] ; f != &cyg_fstab_end; f++ )
    {
        if( !found )
        {
            diag_printf("Filesystems available:\n");
            found = true;
        }
        diag_printf("%s\n",f->name);
    }

    found = false;
    for (t = &__DEVTAB__[0]; t != &__DEVTAB_END__; t++)
    {
        if( (t->status & CYG_DEVTAB_STATUS_BLOCK) == 0 ||
            (t->status & CYG_DEVTAB_STATUS_AVAIL) == 0 )
            continue;
        
        if( !found )
        {
            diag_printf("\nDevices available:\n");
            found = true;
        }
        diag_printf("%s\n",t->name);
    }

    if( mount_count != 0 )
    {
        int i;

        diag_printf("\nMounted filesystems:\n");
        diag_printf("            Device               Filesystem Mounted on\n");

        for( i = 0; i < MAX_MOUNTS; i++ )
        {
            if( mounts[i].mp_str[0] != '\0' )
                diag_printf("%32s %10s %s\n", mounts[i].dev_str, mounts[i].type_str, mounts[i].mp_str);
        }
    }
}

local_cmd_entry("info", 
                "filesystem info",
                "",
                do_info,
                FS_cmds
    );

//==========================================================================

static void
do_fs(int argc, char *argv[])
{
    struct cmd *cmd;

    if (argc < 2) {
        fs_usage("too few arguments");
        return;
    }
    if ((cmd = cmd_search(__FS_cmds_TAB__, &__FS_cmds_TAB_END__, 
                          argv[1])) != (struct cmd *)0) {
        (cmd->fun)(argc-1, argv+1);
        return;
    }
    fs_usage("unrecognized command");
}

RedBoot_nested_cmd("fs", 
            "Manage Filesystem files", 
            "{cmds}",
            do_fs,
            __FS_cmds_TAB__, &__FS_cmds_TAB_END__
    );


//==========================================================================

static int fd;

externC int 
fileio_stream_open(connection_info_t *info, int *err)
{
    char *filename = info->filename;

     if( mount_count == 0 )
     {
         diag_printf("fs: No filesystems mounted\n");
         return -1;
     }
    
    fd = open(filename, O_RDONLY);
    if (fd < 0) {
        diag_printf("fs: Open failed, error %d\n", errno);
        return -1;
    }
    return 0;
}

externC int 
fileio_stream_read(char *buf, int size, int *err)
{
    int nread;

    if ((nread = read(fd, buf, size)) < 0) {
        *err = errno;
        return -1;
    }
    return nread;
}

externC void
fileio_stream_close(int *err)
{
    close(fd);
}

externC char *
fileio_error(int err)
{
    static char myerr[10];

    diag_sprintf(myerr, "error %d", err);
    return myerr;
}

//
// RedBoot interface
//
GETC_IO_FUNCS(fileio_io, fileio_stream_open, fileio_stream_close,
              0, fileio_stream_read, fileio_error);
RedBoot_load(file, fileio_io, true, true, 0);

//==========================================================================
// End of fileio.c

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?