00001 00004 /* Copyright (C) 2006,2007,2008,2009,2011 Olly Betts 00005 * Copyright (C) 2010 Richard Boulton 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #ifndef XAPIAN_INCLUDED_IO_UTILS_H 00023 #define XAPIAN_INCLUDED_IO_UTILS_H 00024 00025 #include <sys/types.h> 00026 #include "safefcntl.h" 00027 #include "safeunistd.h" 00028 #include <string> 00029 00035 inline bool io_sync(int fd) 00036 { 00037 #ifdef F_FULLFSYNC 00038 /* Only supported on Mac OS X (at the time of writing at least). 00039 * 00040 * This call ensures that data has actually been written to disk, not just 00041 * to the drive's write cache, so it provides better protection from power 00042 * failures, etc. It does take longer though. 00043 * 00044 * According to the sqlite sources, this shouldn't fail on a local FS so 00045 * a failure means that the file system doesn't support this operation and 00046 * therefore it's best to fallback to fdatasync()/fsync(). 00047 */ 00048 if (fcntl(fd, F_FULLFSYNC, 0) == 0) 00049 return true; 00050 #endif 00051 00052 #if defined HAVE_FDATASYNC 00053 // If we have it, prefer fdatasync() over fsync() as the former avoids 00054 // updating the access time so is probably a little more efficient. 00055 return fdatasync(fd) == 0; 00056 #elif defined HAVE_FSYNC 00057 return fsync(fd) == 0; 00058 #elif defined __WIN32__ 00059 return _commit(fd) == 0; 00060 #else 00061 # error Cannot implement io_sync() without fdatasync(), fsync(), or _commit() 00062 #endif 00063 } 00064 00072 size_t io_read(int fd, char * p, size_t n, size_t min); 00073 00075 void io_write(int fd, const char * p, size_t n); 00076 00089 bool io_unlink(const std::string & filename); 00090 00091 #endif // XAPIAN_INCLUDED_IO_UTILS_H