00001 #ifndef UTIL_FILE__
00002 #define UTIL_FILE__
00003
00004 #include "util/exception.hh"
00005
00006 #include <cstddef>
00007 #include <cstdio>
00008 #include <string>
00009
00010 #include <stdint.h>
00011
00012 namespace util {
00013
00014 class scoped_fd {
00015 public:
00016 scoped_fd() : fd_(-1) {}
00017
00018 explicit scoped_fd(int fd) : fd_(fd) {}
00019
00020 ~scoped_fd();
00021
00022 void reset(int to = -1) {
00023 scoped_fd other(fd_);
00024 fd_ = to;
00025 }
00026
00027 int get() const { return fd_; }
00028
00029 int operator*() const { return fd_; }
00030
00031 int release() {
00032 int ret = fd_;
00033 fd_ = -1;
00034 return ret;
00035 }
00036
00037 private:
00038 int fd_;
00039
00040 scoped_fd(const scoped_fd &);
00041 scoped_fd &operator=(const scoped_fd &);
00042 };
00043
00044 class scoped_FILE {
00045 public:
00046 explicit scoped_FILE(std::FILE *file = NULL) : file_(file) {}
00047
00048 ~scoped_FILE();
00049
00050 std::FILE *get() { return file_; }
00051 const std::FILE *get() const { return file_; }
00052
00053 void reset(std::FILE *to = NULL) {
00054 scoped_FILE other(file_);
00055 file_ = to;
00056 }
00057
00058 std::FILE *release() {
00059 std::FILE *ret = file_;
00060 file_ = NULL;
00061 return ret;
00062 }
00063
00064 private:
00065 std::FILE *file_;
00066 };
00067
00068
00069 class FDException : public ErrnoException {
00070 public:
00071 explicit FDException(int fd) throw();
00072
00073 virtual ~FDException() throw();
00074
00075
00076 int FD() const { return fd_; }
00077
00078
00079 const std::string &NameGuess() const { return name_guess_; }
00080
00081 private:
00082 int fd_;
00083
00084 std::string name_guess_;
00085 };
00086
00087
00088 class EndOfFileException : public Exception {
00089 public:
00090 EndOfFileException() throw();
00091 ~EndOfFileException() throw();
00092 };
00093
00094
00095 int OpenReadOrThrow(const char *name);
00096
00097 int CreateOrThrow(const char *name);
00098
00099
00100 const uint64_t kBadSize = (uint64_t)-1;
00101 uint64_t SizeFile(int fd);
00102 uint64_t SizeOrThrow(int fd);
00103
00104 void ResizeOrThrow(int fd, uint64_t to);
00105
00106 std::size_t PartialRead(int fd, void *to, std::size_t size);
00107 void ReadOrThrow(int fd, void *to, std::size_t size);
00108 std::size_t ReadOrEOF(int fd, void *to_void, std::size_t size);
00109
00110 void PReadOrThrow(int fd, void *to, std::size_t size, uint64_t off);
00111
00112 void WriteOrThrow(int fd, const void *data_void, std::size_t size);
00113 void WriteOrThrow(FILE *to, const void *data, std::size_t size);
00114
00115 void FSyncOrThrow(int fd);
00116
00117
00118 void SeekOrThrow(int fd, uint64_t off);
00119 void AdvanceOrThrow(int fd, int64_t off);
00120 void SeekEnd(int fd);
00121
00122 std::FILE *FDOpenOrThrow(scoped_fd &file);
00123 std::FILE *FDOpenReadOrThrow(scoped_fd &file);
00124
00125
00126
00127 void NormalizeTempPrefix(std::string &base);
00128 int MakeTemp(const std::string &prefix);
00129 std::FILE *FMakeTemp(const std::string &prefix);
00130
00131
00132 int DupOrThrow(int fd);
00133
00134
00135
00136
00137
00138 std::string NameFromFD(int fd);
00139
00140 }
00141
00142 #endif // UTIL_FILE__