Ticket #222: omindex-noatime-updated.patch
File omindex-noatime-updated.patch, 8.8 KB (added by , 14 years ago) |
---|
-
diritor.cc
21 21 22 22 #include "diritor.h" 23 23 24 #include "safeunistd.h" 25 #include <sys/types.h> 24 26 #include "safeerrno.h" 25 27 26 28 #include <cstring> 27 29 28 30 using namespace std; 29 31 32 uid_t DirectoryIterator::euid = geteuid(); 33 30 34 void 31 35 DirectoryIterator::call_stat() 32 36 { -
diritor.h
24 24 25 25 #include "safedirent.h" 26 26 #include "safeerrno.h" 27 #include "safefcntl.h" 27 28 #include "safesysstat.h" 28 29 29 30 #include <sys/types.h> … … 32 33 33 34 #include "common/noreturn.h" 34 35 36 #include "loadfile.h" 37 #include "runfilter.h" // For class ReadError. 38 35 39 class DirectoryIterator { 40 static uid_t euid; 41 36 42 std::string path; 37 43 DIR * dir; 38 44 struct dirent *entry; … … 150 156 ensure_statbuf_valid(); 151 157 return (statbuf.st_mode & S_IROTH); 152 158 } 159 160 bool try_noatime() { 161 #if defined O_NOATIME && O_NOATIME != 0 162 if (euid == 0) return true; 163 ensure_statbuf_valid(); 164 return statbuf.st_uid == euid; 165 #else 166 return false; 167 #endif 168 } 169 170 std::string file_to_string() { 171 std::string out; 172 std::string file = path; 173 file += '/'; 174 file += leafname(); 175 int flags = NOCACHE; 176 if (try_noatime()) flags |= NOATIME; 177 if (!load_file(file, out, flags)) throw ReadError(); 178 return out; 179 } 153 180 }; 154 181 155 182 #endif // OMEGA_INCLUDED_DIRITOR_H -
loadfile.cc
1 1 /* loadfile.cc: load a file into a std::string. 2 2 * 3 * Copyright (C) 2006 Olly Betts3 * Copyright (C) 2006,2007 Olly Betts 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify 6 6 * it under the terms of the GNU General Public License as published by … … 26 26 # endif 27 27 #endif 28 28 29 #include "loadfile.h" 30 29 31 #include <algorithm> 30 32 #include <string> 31 33 … … 39 41 # ifdef __linux__ 40 42 // This is the value used by rml's O_STREAMING patch for 2.4. 41 43 # define O_STREAMING 04000000 42 # else43 // Define as 0 otherwise, so we don't need ifdefs in the code.44 # define O_STREAMING 045 44 # endif 46 45 #endif 47 46 48 #include "loadfile.h"49 50 47 using namespace std; 51 48 52 49 bool 53 load_file(const string &file_name, size_t max_to_read, 54 bool try_not_to_cache, 50 load_file(const string &file_name, size_t max_to_read, int flags, 55 51 string &output, bool &truncated) 56 52 { 53 (void)flags; // Avoid possible "unused" warning. 57 54 mode_t mode = O_RDONLY; 58 if (try_not_to_cache) mode |= O_STREAMING; 55 #ifdef O_STREAMING 56 if (flags & NOCACHE) mode |= O_STREAMING; 57 #endif 58 #if defined O_NOATIME && O_NOATIME != 0 59 if (flags & NOATIME) mode |= O_NOATIME; 60 #endif 59 61 60 62 int fd = open(file_name.c_str(), mode); 63 #if defined O_NOATIME && O_NOATIME != 0 64 if (fd < 0 && (mode & O_NOATIME)) { 65 mode &= ~O_NOATIME; 66 fd = open(file_name.c_str(), mode); 67 } 68 #endif 61 69 if (fd < 0) return false; 62 70 63 71 #ifdef HAVE_POSIX_FADVISE 64 if ( try_not_to_cache)72 if (flags & NOCACHE) 65 73 posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE); // or POSIX_FADV_SEQUENTIAL 66 74 #endif 67 75 … … 97 105 } 98 106 99 107 #ifdef HAVE_POSIX_FADVISE 100 if ( try_not_to_cache)108 if (flags & NOCACHE) 101 109 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); 102 110 #endif 103 111 -
loadfile.h
1 1 /* loadfile.h: load a file into a std::string. 2 2 * 3 * Copyright (C) 2006 Olly Betts3 * Copyright (C) 2006,2007 Olly Betts 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify 6 6 * it under the terms of the GNU General Public License as published by … … 22 22 23 23 #include <string> 24 24 25 extern bool load_file(const std::string &file_name, size_t max_to_read, 26 bool try_not_to_cache, 27 std::string &output, bool &truncated); 25 enum { NOCACHE = 0x1, NOATIME = 0x2 }; 26 27 bool load_file(const std::string &file_name, size_t max_to_read, int flags, 28 std::string &output, bool &truncated); 28 29 29 30 inline bool 30 load_file(const std::string &file_name, std::string &output, 31 bool try_not_to_cache = false) 31 load_file(const std::string &file_name, std::string &output, int flags = 0) 32 32 { 33 33 bool dummy; 34 return load_file(file_name, 0, try_not_to_cache, output, dummy);34 return load_file(file_name, 0, flags, output, dummy); 35 35 } 36 36 37 37 #endif // OMEGA_INCLUDED_LOADFILE_H -
md5wrap.cc
1 1 /* md5wrap.cc: wrapper functions to allow easy use of MD5 from C++. 2 2 * 3 * Copyright (C) 2006 Olly Betts3 * Copyright (C) 2006,2007 Olly Betts 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify 6 6 * it under the terms of the GNU General Public License as published by … … 48 48 using namespace std; 49 49 50 50 bool 51 md5_file(const string &file_name, string &md5 )51 md5_file(const string &file_name, string &md5, bool try_noatime) 52 52 { 53 mode_t mode = O_RDONLY; 54 mode |= O_STREAMING; 53 mode_t mode = O_RDONLY|O_STREAMING; 54 #if defined O_NOATIME && O_NOATIME != 0 55 if (try_noatime) mode |= O_NOATIME; 56 #else 57 (void)try_noatime; 58 #endif 55 59 56 60 int fd = open(file_name.c_str(), mode); 61 #if defined O_NOATIME && O_NOATIME != 0 62 if (fd < 0 && (mode & O_NOATIME)) { 63 mode &= ~O_NOATIME; 64 fd = open(file_name.c_str(), mode); 65 } 66 #endif 57 67 if (fd < 0) return false; 58 68 59 69 #ifdef HAVE_POSIX_FADVISE -
md5wrap.h
22 22 23 23 #include <string> 24 24 25 bool md5_file(const std::string &file_name, std::string &md5 );25 bool md5_file(const std::string &file_name, std::string &md5, bool try_noatime); 26 26 void md5_string(const std::string &str, std::string &md5); 27 27 28 28 #endif // OMEGA_INCLUDED_MD5WRAP_H -
omindex.cc
46 46 #include "commonhelp.h" 47 47 #include "diritor.h" 48 48 #include "hashterm.h" 49 #include "loadfile.h"50 49 #include "md5wrap.h" 51 50 #include "metaxmlparse.h" 52 51 #include "myhtmlparse.h" … … 173 172 return (p != NULL); 174 173 } 175 174 176 static string177 file_to_string(const string &file)178 {179 string out;180 if (!load_file(file, out)) throw ReadError();181 return out;182 }183 184 175 static void 185 176 get_pdf_metainfo(const string & safefile, string &title, string &keywords) 186 177 { … … 275 266 if (mimetype == "text/html") { 276 267 string text; 277 268 try { 278 text = file_to_string(file);269 text = d.file_to_string(); 279 270 } catch (ReadError) { 280 271 cout << "can't read \"" << file << "\" - skipping" << endl; 281 272 return; … … 308 299 try { 309 300 // Currently we assume that text files are UTF-8 unless they have a 310 301 // byte-order mark. 311 dump = file_to_string(file);302 dump = d.file_to_string(); 312 303 md5_string(dump, md5); 313 304 314 305 // Look for Byte-Order Mark (BOM). … … 473 464 // FIXME: Implement support for metadata. 474 465 try { 475 466 XmlParser xmlparser; 476 string text = file_to_string(file);467 string text = d.file_to_string(); 477 468 xmlparser.parse_html(text); 478 469 dump = xmlparser.dump; 479 470 md5_string(text, md5); … … 577 568 try { 578 569 // Currently we assume that text files are UTF-8 unless they have a 579 570 // byte-order mark. 580 dump = file_to_string(file);571 dump = d.file_to_string(); 581 572 md5_string(dump, md5); 582 573 583 574 // Look for Byte-Order Mark (BOM). … … 686 677 sample = p.sample; 687 678 } else if (mimetype == "image/svg+xml") { 688 679 SvgParser svgparser; 689 svgparser.parse_html( file_to_string(file));680 svgparser.parse_html(d.file_to_string()); 690 681 dump = svgparser.dump; 691 682 title = svgparser.title; 692 683 keywords = svgparser.keywords; … … 718 709 } 719 710 720 711 // Compute the MD5 of the file if we haven't already. 721 if (md5.empty() && md5_file(file, md5 ) == 0) {712 if (md5.empty() && md5_file(file, md5, d.try_noatime()) == 0) { 722 713 cout << "failed to read file to calculate MD5 checksum - skipping" << endl; 723 714 return; 724 715 } -
scriptindex.cc
534 534 break; 535 535 case Action::LOAD: { 536 536 bool truncated = false; 537 if (!load_file(value, i->get_num_arg(), true, 537 // FIXME: Use NOATIME if we own the file or are root. 538 if (!load_file(value, i->get_num_arg(), NOCACHE, 538 539 value, truncated)) { 539 540 cerr << "Couldn't load file '" << value << "': " 540 541 << strerror(errno) << endl;