From 90fbede55256cb672a67d8181b4f0435047d4e13 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 24 Nov 2018 14:44:21 +0200 Subject: Add fdtruncate(), fdseek(), file position to [io]fdstream ctors --- libbutl/fdstream.cxx | 66 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 15 deletions(-) (limited to 'libbutl/fdstream.cxx') diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx index 72439ae..26d5da3 100644 --- a/libbutl/fdstream.cxx +++ b/libbutl/fdstream.cxx @@ -11,7 +11,7 @@ #ifndef _WIN32 # include // open(), O_*, fcntl() # include // close(), read(), write(), lseek(), dup(), pipe(), - // isatty(), ssize_t, STD*_FILENO + // ftruncate(), isatty(), ssize_t, STD*_FILENO # include // writev(), iovec # include // stat(), S_I* # include // stat, off_t @@ -19,9 +19,10 @@ # include # include // _close(), _read(), _write(), _setmode(), _sopen(), - // _lseek(), _dup(), _pipe(), _get_osfhandle() + // _lseek(), _dup(), _pipe(), _chsize_s, + // _get_osfhandle() # include // _SH_DENYNO -# include // _fileno(), stdin, stdout, stderr +# include // _fileno(), stdin, stdout, stderr, SEEK_* # include // _O_* # include // S_I* @@ -95,15 +96,8 @@ namespace butl // fdbuf // - fdbuf:: - fdbuf (auto_fd&& fd) - { - if (fd.get () >= 0) - open (move (fd)); - } - void fdbuf:: - open (auto_fd&& fd) + open (auto_fd&& fd, uint64_t pos) { close (); @@ -118,7 +112,7 @@ namespace butl setg (buf_, buf_, buf_); setp (buf_, buf_ + sizeof (buf_) - 1); // Keep space for overflow's char. - off_ = 0; // @@ Strictly speaking, need to query, can be at end. + off_ = pos; fd_ = move (fd); } @@ -457,8 +451,8 @@ namespace butl // fdstream_base // fdstream_base:: - fdstream_base (auto_fd&& fd, fdstream_mode m) - : fdstream_base (mode (move (fd), m)) // Delegate. + fdstream_base (auto_fd&& fd, fdstream_mode m, std::uint64_t pos) + : fdstream_base (mode (move (fd), m), pos) { } @@ -635,8 +629,9 @@ namespace butl open (fdopen (f, m | fdopen_mode::out)); } - // Utility functions + // fd*() functions // + auto_fd fdopen (const char* f, fdopen_mode m, permissions p) { @@ -788,6 +783,33 @@ namespace butl return auto_fd (fd); } + uint64_t + fdseek (int fd, uint64_t o, fdseek_mode fdm) + { + int m (-1); + + switch (fdm) + { + case fdseek_mode::set: m = SEEK_SET; break; + case fdseek_mode::cur: m = SEEK_CUR; break; + case fdseek_mode::end: m = SEEK_END; break; + } + +#ifndef _WIN32 + off_t r (lseek (fd, static_cast (o), m)); + if (r == static_cast (-1)) + throw_generic_ios_failure (errno); +#else + __int64 r (_lseeki64 (fd, static_cast<__int64> (o), m)); + if (r == -1) + throw_generic_ios_failure (errno); +#endif + + return static_cast (r); + } + + // The rest is platform-specific. + // #ifndef _WIN32 auto_fd @@ -927,6 +949,13 @@ namespace butl return r; } + void + fdtruncate (int fd, uint64_t n) + { + if (ftruncate (fd, static_cast (n)) != 0) + throw_generic_ios_failure (errno); + } + bool fdterm (int fd) { @@ -1128,6 +1157,13 @@ namespace butl return {auto_fd (pd[0]), auto_fd (pd[1])}; } + void + fdtruncate (int fd, uint64_t n) + { + if (errno_t e = _chsize_s (fd, static_cast<__int64> (n))) + throw_generic_ios_failure (e); + } + bool fdterm (int fd) { -- cgit v1.1