Browse Source

more fixes

David Rose 16 years ago
parent
commit
3329c91dba

+ 4 - 0
direct/src/plugin/binaryXml.cxx

@@ -264,6 +264,10 @@ read_xml(istream &in, ostream &logfile) {
   // standard ASCII read.
   TiXmlDocument *doc = new TiXmlDocument;
   in >> *doc;
+  if (in.fail() || in.eof()) {
+    delete doc;
+    return NULL;
+  }
 #endif
 
   if (debug_xml_output) {

+ 6 - 1
direct/src/plugin/p3dInstance.cxx

@@ -836,12 +836,17 @@ make_xml() {
 ////////////////////////////////////////////////////////////////////
 void P3DInstance::
 scan_app_desc_file(TiXmlDocument *doc) {
+  P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr();
+
   TiXmlElement *xpackage = doc->FirstChildElement("package");
   if (xpackage == NULL) {
     return;
   }
 
-  P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr();
+  const char *log_basename = xpackage->Attribute("log");
+  if (log_basename != NULL) {
+    _log_basename = log_basename;
+  }
 
   TiXmlElement *xrequires = xpackage->FirstChildElement("requires");
   while (xrequires != NULL) {

+ 1 - 0
direct/src/plugin/p3dInstance.h

@@ -154,6 +154,7 @@ private:
   int _instance_id;
   string _session_key;
   string _python_version;
+  string _log_basename;
 
   // Not ref-counted: session is the parent.
   P3DSession *_session;

+ 22 - 2
direct/src/plugin/p3dInstanceManager.cxx

@@ -520,7 +520,20 @@ delete_global_ptr() {
 bool P3DInstanceManager::
 copy_file(const string &from_filename, const string &to_filename) {
   ifstream in(from_filename.c_str(), ios::in | ios::binary);
-  ofstream out(to_filename.c_str(), ios::out | ios::binary);
+
+  // Copy to a temporary file first, in case (a) the filenames
+  // actually refer to the same file, or (b) in case we have different
+  // processes writing to the same file, and (c) to prevent
+  // partially overwriting the file should something go wrong.
+  ostringstream strm;
+  strm << to_filename << ".t";
+#ifdef _WIN32
+  strm << GetCurrentProcessId() << "_" << GetCurrentThreadId();
+#else
+  strm << getpid();
+#endif
+  string temp_filename = strm.str();
+  ofstream out(temp_filename.c_str(), ios::out | ios::binary);
         
   static const size_t buffer_size = 4096;
   char buffer[buffer_size];
@@ -530,6 +543,7 @@ copy_file(const string &from_filename, const string &to_filename) {
   while (count != 0) {
     out.write(buffer, count);
     if (out.fail()) {
+      unlink(temp_filename.c_str());
       return false;
     }
     in.read(buffer, buffer_size);
@@ -537,10 +551,16 @@ copy_file(const string &from_filename, const string &to_filename) {
   }
 
   if (!in.eof()) {
+    unlink(temp_filename.c_str());
     return false;
   }
 
-  return true;
+  if (rename(temp_filename.c_str(), to_filename.c_str()) == 0) {
+    return true;
+  }
+
+  unlink(temp_filename.c_str());
+  return false;
 }
 
 ////////////////////////////////////////////////////////////////////

+ 39 - 12
direct/src/plugin/p3dSession.cxx

@@ -58,17 +58,6 @@ P3DSession(P3DInstance *inst) {
   _started_read_thread = false;
   _read_thread_continue = false;
 
-#ifdef _WIN32
-  static const size_t buffer_size = 4096;
-  char buffer[buffer_size];
-  if (GetTempPath(buffer_size, buffer) != 0) {
-    _output_filename = buffer;
-    _output_filename += "panda3d.3.log";
-  }
-#else
-  _output_filename = "/tmp/panda3d.3.log";
-#endif  // _WIN32
-
   INIT_LOCK(_instances_lock);
   INIT_THREAD(_read_thread);
 }
@@ -761,6 +750,44 @@ start_p3dpython(P3DInstance *inst) {
     env += '\0';
   }
 
+  // Get the log filename from the p3d_info.xml file.
+  string log_basename = inst->_log_basename;
+
+  // But we also let it be overridden by the tokens.
+  if (inst->get_fparams().has_token("log")) {
+    log_basename = inst->get_fparams().lookup_token("log");
+  }
+
+  // However, it is always written into the temp directory only; the
+  // user may not override the log file to put it anywhere else.
+  size_t slash = log_basename.rfind('/');
+  if (slash != string::npos) {
+    log_basename = log_basename.substr(slash + 1);
+  }
+#ifdef _WIN32
+  slash = log_basename.rfind('\\');
+  if (slash != string::npos) {
+    log_basename = log_basename.substr(slash + 1);
+  }
+#endif  // _WIN32
+
+  if (!log_basename.empty()) {
+#ifdef _WIN32
+    static const size_t buffer_size = 4096;
+    char buffer[buffer_size];
+    if (GetTempPath(buffer_size, buffer) != 0) {
+      _output_filename = buffer;
+    }
+#else
+    _output_filename = "/tmp/";
+#endif  // _WIN32
+    _output_filename += log_basename;
+
+    // We always tack on the extension ".log", to make it even more
+    // difficult to overwrite a system file.
+    _output_filename += ".log";
+  }
+
   nout << "Attempting to start python from " << p3dpython << "\n";
 #ifdef _WIN32
   _p3dpython_handle = win_create_process
@@ -860,7 +887,7 @@ rt_thread_run() {
     rt_handle_request(doc);
   }
 
-  logfile << "Exiting rt_thread_run in " << this << "\n";
+  nout << "Exiting rt_thread_run in " << this << "\n";
 }
 
 ////////////////////////////////////////////////////////////////////

+ 10 - 2
direct/src/showutil/Packager.py

@@ -192,7 +192,8 @@ class Packager:
                 raise PackagerError, message
             if self.mainModule:
                 moduleName, newName = self.mainModule
-                self.freezer.addModule(moduleName, newName = newName)
+                if newName not in self.freezer.modules:
+                    self.freezer.addModule(moduleName, newName = newName)
 
             # Now all module files have been added.  Exclude modules
             # already imported in a required package, and not
@@ -1911,7 +1912,7 @@ class Packager:
 
         self.currentPackage.freezer.excludeModule(moduleName, forbid = forbid)
 
-    def mainModule(self, moduleName, newName = None):
+    def mainModule(self, moduleName, newName = None, filename = None):
         """ Names the indicated module as the "main" module of the
         application or exe. """
 
@@ -1924,6 +1925,13 @@ class Packager:
 
         if not newName:
             newName = moduleName
+
+        if filename:
+            newFilename = Filename('/'.join(moduleName.split('.')))
+            newFilename.setExtension(filename.getExtension())
+            package.addFile(filename, newName = newFilename.cStr(),
+                            deleteTemp = True, explicit = True, extract = True)
+
         self.currentPackage.mainModule = (moduleName, newName)
 
     def freeze(self, filename, compileToExe = False):

+ 4 - 3
direct/src/showutil/packp3d.py

@@ -111,9 +111,10 @@ def makePackedApp(args):
             main = os.path.split(main[0])[1]
 
     main = Filename.fromOsSpecific(main)
-    main.setExtension('')
+    mainModule = Filename(main)
+    mainModule.setExtension('')
 
-    mainModule = main.cStr().replace('/', '.')
+    mainModule = mainModule.cStr().replace('/', '.')
     
     packager.installDir = appDir
     getModelPath().appendDirectory(root)
@@ -124,7 +125,7 @@ def makePackedApp(args):
         packager.require(requireName)
         
     packager.dir(root)
-    packager.mainModule(mainModule)
+    packager.mainModule(mainModule, filename = main)
         
     packager.endPackage(appBase, p3dApplication = True)