|
#ifndef UTIL_FILE_H |
|
#define UTIL_FILE_H |
|
|
|
#include "exception.hh" |
|
#include "scoped.hh" |
|
#include "string_piece.hh" |
|
|
|
#include <cstddef> |
|
#include <cstdio> |
|
#include <string> |
|
#include <stdint.h> |
|
|
|
namespace util { |
|
|
|
class scoped_fd { |
|
public: |
|
scoped_fd() : fd_(-1) {} |
|
|
|
explicit scoped_fd(int fd) : fd_(fd) {} |
|
|
|
~scoped_fd(); |
|
|
|
#if __cplusplus >= 201103L |
|
scoped_fd(scoped_fd &&from) noexcept : fd_(from.fd_) { |
|
from.fd_ = -1; |
|
} |
|
#endif |
|
|
|
void reset(int to = -1) { |
|
scoped_fd other(fd_); |
|
fd_ = to; |
|
} |
|
|
|
int get() const { return fd_; } |
|
|
|
int operator*() const { return fd_; } |
|
|
|
int release() { |
|
int ret = fd_; |
|
fd_ = -1; |
|
return ret; |
|
} |
|
|
|
private: |
|
int fd_; |
|
|
|
scoped_fd(const scoped_fd &); |
|
scoped_fd &operator=(const scoped_fd &); |
|
}; |
|
|
|
struct scoped_FILE_closer { |
|
static void Close(std::FILE *file); |
|
}; |
|
typedef scoped<std::FILE, scoped_FILE_closer> scoped_FILE; |
|
|
|
|
|
class FDException : public ErrnoException { |
|
public: |
|
explicit FDException(int fd) throw(); |
|
|
|
virtual ~FDException() throw(); |
|
|
|
|
|
int FD() const { return fd_; } |
|
|
|
|
|
const std::string &NameGuess() const { return name_guess_; } |
|
|
|
private: |
|
int fd_; |
|
|
|
std::string name_guess_; |
|
}; |
|
|
|
|
|
class EndOfFileException : public Exception { |
|
public: |
|
EndOfFileException() throw(); |
|
~EndOfFileException() throw(); |
|
}; |
|
|
|
class UnsupportedOSException : public Exception {}; |
|
|
|
|
|
int OpenReadOrThrow(const char *name); |
|
|
|
int CreateOrThrow(const char *name); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool InputPathIsStdin(StringPiece path); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool OutputPathIsStdout(StringPiece path); |
|
|
|
|
|
const uint64_t kBadSize = (uint64_t)-1; |
|
uint64_t SizeFile(int fd); |
|
uint64_t SizeOrThrow(int fd); |
|
|
|
void ResizeOrThrow(int fd, uint64_t to); |
|
|
|
|
|
|
|
void HolePunch(int fd, uint64_t offset, uint64_t size); |
|
|
|
std::size_t PartialRead(int fd, void *to, std::size_t size); |
|
void ReadOrThrow(int fd, void *to, std::size_t size); |
|
std::size_t ReadOrEOF(int fd, void *to_void, std::size_t size); |
|
|
|
void WriteOrThrow(int fd, const void *data_void, std::size_t size); |
|
void WriteOrThrow(FILE *to, const void *data, std::size_t size); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ErsatzPRead(int fd, void *to, std::size_t size, uint64_t off); |
|
void ErsatzPWrite(int fd, const void *data_void, std::size_t size, uint64_t off); |
|
|
|
void FSyncOrThrow(int fd); |
|
|
|
|
|
uint64_t SeekOrThrow(int fd, uint64_t off); |
|
uint64_t AdvanceOrThrow(int fd, int64_t off); |
|
uint64_t SeekEnd(int fd); |
|
|
|
std::FILE *FDOpenOrThrow(scoped_fd &file); |
|
std::FILE *FDOpenReadOrThrow(scoped_fd &file); |
|
|
|
|
|
|
|
void NormalizeTempPrefix(std::string &base); |
|
int MakeTemp(const StringPiece &prefix); |
|
std::FILE *FMakeTemp(const StringPiece &prefix); |
|
|
|
|
|
std::string DefaultTempDirectory(); |
|
|
|
|
|
int DupOrThrow(int fd); |
|
|
|
|
|
|
|
|
|
|
|
std::string NameFromFD(int fd); |
|
|
|
} |
|
|
|
#endif |
|
|