77 result = true; |
78 result = true; |
78 } |
79 } |
79 |
80 |
80 FindClose(handle); |
81 FindClose(handle); |
81 } |
82 } |
82 #endif //WINDOWS |
83 #endif // WINDOWS |
83 #ifdef POSIX |
84 #ifdef POSIX |
84 struct stat buf; |
85 struct stat buf; |
85 |
86 |
86 if ((stat(StringToFileSystemString(DirectoryName), &buf) == 0) && (S_ISDIR(buf.st_mode) != 0)) { |
87 if ((stat(StringToFileSystemString(DirectoryName), &buf) == 0) && |
|
88 (S_ISDIR(buf.st_mode) != 0)) { |
87 result = true; |
89 result = true; |
88 } |
90 } |
89 #endif //POSIX |
91 #endif // POSIX |
90 return result; |
92 return result; |
91 } |
93 } |
92 |
94 |
93 #ifdef WINDOWS |
95 #ifdef WINDOWS |
94 std::string GetLastErrorAsString() { |
96 std::string GetLastErrorAsString() { |
95 //Get the error message, if any. |
97 // Get the error message, if any. |
96 DWORD errorMessageID = ::GetLastError(); |
98 DWORD errorMessageID = ::GetLastError(); |
97 |
99 |
98 if (errorMessageID == 0) { |
100 if (errorMessageID == 0) { |
99 return "No error message has been recorded"; |
101 return "No error message has been recorded"; |
100 } |
102 } |
101 |
103 |
102 LPSTR messageBuffer = NULL; |
104 LPSTR messageBuffer = NULL; |
103 size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
105 size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
104 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); |
106 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
|
107 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, |
|
108 SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); |
105 |
109 |
106 std::string message(messageBuffer, size); |
110 std::string message(messageBuffer, size); |
107 |
111 |
108 // Free the buffer. |
112 // Free the buffer. |
109 LocalFree(messageBuffer); |
113 LocalFree(messageBuffer); |
110 |
114 |
111 return message; |
115 return message; |
112 } |
116 } |
113 #endif //WINDOWS |
117 #endif // WINDOWS |
114 |
118 |
115 bool FilePath::DeleteFile(const TString FileName) { |
119 bool FilePath::DeleteFile(const TString FileName) { |
116 bool result = false; |
120 bool result = false; |
117 |
121 |
118 if (FileExists(FileName) == true) { |
122 if (FileExists(FileName) == true) { |
143 #ifdef WINDOWS |
147 #ifdef WINDOWS |
144 SHFILEOPSTRUCTW fos = {0}; |
148 SHFILEOPSTRUCTW fos = {0}; |
145 TString directoryName = FixPathForPlatform(DirectoryName); |
149 TString directoryName = FixPathForPlatform(DirectoryName); |
146 DynamicBuffer<TCHAR> lDirectoryName(directoryName.size() + 2); |
150 DynamicBuffer<TCHAR> lDirectoryName(directoryName.size() + 2); |
147 memcpy(lDirectoryName.GetData(), directoryName.data(), (directoryName.size() + 2) * sizeof(TCHAR)); |
151 memcpy(lDirectoryName.GetData(), directoryName.data(), (directoryName.size() + 2) * sizeof(TCHAR)); |
148 lDirectoryName[directoryName.size() + 1] = NULL; // Double null terminate for SHFileOperation. |
152 lDirectoryName[directoryName.size() + 1] = NULL; |
|
153 // Double null terminate for SHFileOperation. |
149 |
154 |
150 // Delete the folder and everything inside. |
155 // Delete the folder and everything inside. |
151 fos.wFunc = FO_DELETE; |
156 fos.wFunc = FO_DELETE; |
152 fos.pFrom = lDirectoryName.GetData(); |
157 fos.pFrom = lDirectoryName.GetData(); |
153 fos.fFlags = FOF_NO_UI; |
158 fos.fFlags = FOF_NO_UI; |
154 result = SHFileOperation(&fos) == 0; |
159 result = SHFileOperation(&fos) == 0; |
155 #endif //WINDOWS |
160 #endif // WINDOWS |
156 #ifdef POSIX |
161 #ifdef POSIX |
157 if (unlink(StringToFileSystemString(DirectoryName)) == 0) { |
162 if (unlink(StringToFileSystemString(DirectoryName)) == 0) { |
158 result = true; |
163 result = true; |
159 } |
164 } |
160 #endif //POSIX |
165 #endif // POSIX |
161 } |
166 } |
162 |
167 |
163 return result; |
168 return result; |
164 } |
169 } |
165 |
170 |
166 TString FilePath::IncludeTrailingSeparater(const TString value) { |
171 TString FilePath::IncludeTrailingSeparator(const TString value) { |
167 TString result = value; |
172 TString result = value; |
168 |
173 |
169 if (value.size() > 0) { |
174 if (value.size() > 0) { |
170 TString::iterator i = result.end(); |
175 TString::iterator i = result.end(); |
171 i--; |
176 i--; |
176 } |
181 } |
177 |
182 |
178 return result; |
183 return result; |
179 } |
184 } |
180 |
185 |
181 TString FilePath::IncludeTrailingSeparater(const char* value) { |
186 TString FilePath::IncludeTrailingSeparator(const char* value) { |
182 TString lvalue = PlatformString(value).toString(); |
187 TString lvalue = PlatformString(value).toString(); |
183 return IncludeTrailingSeparater(lvalue); |
188 return IncludeTrailingSeparator(lvalue); |
184 } |
189 } |
185 |
190 |
186 TString FilePath::IncludeTrailingSeparater(const wchar_t* value) { |
191 TString FilePath::IncludeTrailingSeparator(const wchar_t* value) { |
187 TString lvalue = PlatformString(value).toString(); |
192 TString lvalue = PlatformString(value).toString(); |
188 return IncludeTrailingSeparater(lvalue); |
193 return IncludeTrailingSeparator(lvalue); |
189 } |
194 } |
190 |
195 |
191 TString FilePath::ExtractFilePath(TString Path) { |
196 TString FilePath::ExtractFilePath(TString Path) { |
192 #ifdef WINDOWS |
197 #ifdef WINDOWS |
193 TString result; |
198 TString result; |
194 size_t slash = Path.find_last_of(TRAILING_PATHSEPARATOR); |
199 size_t slash = Path.find_last_of(TRAILING_PATHSEPARATOR); |
195 if (slash != TString::npos) |
200 if (slash != TString::npos) |
196 result = Path.substr(0, slash); |
201 result = Path.substr(0, slash); |
197 return result; |
202 return result; |
198 #endif //WINDOWS |
203 #endif // WINDOWS |
199 #ifdef POSIX |
204 #ifdef POSIX |
200 return dirname(StringToFileSystemString(Path)); |
205 return dirname(StringToFileSystemString(Path)); |
201 #endif //POSIX |
206 #endif // POSIX |
202 } |
207 } |
203 |
208 |
204 TString FilePath::ExtractFileExt(TString Path) { |
209 TString FilePath::ExtractFileExt(TString Path) { |
205 TString result; |
210 TString result; |
206 size_t dot = Path.find_last_of('.'); |
211 size_t dot = Path.find_last_of('.'); |
540 DWORD attributes = ::GetFileAttributes(FFileName.data()); |
542 DWORD attributes = ::GetFileAttributes(FFileName.data()); |
541 |
543 |
542 if (attributes != INVALID_FILE_ATTRIBUTES) { |
544 if (attributes != INVALID_FILE_ATTRIBUTES) { |
543 result = true; |
545 result = true; |
544 |
546 |
545 if (attributes | FILE_ATTRIBUTE_ARCHIVE) { FAttributes.push_back(faArchive); } |
547 if (attributes | FILE_ATTRIBUTE_ARCHIVE) { |
546 if (attributes | FILE_ATTRIBUTE_COMPRESSED) { FAttributes.push_back(faCompressed); } |
548 FAttributes.push_back(faArchive); |
547 if (attributes | FILE_ATTRIBUTE_DEVICE) { FAttributes.push_back(faDevice); } |
549 } |
548 if (attributes | FILE_ATTRIBUTE_DIRECTORY) { FAttributes.push_back(faDirectory); } |
550 if (attributes | FILE_ATTRIBUTE_COMPRESSED) { |
549 if (attributes | FILE_ATTRIBUTE_ENCRYPTED) { FAttributes.push_back(faEncrypted); } |
551 FAttributes.push_back(faCompressed); |
550 if (attributes | FILE_ATTRIBUTE_HIDDEN) { FAttributes.push_back(faHidden); } |
552 } |
551 //if (attributes | FILE_ATTRIBUTE_INTEGRITY_STREAM) { FAttributes.push_back(faIntegrityStream); } |
553 if (attributes | FILE_ATTRIBUTE_DEVICE) { |
552 if (attributes | FILE_ATTRIBUTE_NORMAL) { FAttributes.push_back(faNormal); } |
554 FAttributes.push_back(faDevice); |
553 if (attributes | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) { FAttributes.push_back(faNotContentIndexed); } |
555 } |
554 //if (attributes | FILE_ATTRIBUTE_NO_SCRUB_DATA) { FAttributes.push_back(faNoScrubData); } |
556 if (attributes | FILE_ATTRIBUTE_DIRECTORY) { |
555 if (attributes | FILE_ATTRIBUTE_SYSTEM) { FAttributes.push_back(faSystem); } |
557 FAttributes.push_back(faDirectory); |
556 if (attributes | FILE_ATTRIBUTE_OFFLINE) { FAttributes.push_back(faOffline); } |
558 } |
557 if (attributes | FILE_ATTRIBUTE_REPARSE_POINT) { FAttributes.push_back(faSymbolicLink); } |
559 if (attributes | FILE_ATTRIBUTE_ENCRYPTED) { |
558 if (attributes | FILE_ATTRIBUTE_SPARSE_FILE) { FAttributes.push_back(faSparceFile); } |
560 FAttributes.push_back(faEncrypted); |
559 if (attributes | FILE_ATTRIBUTE_READONLY ) { FAttributes.push_back(faReadOnly); } |
561 } |
560 if (attributes | FILE_ATTRIBUTE_TEMPORARY) { FAttributes.push_back(faTemporary); } |
562 if (attributes | FILE_ATTRIBUTE_HIDDEN) { |
561 if (attributes | FILE_ATTRIBUTE_VIRTUAL) { FAttributes.push_back(faVirtual); } |
563 FAttributes.push_back(faHidden); |
|
564 } |
|
565 // if (attributes | FILE_ATTRIBUTE_INTEGRITY_STREAM) { |
|
566 // FAttributes.push_back(faIntegrityStream); |
|
567 // } |
|
568 if (attributes | FILE_ATTRIBUTE_NORMAL) { |
|
569 FAttributes.push_back(faNormal); |
|
570 } |
|
571 if (attributes | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) { |
|
572 FAttributes.push_back(faNotContentIndexed); |
|
573 } |
|
574 // if (attributes | FILE_ATTRIBUTE_NO_SCRUB_DATA) { |
|
575 // FAttributes.push_back(faNoScrubData); |
|
576 // } |
|
577 if (attributes | FILE_ATTRIBUTE_SYSTEM) { |
|
578 FAttributes.push_back(faSystem); |
|
579 } |
|
580 if (attributes | FILE_ATTRIBUTE_OFFLINE) { |
|
581 FAttributes.push_back(faOffline); |
|
582 } |
|
583 if (attributes | FILE_ATTRIBUTE_REPARSE_POINT) { |
|
584 FAttributes.push_back(faSymbolicLink); |
|
585 } |
|
586 if (attributes | FILE_ATTRIBUTE_SPARSE_FILE) { |
|
587 FAttributes.push_back(faSparceFile); |
|
588 } |
|
589 if (attributes | FILE_ATTRIBUTE_READONLY ) { |
|
590 FAttributes.push_back(faReadOnly); |
|
591 } |
|
592 if (attributes | FILE_ATTRIBUTE_TEMPORARY) { |
|
593 FAttributes.push_back(faTemporary); |
|
594 } |
|
595 if (attributes | FILE_ATTRIBUTE_VIRTUAL) { |
|
596 FAttributes.push_back(faVirtual); |
|
597 } |
562 } |
598 } |
563 #endif // WINDOWS |
599 #endif // WINDOWS |
564 #ifdef POSIX |
600 #ifdef POSIX |
565 struct stat status; |
601 struct stat status; |
566 |
602 |
567 if (stat(StringToFileSystemString(FFileName), &status) == 0) { |
603 if (stat(StringToFileSystemString(FFileName), &status) == 0) { |
568 result = true; |
604 result = true; |
569 |
605 |
570 if (S_ISBLK(status.st_mode) != 0) { FAttributes.push_back(faBlockSpecial); } |
606 if (S_ISBLK(status.st_mode) != 0) { |
571 if (S_ISCHR(status.st_mode) != 0) { FAttributes.push_back(faCharacterSpecial); } |
607 FAttributes.push_back(faBlockSpecial); |
572 if (S_ISFIFO(status.st_mode) != 0) { FAttributes.push_back(faFIFOSpecial); } |
608 } |
573 if (S_ISREG(status.st_mode) != 0) { FAttributes.push_back(faNormal); } |
609 if (S_ISCHR(status.st_mode) != 0) { |
574 if (S_ISDIR(status.st_mode) != 0) { FAttributes.push_back(faDirectory); } |
610 FAttributes.push_back(faCharacterSpecial); |
575 if (S_ISLNK(status.st_mode) != 0) { FAttributes.push_back(faSymbolicLink); } |
611 } |
576 if (S_ISSOCK(status.st_mode) != 0) { FAttributes.push_back(faSocket); } |
612 if (S_ISFIFO(status.st_mode) != 0) { |
|
613 FAttributes.push_back(faFIFOSpecial); |
|
614 } |
|
615 if (S_ISREG(status.st_mode) != 0) { |
|
616 FAttributes.push_back(faNormal); |
|
617 } |
|
618 if (S_ISDIR(status.st_mode) != 0) { |
|
619 FAttributes.push_back(faDirectory); |
|
620 } |
|
621 if (S_ISLNK(status.st_mode) != 0) { |
|
622 FAttributes.push_back(faSymbolicLink); |
|
623 } |
|
624 if (S_ISSOCK(status.st_mode) != 0) { |
|
625 FAttributes.push_back(faSocket); |
|
626 } |
577 |
627 |
578 // Owner |
628 // Owner |
579 if (S_ISRUSR(status.st_mode) != 0) { |
629 if (S_ISRUSR(status.st_mode) != 0) { |
580 if (S_ISWUSR(status.st_mode) != 0) { FAttributes.push_back(faReadWrite); } |
630 if (S_ISWUSR(status.st_mode) != 0) { |
581 else { FAttributes.push_back(faReadOnly); } |
631 FAttributes.push_back(faReadWrite); |
582 } |
632 } else { |
583 else if (S_ISWUSR(status.st_mode) != 0) { FAttributes.push_back(faWriteOnly); } |
633 FAttributes.push_back(faReadOnly); |
584 |
634 } |
585 if (S_ISXUSR(status.st_mode) != 0) { FAttributes.push_back(faExecute); } |
635 } else if (S_ISWUSR(status.st_mode) != 0) { |
|
636 FAttributes.push_back(faWriteOnly); |
|
637 } |
|
638 |
|
639 if (S_ISXUSR(status.st_mode) != 0) { |
|
640 FAttributes.push_back(faExecute); |
|
641 } |
586 |
642 |
587 // Group |
643 // Group |
588 if (S_ISRGRP(status.st_mode) != 0) { |
644 if (S_ISRGRP(status.st_mode) != 0) { |
589 if (S_ISWGRP(status.st_mode) != 0) { FAttributes.push_back(faGroupReadWrite); } |
645 if (S_ISWGRP(status.st_mode) != 0) { |
590 else { FAttributes.push_back(faGroupReadOnly); } |
646 FAttributes.push_back(faGroupReadWrite); |
591 } |
647 } else { |
592 else if (S_ISWGRP(status.st_mode) != 0) { FAttributes.push_back(faGroupWriteOnly); } |
648 FAttributes.push_back(faGroupReadOnly); |
593 |
649 } |
594 if (S_ISXGRP(status.st_mode) != 0) { FAttributes.push_back(faGroupExecute); } |
650 } else if (S_ISWGRP(status.st_mode) != 0) { |
|
651 FAttributes.push_back(faGroupWriteOnly); |
|
652 } |
|
653 |
|
654 if (S_ISXGRP(status.st_mode) != 0) { |
|
655 FAttributes.push_back(faGroupExecute); |
|
656 } |
595 |
657 |
596 |
658 |
597 // Others |
659 // Others |
598 if (S_ISROTH(status.st_mode) != 0) { |
660 if (S_ISROTH(status.st_mode) != 0) { |
599 if (S_ISWOTH(status.st_mode) != 0) { FAttributes.push_back(faOthersReadWrite); } |
661 if (S_ISWOTH(status.st_mode) != 0) { |
600 else { FAttributes.push_back(faOthersReadOnly); } |
662 FAttributes.push_back(faOthersReadWrite); |
601 } |
663 } else { |
602 else if (S_ISWOTH(status.st_mode) != 0) { FAttributes.push_back(faOthersWriteOnly); } |
664 FAttributes.push_back(faOthersReadOnly); |
603 |
665 } |
604 if (S_ISXOTH(status.st_mode) != 0) { FAttributes.push_back(faOthersExecute); } |
666 } |
|
667 else if (S_ISWOTH(status.st_mode) != 0) { |
|
668 FAttributes.push_back(faOthersWriteOnly); |
|
669 } |
|
670 |
|
671 if (S_ISXOTH(status.st_mode) != 0) { |
|
672 FAttributes.push_back(faOthersExecute); |
|
673 } |
605 |
674 |
606 if (FFileName.size() > 0 && FFileName[0] == '.') { |
675 if (FFileName.size() > 0 && FFileName[0] == '.') { |
607 FAttributes.push_back(faHidden); |
676 FAttributes.push_back(faHidden); |
608 } |
677 } |
609 } |
678 } |
610 #endif //POSIX |
679 #endif // POSIX |
611 |
680 |
612 return result; |
681 return result; |
613 } |
682 } |
614 |
683 |
615 bool FileAttributes::Valid(const FileAttribute Value) { |
684 bool FileAttributes::Valid(const FileAttribute Value) { |