瀏覽代碼

more safari fixes

David Rose 16 年之前
父節點
當前提交
cd1af67165
共有 2 個文件被更改,包括 36 次插入13 次删除
  1. 2 2
      direct/src/plugin/find_root_dir.cxx
  2. 34 11
      direct/src/plugin_npapi/ppInstance.cxx

+ 2 - 2
direct/src/plugin/find_root_dir.cxx

@@ -33,7 +33,7 @@
 string
 string
 find_root_dir() {
 find_root_dir() {
 #ifdef _WIN32
 #ifdef _WIN32
-  // e.g., c:/Documents and Settings/<username>/Panda3D
+  // e.g., c:/Documents and Settings/<username>/Application Data/Panda3D
 
 
   char buffer[MAX_PATH];
   char buffer[MAX_PATH];
   if (SHGetSpecialFolderPath(NULL, buffer, CSIDL_APPDATA, true)) {
   if (SHGetSpecialFolderPath(NULL, buffer, CSIDL_APPDATA, true)) {
@@ -85,7 +85,7 @@ find_root_dir() {
 
 
 #endif
 #endif
 
 
-  // Couldn't find a directory.  Bail.
+  // Couldn't find a directory.  Punt.
   return ".";
   return ".";
 }
 }
 
 

+ 34 - 11
direct/src/plugin_npapi/ppInstance.cxx

@@ -348,18 +348,41 @@ stream_as_file(NPStream *stream, const char *fname) {
       }
       }
     }
     }
     filename = fname2;
     filename = fname2;
-  }
 
 
-  // Here's another temporary hack.  In addition to the weird filename
-  // format, the file that Safari tells us about appears to be a
-  // temporary file that Safari's about to delete.  In order to
-  // protect ourselves from this, we need to either open the file
-  // immediately, or copy it somewhere else.  The instance_data
-  // filename can't be copied, so in the short term, we implement this
-  // quick hack: if we're just downloading from "file://", then remap
-  // the filename to point to the source file.
-  if (strncmp(stream->url, "file://", 7) == 0) {
-    filename = stream->url + 7;
+    // Here's another temporary hack.  In addition to the weird
+    // filename format, the file that Safari tells us about appears to
+    // be a temporary file that Safari's about to delete.  In order to
+    // protect ourselves from this, we need to temporarily copy the
+    // file somewhere else.
+    char *name = tempnam(NULL, "p3d_");
+
+    // We prefer just making a hard link; it's quick and easy.
+    if (link(filename.c_str(), name) == 0) {
+      logfile << "linked " << filename << " to " << name << "\n";
+    } else {
+      // But sometimes the hard link might fail, particularly if these
+      // are two different file systems.  In this case we have to open
+      // the files and copy the data by hand.
+      ifstream in(filename.c_str(), ios::in | ios::binary);
+      ofstream out(name, ios::out | ios::binary);
+
+      static const size_t buffer_size = 4096;
+      char buffer[buffer_size];
+      
+      in.read(buffer, buffer_size);
+      size_t count = in.gcount();
+      while (count != 0) {
+        out.write(buffer, count);
+        in.read(buffer, buffer_size);
+        count = in.gcount();
+      }
+      logfile << "copied " << filename << " to " << name << "\n";
+    }
+
+    filename = name;
+    free(name);
+
+    // TODO: remove this temporary file when we're done with it.
   }
   }
 
 
 #endif  // __APPLE__
 #endif  // __APPLE__