Files
haiku-beta6/src/bin/query.cpp
T

164 lines
3.8 KiB
C++
Raw Normal View History

2005-04-26 20:29:30 +00:00
/*
* Copyright 2005, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
* Ficus Kirkpatrick ([email protected])
* Jérôme Duval
* Axel Dörfler, [email protected]
*/
/* 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>
2003-11-04 09:30:46 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2004-09-29 07:59:55 +00:00
#include <unistd.h>
2003-11-04 09:30:46 +00:00
2005-04-26 20:29:30 +00:00
extern const char *__progname;
static const char *kProgramName = __progname;
2003-11-04 09:30:46 +00:00
// Option variables.
2005-04-26 20:29:30 +00:00
static bool sAllVolumes = false; // Query all volumes?
static bool sEscapeMetaChars = true; // Escape metacharacters?
static bool sFilesOnly = false; // Show only files?
void
usage(void)
2003-11-04 09:30:46 +00:00
{
2005-04-26 20:29:30 +00:00
printf("usage: %s [ -ef ] [ -a || -v <path-to-volume> ] expression\n"
" -e\t\tdon't escape meta-characters\n"
2005-04-26 20:29:30 +00:00
" -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",
2005-04-26 20:29:30 +00:00
kProgramName, kProgramName);
2003-11-04 09:30:46 +00:00
exit(0);
}
void
perform_query(BVolume &volume, const char *predicate)
2003-11-04 09:30:46 +00:00
{
BQuery query;
2003-11-04 09:30:46 +00:00
// Set up the volume and predicate for the query.
query.SetVolume(&volume);
2003-11-04 09:30:46 +00:00
query.SetPredicate(predicate);
status_t status = query.Fetch();
if (status == B_BAD_VALUE) {
// the "name=" part may be omitted in our arguments
BString string = "name=";
string << predicate;
query.SetPredicate(string.String());
status = query.Fetch();
}
if (status != B_OK) {
2005-04-26 20:29:30 +00:00
fprintf(stderr, "%s: bad query expression\n", kProgramName);
2003-11-04 09:30:46 +00:00
return;
}
BEntry entry;
BPath path;
while (query.GetNextEntry(&entry) == B_OK) {
2005-04-26 20:29:30 +00:00
if (sFilesOnly && !entry.IsFile())
continue;
if (entry.GetPath(&path) < B_OK) {
2005-04-26 20:29:30 +00:00
fprintf(stderr, "%s: could not get path for entry\n", kProgramName);
continue;
}
2005-04-26 20:29:30 +00:00
printf("%s\n", sEscapeMetaChars ?
BString().CharacterEscape(path.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String()
: path.Path());
2003-11-04 09:30:46 +00:00
}
}
int
main(int32 argc, char **argv)
2003-11-04 09:30:46 +00:00
{
// Make sure we have the minimum number of arguments.
if (argc < 2)
usage();
2003-11-04 09:30:46 +00:00
// Which volume do we make the query on?
// Default to the current volume.
char volumePath[B_FILE_NAME_LENGTH];
strcpy(volumePath, ".");
2003-11-04 09:30:46 +00:00
// Parse command-line arguments.
2004-09-29 07:59:55 +00:00
int opt;
2005-04-26 20:29:30 +00:00
while ((opt = getopt(argc, argv, "efav:")) != -1) {
2003-11-04 09:30:46 +00:00
switch(opt) {
case 'e':
2005-04-26 20:29:30 +00:00
sEscapeMetaChars = false;
break;
case 'f':
sFilesOnly = true;
break;
case 'a':
sAllVolumes = true;
break;
case 'v':
strncpy(volumePath, optarg, B_FILE_NAME_LENGTH);
break;
default:
usage();
break;
2003-11-04 09:30:46 +00:00
}
}
BVolume volume;
2005-04-26 20:29:30 +00:00
if (!sAllVolumes) {
2003-11-04 09:30:46 +00:00
// Find the volume that the query should be performed on,
// and set the query to it.
BEntry entry(volumePath);
if (entry.InitCheck() != B_OK) {
2005-04-26 20:29:30 +00:00
fprintf(stderr, "%s: \"%s\" is not a valid file\n", kProgramName, volumePath);
exit(1);
}
status_t status = entry.GetVolume(&volume);
if (status != B_OK) {
2005-04-26 20:29:30 +00:00
fprintf(stderr, "%s: could not get volume: %s\n", kProgramName, strerror(status));
exit(1);
2003-11-04 09:30:46 +00:00
}
if (!volume.KnowsQuery())
2005-04-26 20:29:30 +00:00
fprintf(stderr, "%s: volume containing %s is not query-enabled\n", kProgramName, volumePath);
2003-11-04 09:30:46 +00:00
else
perform_query(volume, argv[optind]);
} else {
// Okay, we want to query all the disks -- so iterate over
// them, one by one, running the query.
BVolumeRoster volumeRoster;
while (volumeRoster.GetNextVolume(&volume) == B_OK) {
// We don't print errors here -- this will catch /pipe and
// other filesystems we don't care about.
if (volume.KnowsQuery())
perform_query(volume, argv[optind]);
}
2003-11-04 09:30:46 +00:00
}
return 0;
2003-11-04 09:30:46 +00:00
}