summaryrefslogtreecommitdiffstats
path: root/src/OSSupport
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-09-26 22:54:18 +0200
committerMattes D <github@xoft.cz>2015-09-26 22:54:18 +0200
commitad57ce5f9940ab2776d9a3ad6dafe86b07d23058 (patch)
tree0cbe86c533f1d5caea5fa00b32a2a395590dbf59 /src/OSSupport
parentMerge pull request #2496 from SafwatHalaby/compileCommitId (diff)
downloadcuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.tar
cuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.tar.gz
cuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.tar.bz2
cuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.tar.lz
cuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.tar.xz
cuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.tar.zst
cuberite-ad57ce5f9940ab2776d9a3ad6dafe86b07d23058.zip
Diffstat (limited to 'src/OSSupport')
-rw-r--r--src/OSSupport/File.cpp206
-rw-r--r--src/OSSupport/File.h75
2 files changed, 186 insertions, 95 deletions
diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp
index 09f6147b2..dec873ccd 100644
--- a/src/OSSupport/File.cpp
+++ b/src/OSSupport/File.cpp
@@ -16,11 +16,7 @@
cFile::cFile(void) :
- #ifdef USE_STDIO_FILE
m_File(nullptr)
- #else
- m_File(INVALID_HANDLE_VALUE)
- #endif // USE_STDIO_FILE
{
// Nothing needed yet
}
@@ -30,11 +26,7 @@ cFile::cFile(void) :
cFile::cFile(const AString & iFileName, eMode iMode) :
- #ifdef USE_STDIO_FILE
m_File(nullptr)
- #else
- m_File(INVALID_HANDLE_VALUE)
- #endif // USE_STDIO_FILE
{
Open(iFileName, iMode);
}
@@ -78,11 +70,11 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
return false;
}
-#ifdef _WIN32
- m_File = _fsopen((FILE_IO_PREFIX + iFileName).c_str(), Mode, _SH_DENYWR);
-#else
- m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), Mode);
-#endif // _WIN32
+ #ifdef _WIN32
+ m_File = _fsopen((FILE_IO_PREFIX + iFileName).c_str(), Mode, _SH_DENYWR);
+ #else
+ m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), Mode);
+ #endif // _WIN32
if ((m_File == nullptr) && (iMode == fmReadWrite))
{
@@ -91,11 +83,11 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
// So now we know either the file doesn't exist or we don't have rights, no need to worry about file contents.
// Simply re-open for read-writing, erasing existing contents:
-#ifdef _WIN32
- m_File = _fsopen((FILE_IO_PREFIX + iFileName).c_str(), "wb+", _SH_DENYWR);
-#else
- m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), "wb+");
-#endif // _WIN32
+ #ifdef _WIN32
+ m_File = _fsopen((FILE_IO_PREFIX + iFileName).c_str(), "wb+", _SH_DENYWR);
+ #else
+ m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), "wb+");
+ #endif // _WIN32
}
return (m_File != nullptr);
@@ -310,7 +302,77 @@ bool cFile::Exists(const AString & a_FileName)
-bool cFile::Delete(const AString & a_FileName)
+bool cFile::Delete(const AString & a_Path)
+{
+ if (IsFolder(a_Path))
+ {
+ return DeleteFolder(a_Path);
+ }
+ else
+ {
+ return DeleteFile(a_Path);
+ }
+}
+
+
+
+
+
+bool cFile::DeleteFolder(const AString & a_FolderName)
+{
+ #ifdef _WIN32
+ return (RemoveDirectoryA(a_FolderName.c_str()) != 0);
+ #else // _WIN32
+ return (rmdir(a_FolderName.c_str()) == 0);
+ #endif // else _WIN32
+}
+
+
+
+
+
+bool cFile::DeleteFolderContents(const AString & a_FolderName)
+{
+ auto Contents = cFile::GetFolderContents(a_FolderName);
+ for (const auto item: Contents)
+ {
+ // Skip "." and ".." altogether:
+ if ((item == ".") || (item == ".."))
+ {
+ continue;
+ }
+
+ // Remove the item:
+ auto WholePath = a_FolderName + GetPathSeparator() + item;
+ if (IsFolder(WholePath))
+ {
+ if (!DeleteFolderContents(WholePath))
+ {
+ return false;
+ }
+ if (!DeleteFolder(WholePath))
+ {
+ return false;
+ }
+ }
+ else
+ {
+ if (!DeleteFile(WholePath))
+ {
+ return false;
+ }
+ }
+ } // for item - Contents[]
+
+ // All deletes succeeded
+ return true;
+}
+
+
+
+
+
+bool cFile::DeleteFile(const AString & a_FileName)
{
return (remove(a_FileName.c_str()) == 0);
}
@@ -331,7 +393,7 @@ bool cFile::Rename(const AString & a_OrigFileName, const AString & a_NewFileName
bool cFile::Copy(const AString & a_SrcFileName, const AString & a_DstFileName)
{
#ifdef _WIN32
- return (CopyFileA(a_SrcFileName.c_str(), a_DstFileName.c_str(), true) != 0);
+ return (CopyFileA(a_SrcFileName.c_str(), a_DstFileName.c_str(), FALSE) != 0);
#else
// Other OSs don't have a direct CopyFile equivalent, do it the harder way:
std::ifstream src(a_SrcFileName.c_str(), std::ios::binary);
@@ -409,57 +471,85 @@ bool cFile::CreateFolder(const AString & a_FolderPath)
+bool cFile::CreateFolderRecursive(const AString & a_FolderPath)
+{
+ // Special case: Fail if the path is empty
+ if (a_FolderPath.empty())
+ {
+ return false;
+ }
+
+ // Go through each path element and create the folder:
+ auto len = a_FolderPath.length();
+ auto PathSep = GetPathSeparator()[0];
+ for (decltype(len) i = 0; i < len; i++)
+ {
+ if (a_FolderPath[i] == PathSep)
+ {
+ CreateFolder(a_FolderPath.substr(0, i));
+ }
+ }
+ CreateFolder(a_FolderPath);
+
+ // Check the result by querying whether the final path exists:
+ return IsFolder(a_FolderPath);
+}
+
+
+
+
+
AStringVector cFile::GetFolderContents(const AString & a_Folder)
{
AStringVector AllFiles;
#ifdef _WIN32
- // If the folder name doesn't contain the terminating slash / backslash, add it:
- AString FileFilter = a_Folder;
- if (
- !FileFilter.empty() &&
- (FileFilter[FileFilter.length() - 1] != '\\') &&
- (FileFilter[FileFilter.length() - 1] != '/')
- )
- {
- FileFilter.push_back('\\');
- }
+ // If the folder name doesn't contain the terminating slash / backslash, add it:
+ AString FileFilter = a_Folder;
+ if (
+ !FileFilter.empty() &&
+ (FileFilter[FileFilter.length() - 1] != '\\') &&
+ (FileFilter[FileFilter.length() - 1] != '/')
+ )
+ {
+ FileFilter.push_back('\\');
+ }
- // Find all files / folders:
- FileFilter.append("*.*");
- HANDLE hFind;
- WIN32_FIND_DATAA FindFileData;
- if ((hFind = FindFirstFileA(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE)
- {
- do
+ // Find all files / folders:
+ FileFilter.append("*.*");
+ HANDLE hFind;
+ WIN32_FIND_DATAA FindFileData;
+ if ((hFind = FindFirstFileA(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE)
{
- AllFiles.push_back(FindFileData.cFileName);
- } while (FindNextFileA(hFind, &FindFileData));
- FindClose(hFind);
- }
+ do
+ {
+ AllFiles.push_back(FindFileData.cFileName);
+ } while (FindNextFileA(hFind, &FindFileData));
+ FindClose(hFind);
+ }
#else // _WIN32
- DIR * dp;
- struct dirent *dirp;
- AString Folder = a_Folder;
- if (Folder.empty())
- {
- Folder = ".";
- }
- if ((dp = opendir(Folder.c_str())) == nullptr)
- {
- LOGERROR("Error (%i) opening directory \"%s\"\n", errno, Folder.c_str());
- }
- else
- {
- while ((dirp = readdir(dp)) != nullptr)
+ DIR * dp;
+ struct dirent *dirp;
+ AString Folder = a_Folder;
+ if (Folder.empty())
{
- AllFiles.push_back(dirp->d_name);
+ Folder = ".";
+ }
+ if ((dp = opendir(Folder.c_str())) == nullptr)
+ {
+ LOGERROR("Error (%i) opening directory \"%s\"\n", errno, Folder.c_str());
+ }
+ else
+ {
+ while ((dirp = readdir(dp)) != nullptr)
+ {
+ AllFiles.push_back(dirp->d_name);
+ }
+ closedir(dp);
}
- closedir(dp);
- }
#endif // else _WIN32
diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h
index b8381ac0e..aab86811d 100644
--- a/src/OSSupport/File.h
+++ b/src/OSSupport/File.h
@@ -32,17 +32,6 @@ For reading entire files into memory, just use the static cFile::ReadWholeFile()
-#ifndef _WIN32
- #define USE_STDIO_FILE
-#endif // _WIN32
-
-// DEBUG:
-#define USE_STDIO_FILE
-
-
-
-
-
// tolua_begin
class cFile
@@ -101,48 +90,64 @@ public:
/** Reads the file from current position till EOF into an AString; returns the number of bytes read or -1 for error */
int ReadRestOfFile(AString & a_Contents);
- // tolua_begin
-
/** Returns true if the file specified exists */
- static bool Exists(const AString & a_FileName);
+ static bool Exists(const AString & a_FileName); // Exported in ManualBindings.cpp
+
+ /** Deletes a file or a folder, returns true if successful.
+ Prefer to use DeleteFile or DeleteFolder, since those don't have the penalty of checking whether a_Path is a folder. */
+ static bool Delete(const AString & a_Path); // Exported in ManualBindings.cpp
+
+ /** Deletes a file, returns true if successful.
+ Returns false if a_FileName points to a folder. */
+ static bool DeleteFile(const AString & a_FileName); // Exported in ManualBindings.cpp
- /** Deletes a file, returns true if successful */
- static bool Delete(const AString & a_FileName);
+ /** Deletes a folder, returns true if successful.
+ Returns false if a_FolderName points to a file. */
+ static bool DeleteFolder(const AString & a_FolderName); // Exported in ManualBindings.cpp
+
+ /** Deletes all content from the specified folder.
+ The specified folder itself stays intact.
+ Returns true on success, false on failure. */
+ static bool DeleteFolderContents(const AString & a_FolderName); // Exported in ManualBindings.cpp
/** Renames a file or folder, returns true if successful. May fail if dest already exists (libc-dependant)! */
- static bool Rename(const AString & a_OrigPath, const AString & a_NewPath);
+ static bool Rename(const AString & a_OrigPath, const AString & a_NewPath); // Exported in ManualBindings.cpp
- /** Copies a file, returns true if successful. */
- static bool Copy(const AString & a_SrcFileName, const AString & a_DstFileName);
+ /** Copies a file, returns true if successful.
+ Overwrites the dest file if it already exists. */
+ static bool Copy(const AString & a_SrcFileName, const AString & a_DstFileName); // Exported in ManualBindings.cpp
/** Returns true if the specified path is a folder */
- static bool IsFolder(const AString & a_Path);
+ static bool IsFolder(const AString & a_Path); // Exported in ManualBindings.cpp
/** Returns true if the specified path is a regular file */
- static bool IsFile(const AString & a_Path);
+ static bool IsFile(const AString & a_Path); // Exported in ManualBindings.cpp
/** Returns the size of the file, or a negative number on error */
- static long GetSize(const AString & a_FileName);
+ static long GetSize(const AString & a_FileName); // Exported in ManualBindings.cpp
/** Creates a new folder with the specified name. Returns true if successful. Path may be relative or absolute */
- static bool CreateFolder(const AString & a_FolderPath);
-
- // tolua_end
+ static bool CreateFolder(const AString & a_FolderPath); // Exported in ManualBindings.cpp
- /** Returns the entire contents of the specified file as a string. Returns empty string on error.
- Exported manually in ManualBindings.cpp due to #1914 - ToLua code doesn't work well with binary files. */
- static AString ReadWholeFile(const AString & a_FileName);
-
- // tolua_begin
+ /** Creates a new folder with the specified name, creating its parents if needed. Path may be relative or absolute.
+ Returns true if the folder exists at the end of the operation (either created, or already existed).
+ Supports only paths that use the path separator used by the current platform (MSVC CRT supports slashes for file paths, too, but this function doesn't) */
+ static bool CreateFolderRecursive(const AString & a_FolderPath); // Exported in ManualBindings.cpp
+
+ /** Returns the entire contents of the specified file as a string. Returns empty string on error. */
+ static AString ReadWholeFile(const AString & a_FileName); // Exported in ManualBindings.cpp
/** Returns a_FileName with its extension changed to a_NewExt.
a_FileName may contain path specification. */
- static AString ChangeFileExt(const AString & a_FileName, const AString & a_NewExt);
+ static AString ChangeFileExt(const AString & a_FileName, const AString & a_NewExt); // Exported in ManualBindings.cpp
/** Returns the last modification time (in current timezone) of the specified file.
The value returned is in the same units as the value returned by time() function.
- If the file is not found / accessible, zero is returned. */
- static unsigned GetLastModificationTime(const AString & a_FileName);
+ If the file is not found / accessible, zero is returned.
+ Works for folders, too, when specified without the trailing path separator. */
+ static unsigned GetLastModificationTime(const AString & a_FileName); // Exported in ManualBindings.cpp
+
+ // tolua_begin
/** Returns the path separator used by the current platform.
Note that the platform / CRT may support additional path separators (such as slashes on Windows), these don't get reported. */
@@ -162,11 +167,7 @@ public:
void Flush(void);
private:
- #ifdef USE_STDIO_FILE
FILE * m_File;
- #else
- HANDLE m_File;
- #endif
} ; // tolua_export