34
34
@copyright (c) BSD-3 License - see LICENSE.txt
35
35
*/
36
36
37
- #define UGREP_INDEXER_VERSION " 0.9 beta"
37
+ #define UGREP_INDEXER_VERSION " 0.9.1 beta"
38
38
39
39
// check if we are compiling for a windows OS, but not Cygwin or MinGW
40
40
#if (defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__BORLANDC__)) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
104
104
#include < vector>
105
105
#include < stack>
106
106
107
+ // number of bytes to gulp into the buffer to index a file
107
108
#define BUF_SIZE 65536
109
+
110
+ // smallest possible power-of-two size of an index of a file, shoud be > 61
108
111
#define MIN_SIZE 128
109
112
113
+ // default --ignore-files=FILE argument
110
114
#define DEFAULT_IGNORE_FILE " .gitignore"
111
115
116
+ // fixed constant strings
112
117
const char ugrep_index_filename[] = " ._UG#_Store" ;
113
118
const char ugrep_index_file_magic[5 ] = " UG#\x03 " ;
114
119
120
+ // command-line optional PATH argument
121
+ const char *arg_pathname = NULL ;
122
+
123
+ // command-line options
115
124
int flag_accuracy = 6 ;
116
125
bool flag_check = false ;
117
126
bool flag_decompress = false ;
@@ -131,20 +140,26 @@ struct Ignore {
131
140
std::vector<std::string> dirs;
132
141
};
133
142
134
- // stack of ignore files/dirs
143
+ // stack of ignore file/dir globs per ignore-file found
135
144
std::stack<Ignore> ignore_stack;
136
145
137
146
// entry data extracted from directory contents, moves pathname to this entry
138
147
struct Entry {
139
148
149
+ // indexing is initiated with the pathname to the root of the directory to index
140
150
Entry (const char *pathname = " ." )
141
151
:
142
152
pathname (pathname), // the working dir by default
143
153
base (0 ),
144
154
mtime (~0ULL ), // max time to make sure we check the working directory for updates
145
155
size (0 )
146
- { }
156
+ {
157
+ const char *sep = strrchr (pathname, PATHSEPCHR);
158
+ if (sep != NULL )
159
+ base = strlen (sep) - 1 ;
160
+ }
147
161
162
+ // new pathname entry, note this moves the pathname to the entry that owns it now
148
163
Entry (std::string& pathname, size_t base, uint64_t mtime, off_t size)
149
164
:
150
165
pathname (std::move(pathname)),
@@ -175,7 +190,7 @@ struct Entry {
175
190
}
176
191
177
192
std::string pathname; // full pathname
178
- size_t base; // size of the basename in the pathname
193
+ size_t base; // length of the basename in the pathname
179
194
uint64_t mtime; // modification time
180
195
off_t size; // file size
181
196
@@ -193,7 +208,8 @@ void version()
193
208
// display a help message and exit
194
209
void help ()
195
210
{
196
- std::cout << " Usage: ugrep-indexer [-0|...|-9] [-.] [-c|-d|-f] [-I] [-q] [-S] [-s] [-X] [-z]\n\n \
211
+ std::cout << " \n Usage:\n\n ugrep-indexer [-0|...|-9] [-.] [-c|-d|-f] [-I] [-q] [-S] [-s] [-X] [-z] [PATH]\n\n \
212
+ PATH Optional pathname to the root of the directory tree to index.\n\n \
197
213
-0, -1, -2, -3, ..., -9, --accuracy=DIGIT\n \
198
214
Specifies indexing accuracy. A low accuracy reduces the indexing\n \
199
215
storage overhead at the cost of a higher rate of false positive\n \
@@ -392,6 +408,14 @@ void options(int argc, const char **argv)
392
408
}
393
409
}
394
410
}
411
+ else if (arg_pathname == NULL )
412
+ {
413
+ arg_pathname = arg;
414
+ }
415
+ else
416
+ {
417
+ usage (" argument PATH already specified as " , arg_pathname);
418
+ }
395
419
}
396
420
397
421
if (flag_check)
@@ -408,7 +432,6 @@ inline int fopenw_s(FILE **file, const char *filename, const char *mode)
408
432
#if defined(HAVE_F_RDAHEAD)
409
433
if (strchr (mode, ' a' ) == NULL && strchr (mode, ' w' ) == NULL )
410
434
{
411
- // removed O_NOATIME which may fail
412
435
#if defined(O_NOCTTY)
413
436
int fd = open (filename, O_RDONLY | O_NOCTTY);
414
437
#else
@@ -874,7 +897,7 @@ void cat(const std::string& pathname, std::stack<Entry>& dir_entries, std::vecto
874
897
}
875
898
876
899
// recursively delete index files
877
- void deleter ()
900
+ void deleter (const char *pathname )
878
901
{
879
902
flag_no_messages = true ;
880
903
@@ -891,7 +914,11 @@ void deleter()
891
914
uint64_t index_time;
892
915
uint64_t last_time;
893
916
894
- dir_entries.emplace ();
917
+ // pathname to the directory tree to index or .
918
+ if (pathname == NULL )
919
+ dir_entries.emplace ();
920
+ else
921
+ dir_entries.emplace (pathname);
895
922
896
923
// recurse subdirectories breadth-first to remove index files
897
924
while (!dir_entries.empty ())
@@ -901,6 +928,7 @@ void deleter()
901
928
902
929
cat (visit.pathname , dir_entries, file_entries, num_dirs, num_links, num_other, ign_dirs, ign_files, index_time, last_time, true );
903
930
931
+ // if index time is nonzero, there is a valid index file in this directory we should remove
904
932
if (index_time > 0 )
905
933
{
906
934
index_filename.assign (visit.pathname ).append (PATHSEPSTR).append (ugrep_index_filename);
@@ -910,7 +938,7 @@ void deleter()
910
938
}
911
939
912
940
// recursively index files
913
- void indexer ()
941
+ void indexer (const char *pathname )
914
942
{
915
943
std::stack<Entry> dir_entries;
916
944
std::vector<Entry> file_entries;
@@ -933,7 +961,11 @@ void indexer()
933
961
float sum_noise = 0 ;
934
962
uint8_t hashes[65536 ];
935
963
936
- dir_entries.emplace ();
964
+ // pathname to the directory tree to index or .
965
+ if (pathname == NULL )
966
+ dir_entries.emplace ();
967
+ else
968
+ dir_entries.emplace (pathname);
937
969
938
970
// recurse subdirectories
939
971
while (!dir_entries.empty ())
@@ -1209,9 +1241,9 @@ int main(int argc, const char **argv)
1209
1241
options (argc, argv);
1210
1242
1211
1243
if (flag_delete)
1212
- deleter ();
1244
+ deleter (arg_pathname );
1213
1245
else
1214
- indexer ();
1246
+ indexer (arg_pathname );
1215
1247
1216
1248
return EXIT_SUCCESS;
1217
1249
}
0 commit comments