26 #if !defined(_MSC_VER) 33 #if !defined(O_BINARY) 42 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) 59 HANDLE h = FindFirstFile((path +
"\\*").c_str(), &ff);
61 if (h == INVALID_HANDLE_VALUE) {
66 std::vector<FileInfo> tmp_list;
69 if (ff.cFileName[0] !=
'.')
72 if (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
78 fi.
path = path +
"\\" + ff.cFileName;
79 fi.
size = (
static_cast<uint64_t
>(ff.nFileSizeHigh) * (MAXDWORD + 1))
80 + static_cast<uint64_t>(ff.nFileSizeLow);
81 tmp_list.emplace_back(fi);
83 }
while (FindNextFile(h, &ff) != 0);
85 DWORD e = GetLastError();
86 if (e != ERROR_NO_MORE_FILES) {
91 std::sort(tmp_list.begin(), tmp_list.end());
93 for (
const FileInfo& fi : tmp_list) {
98 filelist.emplace_back(fi);
104 DIR* dir = opendir(path.c_str());
111 std::vector<std::string> list;
115 if (de->d_name[0] ==
'.')
continue;
117 list.emplace_back(path +
"/" + de->d_name);
123 std::sort(list.begin(), list.end());
126 if (stat(entry.c_str(), &st) != 0)
129 if (S_ISDIR(st.st_mode)) {
133 else if (S_ISREG(st.st_mode)) {
137 fi.
size =
static_cast<uint64_t
>(st.st_size);
138 filelist.emplace_back(fi);
147 std::vector<std::string> list;
150 #if defined(_MSC_VER) 152 sglob.Add(path.c_str());
153 for (
int n = 0; n < sglob.FileCount(); ++n) {
154 list.emplace_back(sglob.File(n));
158 glob(path.c_str(), GLOB_TILDE,
nullptr, &glob_result);
160 for (
unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
161 list.push_back(glob_result.gl_pathv[i]);
163 globfree(&glob_result);
167 std::sort(list.begin(), list.end());
170 struct stat filestat;
173 if (::stat(file.c_str(), &filestat) != 0) {
174 die(
"ERROR: could not stat() path " + file);
177 if (S_ISREG(filestat.st_mode)) {
182 fi.
size =
static_cast<uint64_t
>(filestat.st_size);
183 filelist.emplace_back(fi);
193 filelist.emplace_back(fi);
209 static constexpr
bool debug =
false;
213 SysFile() : fd_(-1) { }
216 explicit SysFile(
int fd,
int pid = 0) noexcept
217 : fd_(fd), pid_(pid) { }
220 SysFile(
const SysFile&) =
delete;
222 SysFile& operator = (
const SysFile&) =
delete;
224 SysFile(SysFile&& f) noexcept
225 : fd_(f.fd_), pid_(f.pid_) {
226 f.fd_ = -1, f.pid_ = 0;
229 SysFile& operator = (SysFile&& f) {
231 fd_ = f.fd_, pid_ = f.pid_;
232 f.fd_ = -1, f.pid_ = 0;
241 ssize_t write(
const void* data,
size_t count)
final {
243 #if defined(_MSC_VER) 244 return ::_write(fd_, data, static_cast<unsigned>(count));
246 return ::write(fd_, data, count);
251 ssize_t read(
void* data,
size_t count)
final {
253 #if defined(_MSC_VER) 254 return ::_read(fd_, data, static_cast<unsigned>(count));
256 return ::read(fd_, data, count);
267 #if defined(_MSC_VER) 275 void SysFile::close() {
277 sLOG <<
"SysFile::close(): fd" << fd_;
278 if (::close(fd_) != 0)
280 LOG1 <<
"SysFile::close()" 282 <<
" errno=" << errno
283 <<
" error=" << strerror(errno);
287 #if !defined(_MSC_VER) 289 sLOG <<
"SysFile::close(): waitpid for" << pid_;
291 pid_t p = waitpid(pid_, &status, 0);
294 "SysFile: waitpid() failed to return child");
296 if (WIFEXITED(status)) {
298 if (WEXITSTATUS(status) != 0) {
300 "SysFile: child failed with return code " 307 else if (WIFSIGNALED(status)) {
309 "SysFile: child killed by signal " 314 "SysFile: child failed with an unknown error");
326 static constexpr
bool debug =
false;
330 int fd = ::open(path.c_str(), O_RDONLY |
O_BINARY, 0);
337 const char* decompressor;
343 decompressor =
"lzop";
346 decompressor =
"lz4";
352 sLOG <<
"SysFile::OpenForRead(): filefd" << fd;
356 ::lseek(fd, range.
begin, SEEK_CUR);
359 return tlx::make_counting<SysFile>(fd);
362 #if defined(_MSC_VER) 364 "Reading compressed files is not supported on windows, yet. " 365 "Please submit a patch.");
380 dup2(fd, STDIN_FILENO);
383 dup2(pipefd[1], STDOUT_FILENO);
386 execlp(decompressor, decompressor,
"-d",
nullptr);
388 LOG1 <<
"Pipe execution failed: " << strerror(errno);
397 sLOG <<
"SysFile::OpenForRead(): pipefd" << pipefd[0] <<
"to pid" << pid;
407 ::lseek(pipefd[0], range.
begin, SEEK_CUR);
410 return tlx::make_counting<SysFile>(pipefd[0], pid);
416 static constexpr
bool debug =
false;
420 int fd = ::open(path.c_str(), O_CREAT | O_WRONLY |
O_BINARY, 0666);
427 const char* compressor;
442 sLOG <<
"SysFile::OpenForWrite(): filefd" << fd;
444 return tlx::make_counting<SysFile>(fd);
447 #if defined(_MSC_VER) 449 "Reading compressed files is not supported on windows, yet. " 450 "Please submit a patch.");
465 dup2(pipefd[0], STDIN_FILENO);
468 dup2(fd, STDOUT_FILENO);
471 execlp(compressor, compressor,
nullptr);
473 LOG1 <<
"Pipe execution failed: " << strerror(errno);
482 sLOG <<
"SysFile::OpenForWrite(): pipefd" << pipefd[0] <<
"to pid" << pid;
490 return tlx::make_counting<SysFile>(pipefd[1], pid);
A cross-platform file globbing library providing the ability to expand wildcards in command-line argu...
Writer object to output data to any supported URI.
#define sLOG
Default logging method: output if the local debug variable is true.
Reader object from any source.
An Exception which is thrown on system errors.
GlobType
Type of objects to include in glob result.
An Exception which is thrown on system errors and contains errno information.
void PortSetCloseOnExec(int fd)
set FD_CLOEXEC on file descriptor (if possible)
represents a 1 dimensional range (interval) [begin,end)
#define CSimpleGlob
TCHAR version dependent on if _UNICODE is defined.
void MakePipe(int out_pipefds[2])
create a pair of pipe file descriptors
#define die(msg)
Instead of std::terminate(), throw the output the message via an exception.
General information of vfs file.
static by_string to_string(int val)
convert to string
uint64_t size
size of file.
bool ends_with(const char *str, const char *match)
Checks if the given match string is located at the end of this string.
struct dirent * ts_readdir(DIR *dirp)
mutex-locked readdir() call
WriteStreamPtr SysOpenWriteStream(const std::string &path)
Open file for writing and return file descriptor.
std::string path
path to file
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
static constexpr bool debug
void SysGlob(const std::string &path, const GlobType >ype, FileList &filelist)
Glob a path and augment the FileList with matching file names.
High-performance smart pointer used as a wrapping reference counting pointer.
ReadStreamPtr SysOpenReadStream(const std::string &path, const common::Range &range)
Open file for reading and return file descriptor.
List of file info and additional overall info.
static void SysGlobWalkRecursive(const std::string &path, FileList &filelist)