file.cc (12829B)
1 #include "file.hh" 2 3 #include "assert.hh" 4 #include "event_manager.hh" 5 #include "string.hh" 6 #include "string_utils.hh" 7 #include "format.hh" 8 #include "ranges.hh" 9 10 #include <limits> 11 #include <cerrno> 12 #include <cstdlib> 13 #include <cstdio> 14 #include <cstring> 15 #include <dirent.h> 16 #include <fcntl.h> 17 #include <pwd.h> 18 #include <sys/mman.h> 19 #include <sys/select.h> 20 #include <unistd.h> 21 22 #if defined(__FreeBSD__) or defined(__NetBSD__) 23 #include <sys/sysctl.h> 24 #endif 25 26 #if defined(__APPLE__) 27 #include <mach-o/dyld.h> 28 #define st_mtim st_mtimespec 29 #endif 30 31 #if defined(__HAIKU__) 32 #include <app/Application.h> 33 #include <app/Roster.h> 34 #include <storage/Path.h> 35 #endif 36 37 namespace Kakoune 38 { 39 40 file_access_error::file_access_error(StringView filename, 41 StringView error_desc) 42 : runtime_error(format("{}: {}", filename, error_desc)) {} 43 44 file_access_error::file_access_error(int fd, StringView error_desc) 45 : runtime_error(format("fd {}: {}", fd, error_desc)) {} 46 47 String parse_filename(StringView filename, StringView buf_dir) 48 { 49 auto prefix = filename.substr(0_byte, 2_byte); 50 if (prefix == "~" or prefix == "~/") 51 return homedir() + filename.substr(1_byte); 52 if ((prefix == "%" or prefix == "%/") and not buf_dir.empty()) 53 return buf_dir + filename.substr(1_byte); 54 return filename.str(); 55 } 56 57 std::pair<StringView, StringView> split_path(StringView path) 58 { 59 auto it = find(path | reverse(), '/'); 60 if (it == path.rend()) 61 return { {}, path }; 62 const char* slash = it.base()-1; 63 return { {path.begin(), slash+1}, {slash+1, path.end()} }; 64 } 65 66 String real_path(StringView filename) 67 { 68 if (filename.empty()) 69 return {}; 70 71 char buffer[PATH_MAX+1]; 72 73 StringView existing = filename; 74 StringView non_existing{}; 75 76 while (true) 77 { 78 if (char* res = realpath(existing.zstr(), buffer)) 79 { 80 if (non_existing.empty()) 81 return res; 82 83 StringView dir = res; 84 while (not dir.empty() and dir.back() == '/') 85 dir = dir.substr(0_byte, dir.length()-1_byte); 86 return format("{}/{}", dir, non_existing); 87 } 88 89 auto it = find(existing.rbegin() + 1, existing.rend(), '/'); 90 if (it == existing.rend()) 91 { 92 char cwd[1024]; 93 return format("{}/{}", getcwd(cwd, 1024), filename); 94 } 95 96 existing = StringView{existing.begin(), it.base()}; 97 non_existing = StringView{it.base(), filename.end()}; 98 } 99 } 100 101 String compact_path(StringView filename) 102 { 103 String real_filename = real_path(filename); 104 105 char cwd[1024]; 106 if (not ::getcwd(cwd, 1024)) 107 throw runtime_error(format("unable to get the current working directory (errno: {})", ::strerror(errno))); 108 109 String real_cwd = real_path(cwd) + "/"; 110 if (prefix_match(real_filename, real_cwd)) 111 return real_filename.substr(real_cwd.length()).str(); 112 113 StringView home = homedir(); 114 while (not home.empty() and home.back() == '/') 115 home = home.substr(0_byte, home.length()-1_byte); 116 117 if (not home.empty()) 118 { 119 ByteCount home_len = home.length(); 120 if (real_filename.substr(0, home_len) == home) 121 return "~" + real_filename.substr(home_len); 122 } 123 124 return filename.str(); 125 } 126 127 StringView tmpdir() 128 { 129 StringView tmpdir = getenv("TMPDIR"); 130 if (not tmpdir.empty()) 131 return tmpdir.back() == '/' ? tmpdir.substr(0_byte, tmpdir.length()-1) 132 : tmpdir; 133 return "/tmp"; 134 } 135 136 StringView homedir() 137 { 138 StringView home = getenv("HOME"); 139 if (home.empty()) 140 return getpwuid(geteuid())->pw_dir; 141 return home; 142 } 143 144 bool fd_readable(int fd) 145 { 146 kak_assert(fd >= 0); 147 fd_set rfds; 148 FD_ZERO(&rfds); 149 FD_SET(fd, &rfds); 150 151 timeval tv{0,0}; 152 return select(fd+1, &rfds, nullptr, nullptr, &tv) == 1; 153 } 154 155 bool fd_writable(int fd) 156 { 157 kak_assert(fd >= 0); 158 fd_set wfds; 159 FD_ZERO(&wfds); 160 FD_SET(fd, &wfds); 161 162 timeval tv{0,0}; 163 return select(fd+1, nullptr, &wfds, nullptr, &tv) == 1; 164 } 165 166 String read_fd(int fd, bool text) 167 { 168 String content; 169 constexpr size_t bufsize = 256; 170 char buf[bufsize]; 171 while (ssize_t size = read(fd, buf, bufsize)) 172 { 173 if (size == -1) 174 throw file_access_error{fd, strerror(errno)}; 175 176 if (text) 177 { 178 for (StringView data{buf, buf + size}; not data.empty();) 179 { 180 auto it = find(data, '\r'); 181 content += StringView{data.begin(), it}; 182 data = StringView{(it != data.end()) ? it+1 : it, data.end()}; 183 } 184 } 185 else 186 content += StringView{buf, buf + size}; 187 } 188 return content; 189 } 190 191 String read_file(StringView filename, bool text) 192 { 193 int fd = open(filename.zstr(), O_RDONLY); 194 if (fd == -1) 195 throw file_access_error(filename, strerror(errno)); 196 197 auto close_fd = OnScopeEnd([fd]{ close(fd); }); 198 return read_fd(fd, text); 199 } 200 201 MappedFile::MappedFile(StringView filename) 202 : data{nullptr} 203 { 204 int fd = open(filename.zstr(), O_RDONLY | O_NONBLOCK); 205 if (fd == -1) 206 throw file_access_error(filename, strerror(errno)); 207 auto close_fd = OnScopeEnd([&] { close(fd); }); 208 209 fstat(fd, &st); 210 if (S_ISDIR(st.st_mode)) 211 throw file_access_error(filename, "is a directory"); 212 213 if (st.st_size == 0) 214 return; 215 216 data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 217 if (data == MAP_FAILED) 218 throw file_access_error{filename, strerror(errno)}; 219 } 220 221 MappedFile::~MappedFile() 222 { 223 if (data != nullptr) 224 munmap((void*)data, st.st_size); 225 } 226 227 MappedFile::operator StringView() const 228 { 229 if (st.st_size > std::numeric_limits<int>::max()) 230 throw runtime_error("file is too big"); 231 return { data, (int)st.st_size }; 232 } 233 234 bool file_exists(StringView filename) 235 { 236 struct stat st; 237 return stat(filename.zstr(), &st) == 0; 238 } 239 240 bool regular_file_exists(StringView filename) 241 { 242 struct stat st; 243 return stat(filename.zstr(), &st) == 0 and 244 (st.st_mode & S_IFMT) == S_IFREG; 245 } 246 247 template<bool atomic> 248 void write(int fd, StringView data) 249 { 250 const char* ptr = data.data(); 251 ssize_t count = (int)data.length(); 252 253 int flags = fcntl(fd, F_GETFL, 0); 254 if (not atomic and EventManager::has_instance()) 255 fcntl(fd, F_SETFL, flags | O_NONBLOCK); 256 auto restore_flags = OnScopeEnd([&] { fcntl(fd, F_SETFL, flags); }); 257 258 while (count) 259 { 260 if (ssize_t written = ::write(fd, ptr, count); written != -1) 261 { 262 ptr += written; 263 count -= written; 264 } 265 else if (errno == EAGAIN and not atomic and EventManager::has_instance()) 266 EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, std::chrono::nanoseconds{}); 267 else 268 throw file_access_error(fd, strerror(errno)); 269 } 270 } 271 template void write<true>(int fd, StringView data); 272 template void write<false>(int fd, StringView data); 273 274 int create_file(const char* filename) 275 { 276 int fd; 277 const int flags = O_CREAT | O_WRONLY | O_TRUNC | (EventManager::has_instance() ? O_NONBLOCK : 0); 278 while ((fd = open(filename, flags, 0644)) == -1) 279 { 280 if (errno == ENXIO and EventManager::has_instance()) // trying to open a FIFO with no readers yet 281 EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, 282 std::chrono::nanoseconds{1'000'000}); 283 else 284 return -1; 285 } 286 return fd; 287 } 288 289 void write_to_file(StringView filename, StringView data) 290 { 291 int fd = create_file(filename.zstr()); 292 if (fd == -1) 293 throw file_access_error(filename, strerror(errno)); 294 auto close_fd = OnScopeEnd([fd]{ close(fd); }); 295 write(fd, data); 296 } 297 298 int open_temp_file(StringView filename, char (&buffer)[PATH_MAX]) 299 { 300 String path = real_path(filename); 301 auto [dir,file] = split_path(path); 302 303 if (dir.empty()) 304 format_to(buffer, ".{}.kak.XXXXXX", file); 305 else 306 format_to(buffer, "{}/.{}.kak.XXXXXX", dir, file); 307 308 return mkstemp(buffer); 309 } 310 311 int open_temp_file(StringView filename) 312 { 313 char buffer[PATH_MAX]; 314 return open_temp_file(filename, buffer); 315 } 316 317 String find_file(StringView filename, StringView buf_dir, ConstArrayView<String> paths) 318 { 319 struct stat buf; 320 if (filename.substr(0_byte, 1_byte) == "/") 321 { 322 if (stat(filename.zstr(), &buf) == 0 and S_ISREG(buf.st_mode)) 323 return filename.str(); 324 return ""; 325 } 326 if (filename.substr(0_byte, 2_byte) == "~/") 327 { 328 String candidate = homedir() + filename.substr(1_byte); 329 if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode)) 330 return candidate; 331 return ""; 332 } 333 334 for (auto candidate : paths | transform([&](StringView s) { return parse_filename(s, buf_dir); })) 335 { 336 if (not candidate.empty() and candidate.back() != '/') 337 candidate += '/'; 338 candidate += filename; 339 if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode)) 340 return candidate; 341 } 342 return ""; 343 } 344 345 void make_directory(StringView dir, mode_t mode) 346 { 347 auto it = dir.begin(), end = dir.end(); 348 while(it != end) 349 { 350 it = std::find(it+1, end, '/'); 351 struct stat st; 352 StringView dirname{dir.begin(), it}; 353 if (stat(dirname.zstr(), &st) == 0) 354 { 355 if (not S_ISDIR(st.st_mode)) 356 throw runtime_error(format("cannot make directory, '{}' exists but is not a directory", dirname)); 357 } 358 else 359 { 360 auto old_mask = umask(0); 361 auto restore_mask = OnScopeEnd([old_mask]() { umask(old_mask); }); 362 363 if (mkdir(dirname.zstr(), mode) != 0) 364 throw runtime_error(format("mkdir failed for directory '{}' errno {}", dirname, errno)); 365 } 366 } 367 } 368 369 void list_files(StringView dirname, FunctionRef<void (StringView, const struct stat&)> callback) 370 { 371 char buffer[PATH_MAX+1]; 372 format_to(buffer, "{}", dirname); 373 DIR* dir = opendir(dirname.empty() ? "./" : buffer); 374 if (not dir) 375 return; 376 377 auto close_dir = OnScopeEnd([dir]{ closedir(dir); }); 378 379 while (dirent* entry = readdir(dir)) 380 { 381 StringView filename = entry->d_name; 382 if (filename.empty()) 383 continue; 384 385 struct stat st; 386 auto fmt_str = (dirname.empty() or dirname.back() == '/') ? "{}{}" : "{}/{}"; 387 format_to(buffer, fmt_str, dirname, filename); 388 if (stat(buffer, &st) != 0) 389 continue; 390 391 if (S_ISDIR(st.st_mode)) 392 filename = format_to(buffer, "{}/", filename); 393 callback(filename, st); 394 } 395 } 396 397 timespec get_fs_timestamp(StringView filename) 398 { 399 struct stat st; 400 if (stat(filename.zstr(), &st) != 0) 401 return InvalidTime; 402 return st.st_mtim; 403 } 404 405 FsStatus get_fs_status(StringView filename) 406 { 407 MappedFile fd{filename}; 408 409 return {fd.st.st_mtim, fd.st.st_size, murmur3(fd.data, fd.st.st_size)}; 410 } 411 412 String get_kak_binary_path() 413 { 414 char buffer[2048]; 415 #if defined(__linux__) or defined(__CYGWIN__) or defined(__gnu_hurd__) 416 ssize_t res = readlink("/proc/self/exe", buffer, 2048); 417 if (res != -1 and res < 2048) { 418 buffer[res] = '\0'; 419 return buffer; 420 } 421 #elif defined(__FreeBSD__) or defined(__NetBSD__) 422 #if defined(__FreeBSD__) 423 int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; 424 #elif defined(__NetBSD__) 425 int mib[] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; 426 #endif 427 size_t res = sizeof(buffer); 428 if (sysctl(mib, 4, buffer, &res, NULL, 0) != -1) 429 return buffer; 430 #elif defined(__APPLE__) 431 uint32_t bufsize = 2048; 432 char* canonical_path = NULL; 433 if (_NSGetExecutablePath(buffer, &bufsize) != -1) 434 canonical_path = realpath(buffer, nullptr); 435 if (canonical_path) { 436 String path = canonical_path; 437 free(canonical_path); 438 return path; 439 } 440 #elif defined(__HAIKU__) 441 BApplication app("application/x-vnd.kakoune"); 442 app_info info; 443 if (app.GetAppInfo(&info) == B_OK) { 444 BPath path(&info.ref); 445 return path.Path(); 446 } 447 #elif defined(__DragonFly__) 448 ssize_t res = readlink("/proc/curproc/file", buffer, 2048); 449 if (res != -1 and res < 2048) { 450 buffer[res] = '\0'; 451 return buffer; 452 } 453 #elif defined(__OpenBSD__) 454 (void)buffer; 455 return KAK_BIN_PATH; 456 #elif defined(__sun__) 457 ssize_t res = readlink("/proc/self/path/a.out", buffer, 2048); 458 if (res != -1 and res < 2048) { 459 buffer[res] = '\0'; 460 return buffer; 461 } 462 #else 463 # error "finding executable path is not implemented on this platform" 464 #endif 465 throw runtime_error("unable to get the executable path"); 466 } 467 468 }