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