Ticket #222: omindex-noatime.patch

File omindex-noatime.patch, 4.7 KB (added by Olly Betts, 17 years ago)

Patch which adds O_NOATIME support

  • omindex.cc

     
    132132file_to_string(const string &file)
    133133{
    134134    string out;
    135     if (!load_file(file, out)) throw ReadError();
     135    if (!load_file(file, out, NOCACHE|NOATIME)) throw ReadError();
    136136    return out;
    137137}
    138138
  • 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
     
    4242# endif
    4343#endif
    4444
     45#ifndef O_NOATIME
     46# define O_NOATIME 0
     47#endif
     48
    4549#include "md5.h"
    4650#include "md5wrap.h"
    4751
     
    5155md5_file(const string &file_name, string &md5)
    5256{
    5357    mode_t mode = O_RDONLY;
    54     mode |= O_STREAMING;
     58    mode |= O_STREAMING|O_NOATIME;
    5559
    5660    int fd = open(file_name.c_str(), mode);
     61    if (fd < 0 && (mode & O_NOATIME)) {
     62        mode &= ~O_NOATIME;
     63        fd = open(file_name.c_str(), mode);
     64    }
    5765    if (fd < 0) return false;
    5866
    5967#ifdef HAVE_POSIX_FADVISE
  • scriptindex.cc

     
    505505                        break;
    506506                    case Action::LOAD: {
    507507                        bool truncated = false;
    508                         if (!load_file(value, i->get_num_arg(), true,
     508                        if (!load_file(value, i->get_num_arg(), NOCACHE|NOATIME,
    509509                                       value, truncated)) {
    510510                            cerr << "Couldn't load file '" << value << "': "
    511511                                 << strerror(errno) << endl;
  • 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#ifdef O_NOATIME
     59    if (flags & NOATIME) mode |= O_NOATIME;
     60#endif
    5961
    6062    int fd = open(file_name.c_str(), mode);
     63#ifdef O_NOATIME
     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