file.hh (3483B)
1 #ifndef file_hh_INCLUDED 2 #define file_hh_INCLUDED 3 4 #include "array.hh" 5 #include "array_view.hh" 6 #include "enum.hh" 7 #include "exception.hh" 8 #include "meta.hh" 9 #include "string.hh" 10 #include "units.hh" 11 #include "utils.hh" 12 13 #include <sys/types.h> 14 #include <sys/stat.h> 15 #include <cstring> 16 #include <exception> 17 18 namespace Kakoune 19 { 20 21 struct file_access_error : runtime_error 22 { 23 file_access_error(StringView filename, StringView error_desc); 24 file_access_error(int fd, StringView error_desc); 25 }; 26 27 // parse ~/ and %/ in filename and returns the translated filename 28 String parse_filename(StringView filename, StringView buf_dir = {}); 29 30 String real_path(StringView filename); 31 String compact_path(StringView filename); 32 33 StringView tmpdir(); 34 StringView homedir(); 35 36 // returns pair { directory, filename } 37 std::pair<StringView, StringView> split_path(StringView path); 38 39 String get_kak_binary_path(); 40 41 bool fd_readable(int fd); 42 bool fd_writable(int fd); 43 String read_fd(int fd, bool text = false); 44 String read_file(StringView filename, bool text = false); 45 template<bool force_blocking = false> 46 void write(int fd, StringView data); 47 void write_to_file(StringView filename, StringView data); 48 int create_file(const char* filename); 49 int open_temp_file(StringView filename); 50 int open_temp_file(StringView filename, char (&buffer)[PATH_MAX]); 51 52 struct MappedFile 53 { 54 MappedFile(StringView filename); 55 ~MappedFile(); 56 57 operator StringView() const; 58 59 const char* data; 60 struct stat st {}; 61 }; 62 63 enum class WriteMethod 64 { 65 Overwrite, 66 Replace 67 }; 68 constexpr auto enum_desc(Meta::Type<WriteMethod>) 69 { 70 return make_array<EnumDesc<WriteMethod>>({ 71 { WriteMethod::Overwrite, "overwrite" }, 72 { WriteMethod::Replace, "replace" }, 73 }); 74 } 75 76 String find_file(StringView filename, StringView buf_dir, ConstArrayView<String> paths); 77 bool file_exists(StringView filename); 78 bool regular_file_exists(StringView filename); 79 80 void list_files(StringView directory, FunctionRef<void (StringView, const struct stat&)> callback); 81 82 void make_directory(StringView dir, mode_t mode); 83 84 constexpr timespec InvalidTime = { -1, -1 }; 85 86 struct FsStatus 87 { 88 timespec timestamp; 89 ByteCount file_size; 90 size_t hash; 91 }; 92 93 timespec get_fs_timestamp(StringView filename); 94 FsStatus get_fs_status(StringView filename); 95 96 constexpr bool operator==(const timespec& lhs, const timespec& rhs) 97 { 98 return lhs.tv_sec == rhs.tv_sec and lhs.tv_nsec == rhs.tv_nsec; 99 } 100 101 template<bool atomic, int buffer_size = 4096> 102 struct BufferedWriter 103 { 104 BufferedWriter(int fd) 105 : m_fd{fd}, m_exception_count{std::uncaught_exceptions()} {} 106 107 ~BufferedWriter() noexcept(false) 108 { 109 if (m_pos != 0 and m_exception_count == std::uncaught_exceptions()) 110 flush(); 111 } 112 113 void write(StringView data) 114 { 115 while (not data.empty()) 116 { 117 const ByteCount length = data.length(); 118 const ByteCount write_len = clamp(length, ByteCount{0}, size - m_pos); 119 memcpy(m_buffer + m_pos, data.data(), (int)write_len); 120 m_pos += write_len; 121 if (m_pos == size) 122 flush(); 123 data = data.substr(write_len); 124 } 125 } 126 127 void flush() 128 { 129 Kakoune::write<atomic>(m_fd, {m_buffer, m_pos}); 130 m_pos = 0; 131 } 132 133 private: 134 static constexpr ByteCount size = buffer_size; 135 int m_fd; 136 int m_exception_count; 137 ByteCount m_pos = 0; 138 char m_buffer[(int)size]; 139 }; 140 141 } 142 143 #endif // file_hh_INCLUDED