Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| SHA1 Hash: | af4c0c30da96e943fd1f901ff2feea58685814ad |
|---|---|
| Date: | 2009-06-15 20:28:43 |
| User: | stephan |
| Comment: | added some internal docs. renamed a func. |
Tags And Properties
- branch=trunk inherited from [d21fde6e87]
- sym-trunk inherited from [d21fde6e87]
Changes
Changes to ls.c
| Old (cb6f702be7ef4c61) | New (e7544fa8f0e18ecd) | |||
|---|---|---|---|---|
| 1 | /* | 1 | /* | |
| 2 | Author: Stephan Beal (http://wanderinghorse.net/home/stephan/) | 2 | Author: Stephan Beal (http://wanderinghorse.net/home/stephan/) | |
| 3 | 3 | |||
| 4 | License: Public Domain | 4 | License: Public Domain | |
| > | 5 | |||
| > | 6 | This file implements an 'ls'-like took for whefs EFS containers. | ||
| > | 7 | |||
| > | 8 | Bugs/misfeatures: | ||
| > | 9 | |||
| > | 10 | - Due to how it iterates over inodes, if a node matches more than | ||
| > | 11 | one of the names/wildcards provided on the command line, ls will | ||
| > | 12 | only list the file once. This is arguably a good thing, but was | ||
| > | 13 | unintentional and does hinder some of my caching tests :/. | ||
| > | 14 | |||
| 5 | */ | 15 | */ | |
| 6 | 16 | |||
| 7 | #include "WHEFSApp.c" | 17 | #include "WHEFSApp.c" | |
| 8 | #include "whefs_string.h" | 18 | #include "whefs_string.h" | |
| 9 | 19 | |||
| 11 | typedef int (*ls_command_f)(); | 21 | typedef int (*ls_command_f)(); | |
| 12 | static struct ThisApp_ | 22 | static struct ThisApp_ | |
| 13 | { | 23 | { | |
| 14 | struct Funcs | 24 | struct Funcs | |
| 15 | { | 25 | { | |
| > | 26 | /* implementation funcs for specific --flags */ | ||
| 16 | ls_command_f f[ThisApp_MAX_COMMANDS]; | 27 | ls_command_f f[ThisApp_MAX_COMMANDS]; | |
| 17 | size_t count; | 28 | size_t count; | |
| 18 | } funcs; | 29 | } funcs; | |
| 19 | bool didSomething; | 30 | bool didSomething; | |
| 20 | } ThisApp = | 31 | } ThisApp = | |
| 3 hidden lines | ||||
| 24 | 0 /*count*/ | 35 | 0 /*count*/ | |
| 25 | }, | 36 | }, | |
| 26 | false /* didSomething */ | 37 | false /* didSomething */ | |
| 27 | }; | 38 | }; | |
| 28 | 39 | |||
| > | 40 | /** | ||
| > | 41 | Appends f to ThisApp.funcs. | ||
| > | 42 | */ | ||
| 29 | static int ThisApp_push_command( ls_command_f f ) | 43 | static int ThisApp_push_command( ls_command_f f ) | |
| 30 | { | 44 | { | |
| 31 | assert( ThisApp.funcs.count < ThisApp_MAX_COMMANDS ); | 45 | assert( ThisApp.funcs.count < ThisApp_MAX_COMMANDS ); | |
| 32 | ThisApp.funcs.f[ThisApp.funcs.count++] = f; | 46 | ThisApp.funcs.f[ThisApp.funcs.count++] = f; | |
| 33 | return 0; | 47 | return 0; | |
| 34 | } | 48 | } | |
| 35 | 49 | |||
| 36 | 50 | |||
| 37 | | | 51 | /** | |
| | | 52 | Internal data for use with whefs_inode_foreach(). | ||
| | | 53 | */ | ||
| 38 | struct ls_foreach_info | 54 | struct ls_foreach_info | |
| 39 | { | 55 | { | |
| > | 56 | /** Total byte size matched so far. */ | ||
| 40 | uint64_t totalSize; | 57 | uint64_t totalSize; | |
| > | 58 | /** Number of inodes checks. */ | ||
| 41 | whefs_id_type checkedInodesCount; | 59 | whefs_id_type checkedInodesCount; | |
| > | 60 | /** Number of inodes which matched a pattern. */ | ||
| 42 | whefs_id_type matchCount; | 61 | whefs_id_type matchCount; | |
| > | 62 | /** | ||
| > | 63 | Buffer for passing the inode name around. | ||
| > | 64 | */ | ||
| 43 | whefs_string name; | 65 | whefs_string name; | |
| 44 | }; | 66 | }; | |
| 45 | typedef struct ls_foreach_info ls_foreach_info; | 67 | typedef struct ls_foreach_info ls_foreach_info; | |
| 46 | static ls_foreach_info ls_foreach_info_init = { | 68 | static ls_foreach_info ls_foreach_info_init = { | |
| 47 | 0U /*totalSize*/, | 69 | 0U /*totalSize*/, | |
| 48 | 0U /*checkedInodesCount*/, | 70 | 0U /*checkedInodesCount*/, | |
| 49 | 0U /*matchCount*/, | 71 | 0U /*matchCount*/, | |
| 50 | whefs_string_init_m /*name*/ | 72 | whefs_string_init_m /*name*/ | |
| 51 | }; | 73 | }; | |
| 52 | 74 | |||
| 53 | bool ls_inode_predicate_used( whefs_fs * fs, whefs_inode const * n, void * clientData ) | | | 75 | static bool ls_inode_predicate_name_matches( whefs_fs * fs, whefs_inode const * n, void * clientData ) |
| 54 | { | 76 | { | |
| > | 77 | if(0) ls_inode_predicate_name_matches(0,0,0); // work around "static function defined but not used. | ||
| 55 | ls_foreach_info * info = (ls_foreach_info*)clientData; | 78 | ls_foreach_info * info = (ls_foreach_info*)clientData; | |
| 56 | ++(info->checkedInodesCount); | 79 | ++(info->checkedInodesCount); | |
| 57 | if(! (n->flags & WHEFS_FLAG_Used)) return false; | 80 | if(! (n->flags & WHEFS_FLAG_Used)) return false; | |
| 58 | int rc = whefs_inode_name_get( fs, n->id, &info->name ); | 81 | int rc = whefs_inode_name_get( fs, n->id, &info->name ); | |
| 59 | if( whefs_rc.OK != rc ) | 82 | if( whefs_rc.OK != rc ) | |
| 11 hidden lines | ||||
| 71 | { | 94 | { | |
| 72 | return false; | 95 | return false; | |
| 73 | } | 96 | } | |
| 74 | } | 97 | } | |
| 75 | 98 | |||
| 76 | int ls_inode_foreach_print( whefs_fs * fs, whefs_inode const * ino, void * clientData ) | | | 99 | static int ls_inode_foreach_print( whefs_fs * fs, whefs_inode const * ino, void * clientData ) |
| 77 | { | 100 | { | |
| 78 | ls_foreach_info * info = (ls_foreach_info*)clientData; | 101 | ls_foreach_info * info = (ls_foreach_info*)clientData; | |
| 79 | ++(info->matchCount); | 102 | ++(info->matchCount); | |
| 80 | printf("%-16"WHEFS_ID_TYPE_PFMT"%-16"WHEFS_ID_TYPE_PFMT"%-12u%-16u%s\n", | 103 | printf("%-16"WHEFS_ID_TYPE_PFMT"%-16"WHEFS_ID_TYPE_PFMT"%-12u%-16u%s\n", | |
| 81 | ino->id, | 104 | ino->id, | |
| 14 hidden lines | ||||
| 96 | int rc = 0; | 119 | int rc = 0; | |
| 97 | puts("List of file entries:\n"); | 120 | puts("List of file entries:\n"); | |
| 98 | printf("%-16s%-16s%-12s%-16s%-16s\n", | 121 | printf("%-16s%-16s%-12s%-16s%-16s\n", | |
| 99 | "Node ID:","First block:", "Size:", "Timestamp:","Name:" ); | 122 | "Node ID:","First block:", "Size:", "Timestamp:","Name:" ); | |
| 100 | ls_foreach_info foi = ls_foreach_info_init; | 123 | ls_foreach_info foi = ls_foreach_info_init; | |
| 101 | rc = whefs_inode_foreach( fs, ls_inode_predicate_used, &foi, ls_inode_foreach_print, &foi ); | | | 124 | rc = whefs_inode_foreach( fs, ls_inode_predicate_name_matches, &foi, ls_inode_foreach_print, &foi ); |
| 102 | whefs_string_clear( &foi.name, false ); | 125 | whefs_string_clear( &foi.name, false ); | |
| 103 | //MARKER("foreach rc=%d, predicate checks=%"WHEFS_ID_TYPE_PFMT" \n",rc, foi.checkedInodesCount); | 126 | //MARKER("foreach rc=%d, predicate checks=%"WHEFS_ID_TYPE_PFMT" \n",rc, foi.checkedInodesCount); | |
| 104 | printf("%31s %"PRIu64" bytes\n", "Total:", foi.totalSize ); | 127 | printf("%31s %"PRIu64" bytes\n", "Total:", foi.totalSize ); | |
| 105 | printf("%"WHEFS_ID_TYPE_PFMT" of %"WHEFS_ID_TYPE_PFMT" total inodes listed.\n", | 128 | printf("%"WHEFS_ID_TYPE_PFMT" of %"WHEFS_ID_TYPE_PFMT" total inodes listed.\n", | |
| 106 | foi.matchCount, | 129 | foi.matchCount, | |
| 246 hidden lines | ||||
| 353 | } | 376 | } | |
| 354 | } | 377 | } | |
| 355 | #undef CHECKRC | 378 | #undef CHECKRC | |
| 356 | return rc; | 379 | return rc; | |
| 357 | } | 380 | } | |