Ignore:
Timestamp:
2009-06-28T21:41:13Z (15 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bfd247f
Parents:
75160a6
Message:

Implement stat() and VFS_IN_STAT.
Modify bdsh() to use stat() during ls.
In devfs, allow lookups that don't
specify one of L_FILE and L_DIRECTORY.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/ls/ls.c

    r75160a6 r415c7e0d  
    5151static char *cmdname = "ls";
    5252
    53 static inline off_t flen(const char *f)
    54 {
    55         int fd;
    56         off_t size;
    57 
    58         fd = open(f, O_RDONLY);
    59         if (fd == -1)
    60                 return 0;
    61 
    62         size = lseek(fd, 0, SEEK_END);
    63         close(fd);
    64 
    65         if (size < 0)
    66                 size = 0;
    67 
    68         return size;
    69 }
    70 
    71 static unsigned int ls_scope(const char *path)
    72 {
    73         int fd;
    74         DIR *dirp;
    75 
    76         dirp = opendir(path);
    77         if (dirp) {
    78                 closedir(dirp);
    79                 return LS_DIR;
    80         }
    81 
    82         fd = open(path, O_RDONLY);
    83         if (fd > 0) {
    84                 close(fd);
    85                 return LS_FILE;
    86         }
    87 
    88         return LS_BOGUS;
    89 }
    90 
    9153static void ls_scan_dir(const char *d, DIR *dirp)
    9254{
    9355        struct dirent *dp;
    94         unsigned int scope;
    9556        char *buff;
    9657
     
    10970                 * absolutize() later with subsequent calls to open() or readdir() */
    11071                snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name);
    111                 scope = ls_scope(buff);
    112                 switch (scope) {
    113                 case LS_DIR:
    114                         ls_print_dir(dp->d_name);
    115                         break;
    116                 case LS_FILE:
    117                         ls_print_file(dp->d_name, buff);
    118                         break;
    119                 case LS_BOGUS:
    120                         /* Odd chance it was deleted from the time readdir() found
    121                          * it and the time that it was scoped */
    122                         printf("ls: skipping bogus node %s\n", dp->d_name);
    123                         break;
    124                 }
     72                ls_print(dp->d_name, buff);
    12573        }
    12674
     
    13078}
    13179
    132 /* ls_print_* currently does nothing more than print the entry.
     80/* ls_print currently does nothing more than print the entry.
    13381 * in the future, we will likely pass the absolute path, and
    13482 * some sort of ls_options structure that controls how each
     
    13785 * Now we just print basic DOS style lists */
    13886
    139 static void ls_print_dir(const char *d)
     87static void ls_print(const char *name, const char *pathname)
    14088{
    141         printf("%-40s\t<dir>\n", d);
     89        struct stat s;
     90        int rc;
    14291
    143         return;
    144 }
    145 
    146 static void ls_print_file(const char *name, const char *pathname)
    147 {
    148         printf("%-40s\t%llu\n", name, (long long) flen(pathname));
     92        if (rc = stat(pathname, &s)) {
     93                /* Odd chance it was deleted from the time readdir() found it */
     94                printf("ls: skipping bogus node %s\n", pathname);
     95                printf("rc=%d\n", rc);
     96                return;
     97        }
     98       
     99        if (s.is_file)
     100                printf("%-40s\t%llu\n", name, (long long) s.size);
     101        else
     102                printf("%-40s\n", name);
    149103
    150104        return;
     
    167121{
    168122        unsigned int argc;
    169         unsigned int scope;
     123        struct stat s;
    170124        char *buff;
    171125        DIR *dirp;
     
    185139                str_cpy(buff, PATH_MAX, argv[1]);
    186140
    187         scope = ls_scope(buff);
    188 
    189         switch (scope) {
    190         case LS_BOGUS:
     141        if (stat(buff, &s)) {
    191142                cli_error(CL_ENOENT, buff);
    192143                free(buff);
    193144                return CMD_FAILURE;
    194         case LS_FILE:
    195                 ls_print_file(buff, buff);
    196                 break;
    197         case LS_DIR:
     145        }
     146
     147        if (s.is_file) {
     148                ls_print(buff, buff);
     149        } else {
    198150                dirp = opendir(buff);
    199                 if (! dirp) {
     151                if (!dirp) {
    200152                        /* May have been deleted between scoping it and opening it */
    201153                        cli_error(CL_EFAIL, "Could not stat %s", buff);
     
    205157                ls_scan_dir(buff, dirp);
    206158                closedir(dirp);
    207                 break;
    208159        }
    209160
Note: See TracChangeset for help on using the changeset viewer.