1 // lsindex - for Haiku 2 // 3 // authors, in order of contribution: 4 // jonas.sundstrom@kirilla.com 5 // revol@free.fr 6 // axeld@pinc-software.de 7 // 8 9 #include <TypeConstants.h> 10 #include <fs_info.h> 11 #include <fs_index.h> 12 13 #include <stdio.h> 14 #include <string.h> 15 #include <errno.h> 16 17 18 static void 19 print_help(void) 20 { 21 fprintf (stderr, 22 "Usage: lsindex [--help | -v | --verbose | --mkindex | -l | --long] [volume path]\n" 23 " -l --long\t outputs long listing\n" 24 " -v --verbose\t gives index type, dates and owner\n" 25 " --mkindex\t outputs mkindex commands to recreate all the indices\n" 26 " --help\t prints out this text\n\n" 27 " If no volume is specified, the volume of the current directory is assumed.\n"); 28 } 29 30 31 static const char * 32 print_index_type(const index_info &info, bool mkindexOutput) 33 { 34 static char buffer[30]; 35 36 switch (info.type) { 37 case B_INT32_TYPE: 38 return mkindexOutput ? "int" : "Int-32"; 39 case B_INT64_TYPE: 40 return mkindexOutput ? "llong" : "Int-64"; 41 case B_STRING_TYPE: 42 return mkindexOutput ? "string" : "Text"; 43 case B_FLOAT_TYPE: 44 return mkindexOutput ? "float" : "Float"; 45 case B_DOUBLE_TYPE: 46 return mkindexOutput ? "double" : "Double"; 47 48 default: 49 sprintf(buffer, mkindexOutput ? "0x%08lx" : "Unknown type (0x%x)", info.type); 50 return buffer; 51 } 52 } 53 54 55 static const char * 56 type_string(type_code type) 57 { 58 // all types from <TypeConstants.h> listed for completeness, 59 // even though they don't all apply to attribute indices 60 61 #define RETURN_TYPE(x) case x: return #x 62 63 switch (type) { 64 RETURN_TYPE(B_ANY_TYPE); 65 RETURN_TYPE(B_BOOL_TYPE); 66 RETURN_TYPE(B_CHAR_TYPE); 67 RETURN_TYPE(B_COLOR_8_BIT_TYPE); 68 RETURN_TYPE(B_DOUBLE_TYPE); 69 RETURN_TYPE(B_FLOAT_TYPE); 70 RETURN_TYPE(B_GRAYSCALE_8_BIT_TYPE); 71 RETURN_TYPE(B_INT64_TYPE); 72 RETURN_TYPE(B_INT32_TYPE); 73 RETURN_TYPE(B_INT16_TYPE); 74 RETURN_TYPE(B_INT8_TYPE); 75 RETURN_TYPE(B_MESSAGE_TYPE); 76 RETURN_TYPE(B_MESSENGER_TYPE); 77 RETURN_TYPE(B_MIME_TYPE); 78 RETURN_TYPE(B_MONOCHROME_1_BIT_TYPE); 79 RETURN_TYPE(B_OBJECT_TYPE); 80 RETURN_TYPE(B_OFF_T_TYPE); 81 RETURN_TYPE(B_PATTERN_TYPE); 82 RETURN_TYPE(B_POINTER_TYPE); 83 RETURN_TYPE(B_POINT_TYPE); 84 RETURN_TYPE(B_RAW_TYPE); 85 RETURN_TYPE(B_RECT_TYPE); 86 RETURN_TYPE(B_REF_TYPE); 87 RETURN_TYPE(B_RGB_32_BIT_TYPE); 88 RETURN_TYPE(B_RGB_COLOR_TYPE); 89 RETURN_TYPE(B_SIZE_T_TYPE); 90 RETURN_TYPE(B_SSIZE_T_TYPE); 91 RETURN_TYPE(B_STRING_TYPE); 92 RETURN_TYPE(B_TIME_TYPE); 93 RETURN_TYPE(B_UINT64_TYPE); 94 RETURN_TYPE(B_UINT32_TYPE); 95 RETURN_TYPE(B_UINT16_TYPE); 96 RETURN_TYPE(B_UINT8_TYPE); 97 RETURN_TYPE(B_MEDIA_PARAMETER_TYPE); 98 RETURN_TYPE(B_MEDIA_PARAMETER_WEB_TYPE); 99 RETURN_TYPE(B_MEDIA_PARAMETER_GROUP_TYPE); 100 RETURN_TYPE(B_ASCII_TYPE); 101 102 default: 103 return NULL; 104 } 105 #undef RETURN_TYPE 106 } 107 108 109 static void 110 print_index_long_stat(const index_info &info, char *name) 111 { 112 char modified[30]; 113 strftime(modified, 30, "%m/%d/%Y %I:%M %p", localtime(&info.modification_time)); 114 printf("%16s %s %8Ld %s\n", print_index_type(info, false), modified, info.size, name); 115 } 116 117 118 static void 119 print_index_verbose_stat(const index_info &info, char *name) 120 { 121 printf("%-18s\t", name); 122 123 // Type 124 const char *typeString = type_string(info.type); 125 if (typeString != NULL) 126 printf("%-10s\t", typeString); 127 else 128 printf("%ld\t", info.type); 129 130 // Size 131 printf("%10Ld ", info.size); 132 133 // Created 134 char string[30]; 135 strftime(string, sizeof(string), "%Y-%m-%d %H:%M", localtime(&info.creation_time)); 136 printf("%s ", string); 137 138 // Modified 139 strftime(string, sizeof(string), "%Y-%m-%d %H:%M", localtime(&info.modification_time)); 140 printf("%s", string); 141 142 // User 143 printf("%5d", info.uid); 144 145 // Group 146 printf("%5d\n", info.gid); 147 } 148 149 150 int 151 main(int argc, char **argv) 152 { 153 dev_t device = dev_for_path("."); 154 DIR *indices = NULL; 155 bool verbose = false; 156 bool longListing = false; 157 bool mkindexOutput = false; /* mkindex-ready output */ 158 159 for (int i = 1; i < argc; i++) { 160 if (argv[i][0] == '-') { 161 if (!strcmp(argv[i], "--help")) { 162 print_help(); 163 return 0; 164 } else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) 165 verbose = true; 166 else if (!strcmp(argv[i], "--long") || !strcmp(argv[i], "-l")) 167 longListing = true; 168 else if (!strcmp(argv[i], "--mkindex")) 169 mkindexOutput = true; 170 else { 171 fprintf(stderr, "%s: option %s is not understood (use --help for help)\n", argv[0], argv[i]); 172 return -1; 173 } 174 } else { 175 device = dev_for_path(argv[i]); 176 if (device < 0) { 177 fprintf(stderr, "%s: can't get information about volume: %s\n", argv[0], argv[i]); 178 return -1; 179 } 180 } 181 } 182 183 indices = fs_open_index_dir(device); 184 if (indices == NULL) { 185 fprintf(stderr, "%s: can't open index dir of device %ld\n", argv[0], device); 186 return -1; 187 } 188 189 if (verbose) { 190 printf(" Name Type Size Created Modified User Group\n"); 191 printf("********************************************************\n"); 192 } 193 194 while (1) { 195 dirent *index = fs_read_index_dir(indices); 196 if (index == NULL) { 197 if (errno != B_ENTRY_NOT_FOUND && errno != B_OK) { 198 printf("%s: fs_read_index_dir: (%d) %s\n", argv[0], errno, strerror(errno)); 199 return errno; 200 } 201 break; 202 } 203 204 if (verbose || longListing || mkindexOutput) { 205 index_info info; 206 207 if (fs_stat_index(device, index->d_name, &info) != B_OK) { 208 printf("%s: fs_stat_index(): (%d) %s\n", argv[0], errno, strerror(errno)); 209 return errno; 210 } 211 212 if (verbose) 213 print_index_verbose_stat(info, index->d_name); 214 else if (longListing) 215 print_index_long_stat(info, index->d_name); 216 else /* mkindexOutput */ 217 printf("mkindex -t %s '%s'\n", print_index_type(info, true), index->d_name); 218 } else 219 printf("%s\n", index->d_name); 220 } 221 222 fs_close_index_dir(indices); 223 return 0; 224 } 225 226