dirut.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/system/sysstat.h>
  4. #include <util/system/fs.h>
  5. #include <util/generic/string.h>
  6. #include <util/generic/yexception.h>
  7. #include <sys/types.h>
  8. #include <cerrno>
  9. #include <cstdlib>
  10. #ifdef _win32_
  11. #include <util/system/winint.h>
  12. #include <direct.h>
  13. #include <malloc.h>
  14. #include <time.h>
  15. #include <io.h>
  16. #include "dirent_win.h"
  17. // these live in mktemp_system.cpp
  18. extern "C" int mkstemps(char* path, int slen);
  19. char* mkdtemp(char* path);
  20. #else
  21. #ifdef _sun_
  22. #include <alloca.h>
  23. char* mkdtemp(char* path);
  24. #endif
  25. #include <unistd.h>
  26. #include <pwd.h>
  27. #include <dirent.h>
  28. #ifndef DT_DIR
  29. #include <sys/stat.h>
  30. #endif
  31. #endif
  32. bool IsDir(const TString& path);
  33. int mkpath(char* path, int mode = 0777);
  34. TString GetHomeDir();
  35. void MakeDirIfNotExist(const char* path, int mode = 0777);
  36. inline void MakeDirIfNotExist(const TString& path, int mode = 0777) {
  37. MakeDirIfNotExist(path.data(), mode);
  38. }
  39. /// Create path making parent directories as needed
  40. void MakePathIfNotExist(const char* path, int mode = 0777);
  41. void SlashFolderLocal(TString& folder);
  42. bool correctpath(TString& filename);
  43. bool resolvepath(TString& folder, const TString& home);
  44. char GetDirectorySeparator();
  45. const char* GetDirectorySeparatorS();
  46. void RemoveDirWithContents(TString dirName);
  47. const char* GetFileNameComponent(const char* f);
  48. inline TString GetFileNameComponent(const TString& f) {
  49. return GetFileNameComponent(f.data());
  50. }
  51. /// RealPath doesn't guarantee trailing separator to be stripped or left in place for directories.
  52. TString RealPath(const TString& path); // throws
  53. TString RealLocation(const TString& path); /// throws; last file name component doesn't need to exist
  54. TString GetSystemTempDir();
  55. int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix);
  56. int ResolvePath(const char* rel, const char* abs, char res[/*FILENAME_MAX*/], bool isdir = false);
  57. TString ResolvePath(const char* rel, const char* abs, bool isdir = false);
  58. TString ResolvePath(const char* path, bool isDir = false);
  59. TString ResolveDir(const char* path);
  60. bool SafeResolveDir(const char* path, TString& result);
  61. TString GetDirName(const TString& path);
  62. TString GetBaseName(const TString& path);
  63. TString StripFileComponent(const TString& fileName);
  64. class TExistenceChecker {
  65. public:
  66. TExistenceChecker(bool strict = false)
  67. : Strict(strict)
  68. {
  69. }
  70. void SetStrict(bool strict) {
  71. Strict = strict;
  72. }
  73. bool IsStrict() const {
  74. return Strict;
  75. }
  76. const char* Check(const char* fname) const {
  77. if (!fname || !*fname)
  78. return nullptr;
  79. if (Strict) {
  80. NFs::EnsureExists(fname);
  81. } else if (!NFs::Exists(fname))
  82. fname = nullptr;
  83. return fname;
  84. }
  85. private:
  86. bool Strict;
  87. };