Ticket #222: omindex-noatime-updated.patch

File omindex-noatime-updated.patch, 8.8 KB (added by Olly Betts, 14 years ago)

Updated patch which checks the euid and file owner

  • diritor.cc

     
    2121
    2222#include "diritor.h"
    2323
     24#include "safeunistd.h"
     25#include <sys/types.h>
    2426#include "safeerrno.h"
    2527
    2628#include <cstring>
    2729
    2830using namespace std;
    2931
     32uid_t DirectoryIterator::euid = geteuid();
     33
    3034void
    3135DirectoryIterator::call_stat()
    3236{
  • diritor.h

     
    2424
    2525#include "safedirent.h"
    2626#include "safeerrno.h"
     27#include "safefcntl.h"
    2728#include "safesysstat.h"
    2829
    2930#include <sys/types.h>
     
    3233
    3334#include "common/noreturn.h"
    3435
     36#include "loadfile.h"
     37#include "runfilter.h" // For class ReadError.
     38
    3539class DirectoryIterator {
     40    static uid_t euid;
     41
    3642    std::string path;
    3743    DIR * dir;
    3844    struct dirent *entry;
     
    150156        ensure_statbuf_valid();
    151157        return (statbuf.st_mode & S_IROTH);
    152158    }
     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    }
    153180};
    154181
    155182#endif // OMEGA_INCLUDED_DIRITOR_H
  • loadfile.cc

     
    11/* loadfile.cc: load a file into a std::string.
    22 *
    3  * Copyright (C) 2006 Olly Betts
     3 * Copyright (C) 2006,2007 Olly Betts
    44 *
    55 * This program is free software; you can redistribute it and/or modify
    66 * it under the terms of the GNU General Public License as published by
     
    2626# endif
    2727#endif
    2828
     29#include "loadfile.h"
     30
    2931#include <algorithm>
    3032#include <string>
    3133
     
    3941# ifdef __linux__
    4042// This is the value used by rml's O_STREAMING patch for 2.4.
    4143#  define O_STREAMING   04000000
    42 # else
    43 // Define as 0 otherwise, so we don't need ifdefs in the code.
    44 #  define O_STREAMING   0
    4544# endif
    4645#endif
    4746
    48 #include "loadfile.h"
    49 
    5047using namespace std;
    5148
    5249bool
    53 load_file(const string &file_name, size_t max_to_read,
    54           bool try_not_to_cache,
     50load_file(const string &file_name, size_t max_to_read, int flags,
    5551          string &output, bool &truncated)
    5652{
     53    (void)flags; // Avoid possible "unused" warning.
    5754    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
    5961
    6062    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
    6169    if (fd < 0) return false;
    6270
    6371#ifdef HAVE_POSIX_FADVISE
    64     if (try_not_to_cache)
     72    if (flags & NOCACHE)
    6573        posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE); // or POSIX_FADV_SEQUENTIAL
    6674#endif
    6775
     
    97105    }
    98106
    99107#ifdef HAVE_POSIX_FADVISE
    100     if (try_not_to_cache)
     108    if (flags & NOCACHE)
    101109        posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
    102110#endif
    103111
  • loadfile.h

     
    11/* loadfile.h: load a file into a std::string.
    22 *
    3  * Copyright (C) 2006 Olly Betts
     3 * Copyright (C) 2006,2007 Olly Betts
    44 *
    55 * This program is free software; you can redistribute it and/or modify
    66 * it under the terms of the GNU General Public License as published by
     
    2222
    2323#include <string>
    2424
    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);
     25enum { NOCACHE = 0x1, NOATIME = 0x2 };
     26
     27bool load_file(const std::string &file_name, size_t max_to_read, int flags,
     28               std::string &output, bool &truncated);
    2829
    2930inline bool
    30 load_file(const std::string &file_name, std::string &output,
    31           bool try_not_to_cache = false)
     31load_file(const std::string &file_name, std::string &output, int flags = 0)
    3232{
    3333    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);
    3535}
    3636
    3737#endif // OMEGA_INCLUDED_LOADFILE_H
  • md5wrap.cc

     
    11/* md5wrap.cc: wrapper functions to allow easy use of MD5 from C++.
    22 *
    3  * Copyright (C) 2006 Olly Betts
     3 * Copyright (C) 2006,2007 Olly Betts
    44 *
    55 * This program is free software; you can redistribute it and/or modify
    66 * it under the terms of the GNU General Public License as published by
     
    4848using namespace std;
    4949
    5050bool
    51 md5_file(const string &file_name, string &md5)
     51md5_file(const string &file_name, string &md5, bool try_noatime)
    5252{
    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
    5559
    5660    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
    5767    if (fd < 0) return false;
    5868
    5969#ifdef HAVE_POSIX_FADVISE
  • md5wrap.h

     
    2222
    2323#include <string>
    2424
    25 bool md5_file(const std::string &file_name, std::string &md5);
     25bool md5_file(const std::string &file_name, std::string &md5, bool try_noatime);
    2626void md5_string(const std::string &str, std::string &md5);
    2727
    2828#endif // OMEGA_INCLUDED_MD5WRAP_H
  • omindex.cc

     
    4646#include "commonhelp.h"
    4747#include "diritor.h"
    4848#include "hashterm.h"
    49 #include "loadfile.h"
    5049#include "md5wrap.h"
    5150#include "metaxmlparse.h"
    5251#include "myhtmlparse.h"
     
    173172    return (p != NULL);
    174173}
    175174
    176 static string
    177 file_to_string(const string &file)
    178 {
    179     string out;
    180     if (!load_file(file, out)) throw ReadError();
    181     return out;
    182 }
    183 
    184175static void
    185176get_pdf_metainfo(const string & safefile, string &title, string &keywords)
    186177{
     
    275266    if (mimetype == "text/html") {
    276267        string text;
    277268        try {
    278             text = file_to_string(file);
     269            text = d.file_to_string();
    279270        } catch (ReadError) {
    280271            cout << "can't read \"" << file << "\" - skipping" << endl;
    281272            return;
     
    308299        try {
    309300            // Currently we assume that text files are UTF-8 unless they have a
    310301            // byte-order mark.
    311             dump = file_to_string(file);
     302            dump = d.file_to_string();
    312303            md5_string(dump, md5);
    313304
    314305            // Look for Byte-Order Mark (BOM).
     
    473464        // FIXME: Implement support for metadata.
    474465        try {
    475466            XmlParser xmlparser;
    476             string text = file_to_string(file);
     467            string text = d.file_to_string();
    477468            xmlparser.parse_html(text);
    478469            dump = xmlparser.dump;
    479470            md5_string(text, md5);
     
    577568        try {
    578569            // Currently we assume that text files are UTF-8 unless they have a
    579570            // byte-order mark.
    580             dump = file_to_string(file);
     571            dump = d.file_to_string();
    581572            md5_string(dump, md5);
    582573
    583574            // Look for Byte-Order Mark (BOM).
     
    686677        sample = p.sample;
    687678    } else if (mimetype == "image/svg+xml") {
    688679        SvgParser svgparser;
    689         svgparser.parse_html(file_to_string(file));
     680        svgparser.parse_html(d.file_to_string());
    690681        dump = svgparser.dump;
    691682        title = svgparser.title;
    692683        keywords = svgparser.keywords;
     
    718709    }
    719710
    720711    // 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) {
    722713        cout << "failed to read file to calculate MD5 checksum - skipping" << endl;
    723714        return;
    724715    }
  • scriptindex.cc

     
    534534                        break;
    535535                    case Action::LOAD: {
    536536                        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,
    538539                                       value, truncated)) {
    539540                            cerr << "Couldn't load file '" << value << "': "
    540541                                 << strerror(errno) << endl;