1*1487a242SAxel Dörfler /*
2*1487a242SAxel Dörfler * Copyright 2002-2020, Haiku, Inc. All Rights Reserved.
3*1487a242SAxel Dörfler * Distributed under the terms of the MIT License.
4*1487a242SAxel Dörfler *
5*1487a242SAxel Dörfler * Authors:
6*1487a242SAxel Dörfler * jonas.sundstrom@kirilla.com
7*1487a242SAxel Dörfler * revol@free.fr
8*1487a242SAxel Dörfler * Axel Dörfler, axeld@pinc-software.de
9*1487a242SAxel Dörfler */
10*1487a242SAxel Dörfler
1117049c45SAxel Dörfler
1217049c45SAxel Dörfler #include <TypeConstants.h>
1317049c45SAxel Dörfler #include <fs_info.h>
1417049c45SAxel Dörfler #include <fs_index.h>
1517049c45SAxel Dörfler
1617049c45SAxel Dörfler #include <stdio.h>
1717049c45SAxel Dörfler #include <string.h>
1817049c45SAxel Dörfler #include <errno.h>
1917049c45SAxel Dörfler
2017049c45SAxel Dörfler
2117049c45SAxel Dörfler static void
print_help(void)2217049c45SAxel Dörfler print_help(void)
2317049c45SAxel Dörfler {
2417049c45SAxel Dörfler fprintf (stderr,
2517049c45SAxel Dörfler "Usage: lsindex [--help | -v | --verbose | --mkindex | -l | --long] [volume path]\n"
2617049c45SAxel Dörfler " -l --long\t outputs long listing\n"
2717049c45SAxel Dörfler " -v --verbose\t gives index type, dates and owner\n"
2817049c45SAxel Dörfler " --mkindex\t outputs mkindex commands to recreate all the indices\n"
2917049c45SAxel Dörfler " --help\t prints out this text\n\n"
3017049c45SAxel Dörfler " If no volume is specified, the volume of the current directory is assumed.\n");
3117049c45SAxel Dörfler }
3217049c45SAxel Dörfler
3317049c45SAxel Dörfler
3417049c45SAxel Dörfler static const char *
print_index_type(const index_info & info,bool mkindexOutput)3517049c45SAxel Dörfler print_index_type(const index_info &info, bool mkindexOutput)
3617049c45SAxel Dörfler {
3717049c45SAxel Dörfler static char buffer[30];
3817049c45SAxel Dörfler
3917049c45SAxel Dörfler switch (info.type) {
4017049c45SAxel Dörfler case B_INT32_TYPE:
4117049c45SAxel Dörfler return mkindexOutput ? "int" : "Int-32";
4217049c45SAxel Dörfler case B_INT64_TYPE:
4317049c45SAxel Dörfler return mkindexOutput ? "llong" : "Int-64";
4417049c45SAxel Dörfler case B_STRING_TYPE:
4517049c45SAxel Dörfler return mkindexOutput ? "string" : "Text";
4617049c45SAxel Dörfler case B_FLOAT_TYPE:
4717049c45SAxel Dörfler return mkindexOutput ? "float" : "Float";
4817049c45SAxel Dörfler case B_DOUBLE_TYPE:
4917049c45SAxel Dörfler return mkindexOutput ? "double" : "Double";
5017049c45SAxel Dörfler
5117049c45SAxel Dörfler default:
5274b56a20SMurai Takashi sprintf(buffer, mkindexOutput
5374b56a20SMurai Takashi ? "0x%08" B_PRIx32
5474b56a20SMurai Takashi : "Unknown type (0x%" B_PRIx32 ")",
5574b56a20SMurai Takashi info.type);
5617049c45SAxel Dörfler return buffer;
5717049c45SAxel Dörfler }
5817049c45SAxel Dörfler }
5917049c45SAxel Dörfler
6017049c45SAxel Dörfler
6117049c45SAxel Dörfler static const char *
type_string(type_code type)6217049c45SAxel Dörfler type_string(type_code type)
6317049c45SAxel Dörfler {
6417049c45SAxel Dörfler // all types from <TypeConstants.h> listed for completeness,
6517049c45SAxel Dörfler // even though they don't all apply to attribute indices
6617049c45SAxel Dörfler
6717049c45SAxel Dörfler #define RETURN_TYPE(x) case x: return #x
6817049c45SAxel Dörfler
6917049c45SAxel Dörfler switch (type) {
7017049c45SAxel Dörfler RETURN_TYPE(B_ANY_TYPE);
7117049c45SAxel Dörfler RETURN_TYPE(B_BOOL_TYPE);
7217049c45SAxel Dörfler RETURN_TYPE(B_CHAR_TYPE);
7317049c45SAxel Dörfler RETURN_TYPE(B_COLOR_8_BIT_TYPE);
7417049c45SAxel Dörfler RETURN_TYPE(B_DOUBLE_TYPE);
7517049c45SAxel Dörfler RETURN_TYPE(B_FLOAT_TYPE);
7617049c45SAxel Dörfler RETURN_TYPE(B_GRAYSCALE_8_BIT_TYPE);
7717049c45SAxel Dörfler RETURN_TYPE(B_INT64_TYPE);
7817049c45SAxel Dörfler RETURN_TYPE(B_INT32_TYPE);
7917049c45SAxel Dörfler RETURN_TYPE(B_INT16_TYPE);
8017049c45SAxel Dörfler RETURN_TYPE(B_INT8_TYPE);
8117049c45SAxel Dörfler RETURN_TYPE(B_MESSAGE_TYPE);
8217049c45SAxel Dörfler RETURN_TYPE(B_MESSENGER_TYPE);
8317049c45SAxel Dörfler RETURN_TYPE(B_MIME_TYPE);
8417049c45SAxel Dörfler RETURN_TYPE(B_MONOCHROME_1_BIT_TYPE);
8517049c45SAxel Dörfler RETURN_TYPE(B_OBJECT_TYPE);
8617049c45SAxel Dörfler RETURN_TYPE(B_OFF_T_TYPE);
8717049c45SAxel Dörfler RETURN_TYPE(B_PATTERN_TYPE);
8817049c45SAxel Dörfler RETURN_TYPE(B_POINTER_TYPE);
8917049c45SAxel Dörfler RETURN_TYPE(B_POINT_TYPE);
9017049c45SAxel Dörfler RETURN_TYPE(B_RAW_TYPE);
9117049c45SAxel Dörfler RETURN_TYPE(B_RECT_TYPE);
9217049c45SAxel Dörfler RETURN_TYPE(B_REF_TYPE);
9317049c45SAxel Dörfler RETURN_TYPE(B_RGB_32_BIT_TYPE);
9417049c45SAxel Dörfler RETURN_TYPE(B_RGB_COLOR_TYPE);
9517049c45SAxel Dörfler RETURN_TYPE(B_SIZE_T_TYPE);
9617049c45SAxel Dörfler RETURN_TYPE(B_SSIZE_T_TYPE);
9717049c45SAxel Dörfler RETURN_TYPE(B_STRING_TYPE);
9817049c45SAxel Dörfler RETURN_TYPE(B_TIME_TYPE);
9917049c45SAxel Dörfler RETURN_TYPE(B_UINT64_TYPE);
10017049c45SAxel Dörfler RETURN_TYPE(B_UINT32_TYPE);
10117049c45SAxel Dörfler RETURN_TYPE(B_UINT16_TYPE);
10217049c45SAxel Dörfler RETURN_TYPE(B_UINT8_TYPE);
10317049c45SAxel Dörfler RETURN_TYPE(B_MEDIA_PARAMETER_TYPE);
10417049c45SAxel Dörfler RETURN_TYPE(B_MEDIA_PARAMETER_WEB_TYPE);
10517049c45SAxel Dörfler RETURN_TYPE(B_MEDIA_PARAMETER_GROUP_TYPE);
10617049c45SAxel Dörfler RETURN_TYPE(B_ASCII_TYPE);
10717049c45SAxel Dörfler
10817049c45SAxel Dörfler default:
10917049c45SAxel Dörfler return NULL;
11017049c45SAxel Dörfler }
11117049c45SAxel Dörfler #undef RETURN_TYPE
11217049c45SAxel Dörfler }
11317049c45SAxel Dörfler
11417049c45SAxel Dörfler
11517049c45SAxel Dörfler static void
print_index_long_stat(const index_info & info,char * name)11617049c45SAxel Dörfler print_index_long_stat(const index_info &info, char *name)
11717049c45SAxel Dörfler {
11817049c45SAxel Dörfler char modified[30];
11974b56a20SMurai Takashi strftime(modified, 30, "%m/%d/%Y %I:%M %p",
12074b56a20SMurai Takashi localtime(&info.modification_time));
12174b56a20SMurai Takashi printf("%16s %s %8" B_PRIdOFF " %s\n",
12274b56a20SMurai Takashi print_index_type(info, false), modified, info.size, name);
12317049c45SAxel Dörfler }
12417049c45SAxel Dörfler
12517049c45SAxel Dörfler
12617049c45SAxel Dörfler static void
print_index_verbose_stat(const index_info & info,char * name)12717049c45SAxel Dörfler print_index_verbose_stat(const index_info &info, char *name)
12817049c45SAxel Dörfler {
12917049c45SAxel Dörfler printf("%-18s\t", name);
13017049c45SAxel Dörfler
13117049c45SAxel Dörfler // Type
13217049c45SAxel Dörfler const char *typeString = type_string(info.type);
13317049c45SAxel Dörfler if (typeString != NULL)
13417049c45SAxel Dörfler printf("%-10s\t", typeString);
13517049c45SAxel Dörfler else
13674b56a20SMurai Takashi printf("%" B_PRIu32 "\t", info.type);
13717049c45SAxel Dörfler
13817049c45SAxel Dörfler // Size
13974b56a20SMurai Takashi printf("%10" B_PRIdOFF " ", info.size);
14017049c45SAxel Dörfler
14117049c45SAxel Dörfler // Created
14217049c45SAxel Dörfler char string[30];
143*1487a242SAxel Dörfler strftime(string, sizeof(string), "%Y-%m-%d %H:%M",
144*1487a242SAxel Dörfler localtime(&info.creation_time));
14517049c45SAxel Dörfler printf("%s ", string);
14617049c45SAxel Dörfler
14717049c45SAxel Dörfler // Modified
148*1487a242SAxel Dörfler strftime(string, sizeof(string), "%Y-%m-%d %H:%M",
149*1487a242SAxel Dörfler localtime(&info.modification_time));
15017049c45SAxel Dörfler printf("%s", string);
15117049c45SAxel Dörfler
15217049c45SAxel Dörfler // User
15317049c45SAxel Dörfler printf("%5d", info.uid);
15417049c45SAxel Dörfler
15517049c45SAxel Dörfler // Group
15617049c45SAxel Dörfler printf("%5d\n", info.gid);
15717049c45SAxel Dörfler }
15817049c45SAxel Dörfler
15917049c45SAxel Dörfler
16017049c45SAxel Dörfler int
main(int argc,char ** argv)16106447030SMichael Lotz main(int argc, char **argv)
16217049c45SAxel Dörfler {
16317049c45SAxel Dörfler dev_t device = dev_for_path(".");
16417049c45SAxel Dörfler DIR *indices = NULL;
16517049c45SAxel Dörfler bool verbose = false;
16617049c45SAxel Dörfler bool longListing = false;
16717049c45SAxel Dörfler bool mkindexOutput = false; /* mkindex-ready output */
16817049c45SAxel Dörfler
16917049c45SAxel Dörfler for (int i = 1; i < argc; i++) {
17017049c45SAxel Dörfler if (argv[i][0] == '-') {
17117049c45SAxel Dörfler if (!strcmp(argv[i], "--help")) {
17217049c45SAxel Dörfler print_help();
17317049c45SAxel Dörfler return 0;
174*1487a242SAxel Dörfler }
175*1487a242SAxel Dörfler if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v"))
17617049c45SAxel Dörfler verbose = true;
17717049c45SAxel Dörfler else if (!strcmp(argv[i], "--long") || !strcmp(argv[i], "-l"))
17817049c45SAxel Dörfler longListing = true;
17917049c45SAxel Dörfler else if (!strcmp(argv[i], "--mkindex"))
18017049c45SAxel Dörfler mkindexOutput = true;
18117049c45SAxel Dörfler else {
182*1487a242SAxel Dörfler fprintf(stderr, "%s: option %s is not understood (use --help "
183*1487a242SAxel Dörfler "for help)\n", argv[0], argv[i]);
18417049c45SAxel Dörfler return -1;
18517049c45SAxel Dörfler }
18617049c45SAxel Dörfler } else {
18717049c45SAxel Dörfler device = dev_for_path(argv[i]);
18817049c45SAxel Dörfler if (device < 0) {
189*1487a242SAxel Dörfler fprintf(stderr, "%s: can't get information about volume: %s\n",
190*1487a242SAxel Dörfler argv[0], argv[i]);
19117049c45SAxel Dörfler return -1;
19217049c45SAxel Dörfler }
19317049c45SAxel Dörfler }
19417049c45SAxel Dörfler }
19517049c45SAxel Dörfler
19617049c45SAxel Dörfler indices = fs_open_index_dir(device);
19717049c45SAxel Dörfler if (indices == NULL) {
19874b56a20SMurai Takashi fprintf(stderr, "%s: can't open index dir of device %" B_PRIdDEV "\n",
19974b56a20SMurai Takashi argv[0], device);
20017049c45SAxel Dörfler return -1;
20117049c45SAxel Dörfler }
20217049c45SAxel Dörfler
20317049c45SAxel Dörfler if (verbose) {
20417049c45SAxel Dörfler printf(" Name Type Size Created Modified User Group\n");
20517049c45SAxel Dörfler printf("********************************************************\n");
20617049c45SAxel Dörfler }
20717049c45SAxel Dörfler
20817049c45SAxel Dörfler while (1) {
209*1487a242SAxel Dörfler // We have to reset errno before calling fs_read_index_dir().
210*1487a242SAxel Dörfler errno = 0;
21117049c45SAxel Dörfler dirent *index = fs_read_index_dir(indices);
21217049c45SAxel Dörfler if (index == NULL) {
21317049c45SAxel Dörfler if (errno != B_ENTRY_NOT_FOUND && errno != B_OK) {
214*1487a242SAxel Dörfler printf("%s: fs_read_index_dir: (%d) %s\n", argv[0], errno,
215*1487a242SAxel Dörfler strerror(errno));
21617049c45SAxel Dörfler return errno;
21717049c45SAxel Dörfler }
21817049c45SAxel Dörfler break;
21917049c45SAxel Dörfler }
22017049c45SAxel Dörfler
22117049c45SAxel Dörfler if (verbose || longListing || mkindexOutput) {
22217049c45SAxel Dörfler index_info info;
22317049c45SAxel Dörfler
22417049c45SAxel Dörfler if (fs_stat_index(device, index->d_name, &info) != B_OK) {
225*1487a242SAxel Dörfler printf("%s: fs_stat_index(): (%d) %s\n", argv[0], errno,
226*1487a242SAxel Dörfler strerror(errno));
22717049c45SAxel Dörfler return errno;
22817049c45SAxel Dörfler }
22917049c45SAxel Dörfler
23017049c45SAxel Dörfler if (verbose)
23117049c45SAxel Dörfler print_index_verbose_stat(info, index->d_name);
23217049c45SAxel Dörfler else if (longListing)
23317049c45SAxel Dörfler print_index_long_stat(info, index->d_name);
234*1487a242SAxel Dörfler else {
235*1487a242SAxel Dörfler // mkindex output
236*1487a242SAxel Dörfler printf("mkindex -t %s '%s'\n", print_index_type(info, true),
237*1487a242SAxel Dörfler index->d_name);
238*1487a242SAxel Dörfler }
23917049c45SAxel Dörfler } else
24017049c45SAxel Dörfler printf("%s\n", index->d_name);
24117049c45SAxel Dörfler }
24217049c45SAxel Dörfler
24317049c45SAxel Dörfler fs_close_index_dir(indices);
24417049c45SAxel Dörfler return 0;
24517049c45SAxel Dörfler }
24617049c45SAxel Dörfler
247