1 /* 2 * Copyright (C) 2018 Matthieu Gautier <mgautier@kymeria.fr> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 of the 7 * License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied 11 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and 12 * NON-INFRINGEMENT. See the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 */ 19 20 #ifndef ZIM_FS_UNIX_H_ 21 #define ZIM_FS_UNIX_H_ 22 23 #include "zim_types.h" 24 25 #include <stdexcept> 26 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <fcntl.h> 30 #include <unistd.h> 31 #include <dirent.h> 32 33 namespace zim { 34 35 namespace unix { 36 37 using path_t = const std::string&; 38 39 class FD { 40 public: 41 using fd_t = int; 42 43 private: 44 fd_t m_fd = -1; 45 46 public: 47 FD() = default; FD(fd_t fd)48 FD(fd_t fd): 49 m_fd(fd) {}; 50 FD(const FD& o) = delete; FD(FD && o)51 FD(FD&& o) : 52 m_fd(o.m_fd) { o.m_fd = -1; } 53 FD& operator=(FD&& o) { 54 m_fd = o.m_fd; 55 o.m_fd = -1; 56 return *this; 57 } ~FD()58 ~FD() { close(); } 59 zsize_t readAt(char* dest, zsize_t size, offset_t offset) const; 60 zsize_t getSize() const; getNativeHandle()61 fd_t getNativeHandle() const 62 { 63 return m_fd; 64 } release()65 fd_t release() 66 { 67 int ret = m_fd; 68 m_fd = -1; 69 return ret; 70 } 71 bool seek(offset_t offset); 72 bool close(); 73 }; 74 75 struct FS { 76 using FD = zim::unix::FD; 77 static std::string join(path_t base, path_t name); 78 static FD openFile(path_t filepath); 79 static bool makeDirectory(path_t path); 80 static void rename(path_t old_path, path_t new_path); 81 static bool remove(path_t path); 82 static bool removeDir(path_t path); 83 static bool removeFile(path_t path); 84 }; 85 86 }; // unix namespace 87 88 }; // zim namespace 89 90 #endif //ZIM_FS_UNIX_H_ 91