1 // Windows/FileDir.h 2 3 #ifndef __WINDOWS_FILE_DIR_H 4 #define __WINDOWS_FILE_DIR_H 5 6 #include "../Common/MyString.h" 7 8 #include "FileIO.h" 9 10 namespace NWindows { 11 namespace NFile { 12 namespace NDir { 13 14 bool GetWindowsDir(FString &path); 15 bool GetSystemDir(FString &path); 16 17 /* 18 WIN32 API : SetFileTime() doesn't allow to set zero timestamps in file 19 but linux : allows unix time = 0 in filesystem 20 */ 21 22 bool SetDirTime(CFSTR path, const CFiTime *cTime, const CFiTime *aTime, const CFiTime *mTime); 23 24 25 #ifdef _WIN32 26 27 bool SetFileAttrib(CFSTR path, DWORD attrib); 28 29 /* 30 Some programs store posix attributes in high 16 bits of windows attributes field. 31 Also some programs use additional flag markers: 0x8000 or 0x4000. 32 SetFileAttrib_PosixHighDetect() tries to detect posix field, and it extracts only attribute 33 bits that are related to current system only. 34 */ 35 #else 36 37 int my_chown(CFSTR path, uid_t owner, gid_t group); 38 39 #endif 40 41 bool SetFileAttrib_PosixHighDetect(CFSTR path, DWORD attrib); 42 43 44 bool MyMoveFile(CFSTR existFileName, CFSTR newFileName); 45 46 #ifndef UNDER_CE 47 bool MyCreateHardLink(CFSTR newFileName, CFSTR existFileName); 48 #endif 49 50 bool RemoveDir(CFSTR path); 51 bool CreateDir(CFSTR path); 52 53 /* CreateComplexDir returns true, if directory can contain files after the call (two cases): 54 1) the directory already exists (network shares and drive paths are supported) 55 2) the directory was created 56 path can be WITH or WITHOUT trailing path separator. */ 57 58 bool CreateComplexDir(CFSTR path); 59 60 bool DeleteFileAlways(CFSTR name); 61 bool RemoveDirWithSubItems(const FString &path); 62 63 bool MyGetFullPathName(CFSTR path, FString &resFullPath); 64 bool GetFullPathAndSplit(CFSTR path, FString &resDirPrefix, FString &resFileName); 65 bool GetOnlyDirPrefix(CFSTR path, FString &resDirPrefix); 66 67 #ifndef UNDER_CE 68 69 bool SetCurrentDir(CFSTR path); 70 bool GetCurrentDir(FString &resultPath); 71 72 #endif 73 74 bool MyGetTempPath(FString &resultPath); 75 76 class CTempFile MY_UNCOPYABLE 77 { 78 bool _mustBeDeleted; 79 FString _path; DisableDeleting()80 void DisableDeleting() { _mustBeDeleted = false; } 81 public: CTempFile()82 CTempFile(): _mustBeDeleted(false) {} ~CTempFile()83 ~CTempFile() { Remove(); } GetPath()84 const FString &GetPath() const { return _path; } 85 bool Create(CFSTR pathPrefix, NIO::COutFile *outFile); // pathPrefix is not folder prefix 86 bool CreateRandomInTempFolder(CFSTR namePrefix, NIO::COutFile *outFile); 87 bool Remove(); 88 bool MoveTo(CFSTR name, bool deleteDestBefore); 89 }; 90 91 92 #ifdef _WIN32 93 class CTempDir MY_UNCOPYABLE 94 { 95 bool _mustBeDeleted; 96 FString _path; 97 public: CTempDir()98 CTempDir(): _mustBeDeleted(false) {} ~CTempDir()99 ~CTempDir() { Remove(); } GetPath()100 const FString &GetPath() const { return _path; } DisableDeleting()101 void DisableDeleting() { _mustBeDeleted = false; } 102 bool Create(CFSTR namePrefix) ; 103 bool Remove(); 104 }; 105 #endif 106 107 108 #if !defined(UNDER_CE) 109 class CCurrentDirRestorer MY_UNCOPYABLE 110 { 111 FString _path; 112 public: 113 bool NeedRestore; 114 CCurrentDirRestorer()115 CCurrentDirRestorer(): NeedRestore(true) 116 { 117 GetCurrentDir(_path); 118 } ~CCurrentDirRestorer()119 ~CCurrentDirRestorer() 120 { 121 if (!NeedRestore) 122 return; 123 FString s; 124 if (GetCurrentDir(s)) 125 if (s != _path) 126 SetCurrentDir(_path); 127 } 128 }; 129 #endif 130 131 }}} 132 133 #endif 134