Эх сурвалжийг харах

Better directory copy logic, present error message instead of crashing if can't copy project template

Josh Engebretson 9 жил өмнө
parent
commit
1e0ef04fc1

+ 5 - 1
Script/AtomicEditor/ui/modal/CreateProject.ts

@@ -100,7 +100,11 @@ class CreateProject extends ModalWindow {
             // Do the creation!
             if (templateDetail && fileSystem.dirExists(templateDetail.folder)) {
 
-                fileSystem.copyDir(templateDetail.folder, folder);
+                if (!fileSystem.copyDir(templateDetail.folder, folder)) {
+                  var message = "Unable to copy folder: " + templateDetail.folder + " to " + folder;
+                  EditorUI.showModalError("New Project Editor Error", message);
+                  return false;
+                }
 
                 var utils = new Editor.FileUtils();
 

+ 46 - 14
Source/Atomic/IO/FileSystem.cpp

@@ -908,29 +908,56 @@ bool FileSystem::CreateDirs(const String& root, const String& subdirectory)
 
 }
 
-bool FileSystem::CreateDirsRecursive(const String& directoryIn, const String& directoryOut)
+bool FileSystem::CreateDirsRecursive(const String& directoryIn)
 {
-    if (!CreateDir(directoryOut))
+    String directory = AddTrailingSlash(GetInternalPath(directoryIn));
+
+    if (DirExists(directory))
+        return true;
+
+    if (FileExists(directory))
         return false;
 
-    Vector<String> results;
-    ScanDir(results, directoryIn, "*", SCAN_DIRS, false );
+    String parentPath = directory;
 
-    while (results.Remove(".")) {}
-    while (results.Remove("..")) {}
+    Vector<String> paths;
 
-    for (unsigned i = 0; i < results.Size(); i++)
+    paths.Push(directory);
+
+    while (true)
+    {
+        parentPath = GetParentPath(parentPath);
+
+        if (!parentPath.Length())
+            break;
+
+        paths.Push(parentPath);
+    }
+
+    if (!paths.Size())
+        return false;
+
+    for (int i = (int) (paths.Size() - 1); i >= 0; i--)
     {
-        String dirIn = directoryIn + "/" + results[i];
-        String dirOut = directoryOut + "/" + results[i];
+        const String& pathName = paths[i];
 
-        //LOGINFOF("SRC: %s, DST: %s", dirIn.CString(), dirOut.CString());
+        if (FileExists(pathName))
+            return false;
+
+        if (DirExists(pathName))
+            continue;
 
-        if (!CreateDirsRecursive(dirIn, dirOut))
+        if (!CreateDir(pathName))
             return false;
+
+        // double check
+        if (!DirExists(pathName))
+            return false;
+
     }
 
     return true;
+
 }
 
 bool FileSystem::CopyDir(const String& directoryIn, const String& directoryOut)
@@ -938,8 +965,6 @@ bool FileSystem::CopyDir(const String& directoryIn, const String& directoryOut)
     if (FileExists(directoryOut) || DirExists(directoryOut))
         return false;
 
-    CreateDirsRecursive(directoryIn, directoryOut);
-
     Vector<String> results;
     ScanDir(results, directoryIn, "*", SCAN_FILES, true );
 
@@ -947,8 +972,15 @@ bool FileSystem::CopyDir(const String& directoryIn, const String& directoryOut)
     {
         String srcFile = directoryIn + "/" + results[i];
         String dstFile = directoryOut + "/" + results[i];
+
+        String dstPath = GetPath(dstFile);
+
+        if (!CreateDirsRecursive(dstPath))
+            return false;
+
         //LOGINFOF("SRC: %s DST: %s", srcFile.CString(), dstFile.CString());
-        Copy(srcFile, dstFile);
+        if (!Copy(srcFile, dstFile))
+            return false;
     }
 
     return true;

+ 1 - 1
Source/Atomic/IO/FileSystem.h

@@ -113,7 +113,7 @@ public:
 	/// Check if a file or directory exists at the specified path
     bool Exists(const String& pathName) const { return FileExists(pathName) || DirExists(pathName); }
 
-    bool CreateDirsRecursive(const String& directoryIn, const String& directoryOut);
+    bool CreateDirsRecursive(const String& directoryIn);
     
 private:
     /// Scan directory, called internally.