]> git.sesse.net Git - plocate/blobdiff - plocate.cpp
Bump version number.
[plocate] / plocate.cpp
index 72dbe287434ac848d705f17b02f58929bdd4b16c..1c0b3d0463f09ea04a5f9a39156d91a1e089c7fe 100644 (file)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <string>
 #include <string_view>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <thread>
@@ -53,9 +54,11 @@ bool flush_cache = false;
 bool patterns_are_regex = false;
 bool use_extended_regex = false;
 bool match_basename = false;
+bool check_existence = false;
 int64_t limit_matches = numeric_limits<int64_t>::max();
 int64_t limit_left = numeric_limits<int64_t>::max();
 bool stdout_is_tty = false;
+bool literal_printing = false;
 static bool in_forked_child = false;
 
 steady_clock::time_point start;
@@ -152,8 +155,24 @@ size_t Corpus::get_num_filename_blocks() const
        return hdr.num_docids;
 }
 
+template<class T>
+void stat_if_needed(const char *filename, bool access_ok, IOUringEngine *engine, T cb)
+{
+       if (!access_ok || !check_existence) {
+               // Doesn't have access or doesn't care about existence, so no need to stat.
+               cb(access_ok);
+       } else if (engine == nullptr || !engine->get_supports_stat()) {
+               // Do a synchronous stat.
+               struct stat buf;
+               bool ok = lstat(filename, &buf) == 0;
+               cb(ok);
+       } else {
+               engine->submit_stat(filename, cb);
+       }
+}
+
 void scan_file_block(const vector<Needle> &needles, string_view compressed,
-                     AccessRXCache *access_rx_cache, uint64_t seq, ResultReceiver *serializer,
+                     IOUringEngine *engine, AccessRXCache *access_rx_cache, uint64_t seq, ResultReceiver *serializer,
                      atomic<uint64_t> *matched)
 {
        unsigned long long uncompressed_len = ZSTD_getFrameContentSize(compressed.data(), compressed.size());
@@ -182,14 +201,16 @@ void scan_file_block(const vector<Needle> &needles, string_view compressed,
        block[block.size() - 1] = '\0';
 
        auto test_candidate = [&](const char *filename, uint64_t local_seq, uint64_t next_seq) {
-               access_rx_cache->check_access(filename, /*allow_async=*/true, [matched, serializer, local_seq, next_seq, filename{ strdup(filename) }](bool ok) {
-                       if (ok) {
-                               ++*matched;
-                               serializer->print(local_seq, next_seq - local_seq, filename);
-                       } else {
-                               serializer->print(local_seq, next_seq - local_seq, "");
-                       }
-                       free(filename);
+               access_rx_cache->check_access(filename, /*allow_async=*/true, [matched, engine, serializer, local_seq, next_seq, filename{ strdup(filename) }](bool ok) {
+                       stat_if_needed(filename, ok, engine, [matched, serializer, local_seq, next_seq, filename](bool ok) {
+                               if (ok) {
+                                       ++*matched;
+                                       serializer->print(local_seq, next_seq - local_seq, filename);
+                               } else {
+                                       serializer->print(local_seq, next_seq - local_seq, "");
+                               }
+                               free(filename);
+                       });
                });
        };
 
@@ -240,8 +261,8 @@ size_t scan_docids(const vector<Needle> &needles, const vector<uint32_t> &docids
        atomic<uint64_t> matched{ 0 };
        for (size_t i = 0; i < docids.size(); ++i) {
                uint32_t docid = docids[i];
-               corpus.get_compressed_filename_block(docid, [i, &matched, &needles, &access_rx_cache, &docids_in_order](string_view compressed) {
-                       scan_file_block(needles, compressed, &access_rx_cache, i, &docids_in_order, &matched);
+               corpus.get_compressed_filename_block(docid, [i, &matched, &needles, &access_rx_cache, engine, &docids_in_order](string_view compressed) {
+                       scan_file_block(needles, compressed, engine, &access_rx_cache, i, &docids_in_order, &matched);
                });
        }
        engine->finish();
@@ -359,7 +380,8 @@ uint64_t scan_all_docids(const vector<Needle> &needles, int fd, const Corpus &co
                                for (uint32_t docid = io_docid; docid < last_docid; ++docid) {
                                        size_t relative_offset = offsets[docid] - offsets[io_docid];
                                        size_t len = offsets[docid + 1] - offsets[docid];
-                                       scan_file_block(*use_needles, { &compressed[relative_offset], len }, &access_rx_cache, docid, &receiver, &matched);
+                                       // IOUringEngine isn't thread-safe, so we do any needed stat()s synchronously (nullptr engine).
+                                       scan_file_block(*use_needles, { &compressed[relative_offset], len }, /*engine=*/nullptr, &access_rx_cache, docid, &receiver, &matched);
                                }
                        }
                });
@@ -788,6 +810,7 @@ void usage()
                "  -i, --ignore-case      search case-insensitively\n"
                "  -l, --limit LIMIT      stop after LIMIT matches\n"
                "  -0, --null             delimit matches by NUL instead of newline\n"
+               "  -N, --literal          do not quote filenames, even if printing to a tty\n"
                "  -r, --regexp           interpret patterns as basic regexps (slow)\n"
                "      --regex            interpret patterns as extended regexps (slow)\n"
                "  -w, --wholename        search the entire path name (default; see -b)\n"
@@ -816,8 +839,10 @@ int main(int argc, char **argv)
                { "count", no_argument, 0, 'c' },
                { "basename", no_argument, 0, 'b' },
                { "database", required_argument, 0, 'd' },
+               { "existing", no_argument, 0, 'e' },
                { "ignore-case", no_argument, 0, 'i' },
                { "limit", required_argument, 0, 'l' },
+               { "literal", no_argument, 0, 'N' },
                { "null", no_argument, 0, '0' },
                { "version", no_argument, 0, 'V' },
                { "regexp", no_argument, 0, 'r' },
@@ -832,7 +857,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        for (;;) {
                int option_index = 0;
-               int c = getopt_long(argc, argv, "bcd:hil:n:0rwVD", long_options, &option_index);
+               int c = getopt_long(argc, argv, "bcd:ehil:n:N0rwVD", long_options, &option_index);
                if (c == -1) {
                        break;
                }
@@ -846,6 +871,9 @@ int main(int argc, char **argv)
                case 'd':
                        parse_dbpaths(optarg, &dbpaths);
                        break;
+               case 'e':
+                       check_existence = true;
+                       break;
                case 'h':
                        usage();
                        exit(0);
@@ -860,6 +888,9 @@ int main(int argc, char **argv)
                                exit(1);
                        }
                        break;
+               case 'N':
+                       literal_printing = true;
+                       break;
                case '0':
                        print_nul = true;
                        break;