Added "-f" option to only show real files.

Some cleanups and minor fixes.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12476 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-04-26 20:29:30 +00:00
parent 7542e025d3
commit a38e60f223
1 changed files with 47 additions and 35 deletions

View File

@ -1,45 +1,51 @@
// query.cpp
//
// A shell utility for somewhat emulating the Tracker's "Find By Formula"
// functionality.
//
// by Ficus Kirkpatrick (ficus@ior.com)
//
// Modified by Jerome Duval on November 03, 2003
//
/*
* Copyright 2005, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
* Ficus Kirkpatrick (ficus@ior.com)
* Jérôme Duval
* Axel Dörfler, axeld@pinc-software.de
*/
/* A shell utility for somewhat emulating the Tracker's "Find By Formula"
* functionality.
*/
#include <Path.h>
#include <Query.h>
#include <Entry.h>
#include <Volume.h>
#include <VolumeRoster.h>
#include <String.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <storage/Path.h>
#include <storage/Query.h>
#include <storage/Entry.h>
#include <storage/Volume.h>
#include <storage/VolumeRoster.h>
#include <support/SupportDefs.h>
#include <support/String.h>
// Option variables.
bool o_all_volumes = false; // Query all volumes?
bool o_escaping = true; // Escape metacharacters?
extern const char *__progname;
static const char *kProgramName = __progname;
// Option variables.
static bool sAllVolumes = false; // Query all volumes?
static bool sEscapeMetaChars = true; // Escape metacharacters?
static bool sFilesOnly = false; // Show only files?
void
usage(void)
{
printf("usage: %s [ -e ] [ -a || -v <path-to-volume> ] expression\n"
printf("usage: %s [ -ef ] [ -a || -v <path-to-volume> ] expression\n"
" -e\t\tdon't escape meta-characters\n"
" -f\t\tshow only files (ie. no directories or symbolic links)\n"
" -a\t\tperform the query on all volumes\n"
" -v <file>\tperform the query on just one volume; <file> can be any\n"
"\t\tfile on that volume. Defaults to the current volume.\n"
" Hint: '%s name=foo' will find files named \"foo\"\n",
__progname, __progname);
kProgramName, kProgramName);
exit(0);
}
@ -63,19 +69,22 @@ perform_query(BVolume &volume, const char *predicate)
status = query.Fetch();
}
if (status != B_OK) {
printf("query: bad query expression\n");
fprintf(stderr, "%s: bad query expression\n", kProgramName);
return;
}
BEntry entry;
BPath path;
while (query.GetNextEntry(&entry) == B_OK) {
if (sFilesOnly && !entry.IsFile())
continue;
if (entry.GetPath(&path) < B_OK) {
fprintf(stderr, "%s: could not get path for entry\n", __progname);
fprintf(stderr, "%s: could not get path for entry\n", kProgramName);
continue;
}
printf("%s\n", o_escaping ?
printf("%s\n", sEscapeMetaChars ?
BString().CharacterEscape(path.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String()
: path.Path());
}
@ -96,13 +105,16 @@ main(int32 argc, char **argv)
// Parse command-line arguments.
int opt;
while ((opt = getopt(argc, argv, "eav:")) != -1) {
while ((opt = getopt(argc, argv, "efav:")) != -1) {
switch(opt) {
case 'a':
o_all_volumes = true;
break;
case 'e':
o_escaping = false;
sEscapeMetaChars = false;
break;
case 'f':
sFilesOnly = true;
break;
case 'a':
sAllVolumes = true;
break;
case 'v':
strncpy(volumePath, optarg, B_FILE_NAME_LENGTH);
@ -116,23 +128,23 @@ main(int32 argc, char **argv)
BVolume volume;
if (!o_all_volumes) {
if (!sAllVolumes) {
// Find the volume that the query should be performed on,
// and set the query to it.
BEntry entry(volumePath);
if (entry.InitCheck() != B_OK) {
fprintf(stderr, "%s: \"%s\" is not a valid file\n", __progname, volumePath);
fprintf(stderr, "%s: \"%s\" is not a valid file\n", kProgramName, volumePath);
exit(1);
}
status_t status = entry.GetVolume(&volume);
if (status != B_OK) {
fprintf(stderr, "%s: could not get volume: %s\n", __progname, strerror(status));
fprintf(stderr, "%s: could not get volume: %s\n", kProgramName, strerror(status));
exit(1);
}
if (!volume.KnowsQuery())
fprintf(stderr, "%s: volume containing %s is not query-enabled\n", __progname, volumePath);
fprintf(stderr, "%s: volume containing %s is not query-enabled\n", kProgramName, volumePath);
else
perform_query(volume, argv[optind]);
} else {