2 * Copyright (c) 2003-2005, Haiku
4 * This software is part of the Haiku distribution and is covered
8 * Scott Dellinger (dellinsd@myrealbox.com)
9 * Axel Dörfler, axeld@pinc-software.de.
12 /** Description: Adds an index to a volume. */
17 #include <TypeConstants.h>
18 #include <Directory.h>
29 static struct option
const kLongOptions
[] = {
30 {"volume", required_argument
, 0, 'd'},
31 {"type", required_argument
, 0, 't'},
32 {"copy-from", required_argument
, 0, 'f'},
33 {"verbose", no_argument
, 0, 'v'},
34 {"help", no_argument
, 0, 'h'},
38 extern const char *__progname
;
39 static const char *kProgramName
= __progname
;
43 copy_indexes(dev_t from
, dev_t to
, bool verbose
)
45 DIR *indexes
= fs_open_index_dir(from
);
48 puts("Copying indexes:");
50 while (dirent
*dirent
= fs_read_index_dir(indexes
)) {
51 if (!strcmp(dirent
->d_name
, "name")
52 || !strcmp(dirent
->d_name
, "size")
53 || !strcmp(dirent
->d_name
, "last_modified"))
57 if (fs_stat_index(from
, dirent
->d_name
, &info
) != 0) {
58 fprintf(stderr
, "%s: Skipped index \"%s\": %s\n",
59 kProgramName
, dirent
->d_name
, strerror(errno
));
63 if (fs_create_index(to
, dirent
->d_name
, info
.type
, 0) != 0) {
64 if (errno
== B_BAD_VALUE
|| errno
== B_FILE_EXISTS
) {
65 // B_BAD_VALUE is what BeOS returns here...
68 fprintf(stderr
, "%s: Could not create index \"%s\": %s\n",
69 kProgramName
, dirent
->d_name
, strerror(errno
));
71 printf("\t%s\n", dirent
->d_name
);
74 fs_close_index_dir(indexes
);
82 "Usage: %s [options] <attribute>\n"
83 "Creates a new index for the specified attribute.\n"
85 " -d, --volume=PATH\ta path on the volume to which the index will be added,\n"
86 "\t\t\tdefaults to current volume.\n"
87 " -t, --type=TYPE the type of the attribute being indexed. One of \"int\",\n"
88 "\t\t\t\"llong\", \"string\", \"float\", or \"double\".\n"
89 "\t\t\tDefaults to \"string\".\n"
90 " --copy-from\tpath to volume to copy the indexes from.\n"
91 " -v, --verbose\t\tprint information about the index being created\n",
99 main(int argc
, char **argv
)
101 const char *indexTypeName
= "string";
102 int indexType
= B_STRING_TYPE
;
103 char *indexName
= NULL
;
104 bool verbose
= false;
105 dev_t device
= -1, copyFromDevice
= -1;
108 while ((c
= getopt_long(argc
, argv
, "d:ht:v", kLongOptions
, NULL
)) != -1) {
113 device
= dev_for_path(optarg
);
115 fprintf(stderr
, "%s: can't get information about volume: %s\n",
116 kProgramName
, optarg
);
121 copyFromDevice
= dev_for_path(optarg
);
122 if (copyFromDevice
< 0) {
123 fprintf(stderr
, "%s: can't get information about volume: %s\n",
124 kProgramName
, optarg
);
132 indexTypeName
= optarg
;
133 if (strncmp("int", optarg
, 3) == 0)
134 indexType
= B_INT32_TYPE
;
135 else if (strncmp("llong", optarg
, 5) == 0)
136 indexType
= B_INT64_TYPE
;
137 else if (strncmp("string", optarg
, 6) == 0)
138 indexType
= B_STRING_TYPE
;
139 else if (strncmp("float", optarg
, 5) == 0)
140 indexType
= B_FLOAT_TYPE
;
141 else if (strncmp("double", optarg
, 6) == 0)
142 indexType
= B_DOUBLE_TYPE
;
156 // Create the index on the volume of the current
157 // directory if no volume was specified.
159 device
= dev_for_path(".");
161 fprintf(stderr
, "%s: can't get information about current volume\n", kProgramName
);
166 if (copyFromDevice
!= -1) {
167 copy_indexes(copyFromDevice
, device
, verbose
);
171 if (argc
- optind
== 1) {
173 indexName
= argv
[optind
];
178 /* Get the mount point of the specified volume. */
179 BVolume
volume(device
);
181 volume
.GetRootDirectory(&dir
);
182 BPath
path(&dir
, NULL
);
184 printf("Creating index \"%s\" of type %s on volume mounted at %s.\n",
185 indexName
, indexTypeName
, path
.Path());
188 if (fs_create_index(device
, indexName
, indexType
, 0) != 0)
189 fprintf(stderr
, "%s: Could not create index: %s\n", kProgramName
, strerror(errno
));