2 * Copyright 2003-2006 Haiku Inc.
3 * Distributed under the terms of the MIT license
6 * Scott Dellinger (dellinsd@myrealbox.com)
13 #include <TypeConstants.h>
23 extern const char *__progname
;
24 const char *kProgramName
= __progname
;
28 // The following enum and #define are copied from gnu/sys2.h, because it
29 // didn't want to compile when including that directly. Since that file
30 // is marked as being temporary and getting migrated into system.h,
31 // assume these'll go away soon.
33 /* These enum values cannot possibly conflict with the option values
34 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
35 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
37 GETOPT_HELP_CHAR
= (CHAR_MIN
- 2),
38 GETOPT_VERSION_CHAR
= (CHAR_MIN
- 3)
41 static struct option
const longopts
[] = {
42 {"volume", required_argument
, 0, 'd'},
43 {"type", required_argument
, 0, 't'},
44 {"pattern", no_argument
, 0, 'p'},
45 {"verbose", no_argument
, 0, 'v'},
46 {"help", no_argument
, 0, GETOPT_HELP_CHAR
},
55 "Usage: %s [OPTION]... INDEX_NAME\n"
57 "Removes the index named INDEX_NAME from a disk volume. Once this has been\n"
58 "done, it will no longer be possible to use the query system to search for\n"
59 "files with the INDEX_NAME attribute.\n"
61 " -d, --volume=PATH a path on the volume from which the index will be\n"
63 " -h, --help display this help and exit\n"
64 " -p, --pattern INDEX_NAME is a pattern\n"
65 " -v, --verbose print information about the index being removed\n"
67 "INDEX_NAME is the name of a file attribute.\n"
69 "If no volume is specified, the volume of the current directory is assumed.\n",
77 lookup_index_type(uint32 device_type
)
79 switch (device_type
) {
98 remove_index(dev_t device
, const char* indexName
, bool verbose
)
101 // Get the index type
103 status_t status
= fs_stat_index(device
, indexName
, &info
);
104 if (status
!= B_OK
) {
105 fprintf(stderr
, "%s: Can't get type of index %s: %s\n",
106 kProgramName
, indexName
, strerror(errno
));
110 fprintf(stdout
, "Removing index \"%s\" of type %s.\n",
111 indexName
, lookup_index_type(info
.type
));
114 if (fs_remove_index(device
, indexName
) != 0) {
115 fprintf(stderr
, "%s: Cannot remove index %s: %s\n", kProgramName
, indexName
, strerror(errno
));
124 open_index_dir(const char* /*path*/)
126 return fs_open_index_dir(gCurrentDevice
);
131 stat_index(const char* /*index*/, struct stat
* stat
)
133 memset(stat
, 0, sizeof(struct stat
));
134 stat
->st_mode
= S_IFREG
;
140 remove_indices(dev_t device
, const char* indexPattern
, bool verbose
)
143 memset(&glob
, 0, sizeof(glob_t
));
145 glob
.gl_closedir
= (void (*)(void *))fs_close_index_dir
;
146 glob
.gl_readdir
= (dirent
*(*)(void *))fs_read_index_dir
;
147 glob
.gl_opendir
= open_index_dir
;
148 glob
.gl_lstat
= stat_index
;
149 glob
.gl_stat
= stat_index
;
151 // for open_attr_dir():
152 gCurrentDevice
= device
;
154 int result
= ::glob(indexPattern
, GLOB_ALTDIRFUNC
, NULL
, &glob
);
162 for (int i
= 0; i
< glob
.gl_pathc
; i
++) {
163 if (remove_index(device
, glob
.gl_pathv
[i
], verbose
) != 0)
167 return error
? -1 : 0;
172 main(int argc
, char **argv
)
174 bool isPattern
= false;
175 bool verbose
= false;
177 char *indexName
= NULL
;
181 while ((c
= getopt_long(argc
, argv
, "d:ht:pv", longopts
, NULL
)) != -1) {
188 case GETOPT_HELP_CHAR
:
203 // Remove the index from the volume of the current
204 // directory if no volume was specified.
208 device
= dev_for_path(path
);
210 fprintf(stderr
, "%s: can't get information about volume %s\n", kProgramName
, path
);
214 if (argc
- optind
== 1) {
216 indexName
= argv
[optind
];
222 result
= remove_indices(device
, indexName
, verbose
);
224 result
= remove_index(device
, indexName
, verbose
);
226 return result
== 0 ? 0 : 1;