[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13917] trunk/blender/source/blender: * Made BLI_join_dirfile() check before adding a slash between dir and file so as not to get /foo/// bar.blend

Campbell Barton ideasman42 at gmail.com
Fri Feb 29 16:50:28 CET 2008


Revision: 13917
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13917
Author:   campbellbarton
Date:     2008-02-29 16:50:28 +0100 (Fri, 29 Feb 2008)

Log Message:
-----------
* Made BLI_join_dirfile() check before adding a slash between dir and file so as not to get /foo///bar.blend
* Pointcache now uses the process id to construct the path for unsaved files. (so 2 or more blender's open wont try to read/write the same pointcache)
* Temp pointcache is cleared when existing blender, added BIF_clear_tempfiles() for this. Should also be usedto clear EXR's in the temp dir (TODO), BIF_clear_tempfiles also needs to be added in more places. (On file load for instace)

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_pointcache.h
    trunk/blender/source/blender/blenkernel/intern/pointcache.c
    trunk/blender/source/blender/blenlib/intern/util.c
    trunk/blender/source/blender/include/BIF_usiblender.h
    trunk/blender/source/blender/src/usiblender.c

Modified: trunk/blender/source/blender/blenkernel/BKE_pointcache.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_pointcache.h	2008-02-29 15:36:24 UTC (rev 13916)
+++ trunk/blender/source/blender/blenkernel/BKE_pointcache.h	2008-02-29 15:50:28 UTC (rev 13917)
@@ -40,12 +40,11 @@
 
 /* Add the blendfile name after blendcache_ */
 #define PTCACHE_EXT ".bphys"
-#define PTCACHE_PATH "//blendcache_"
+#define PTCACHE_PATH "blendcache_"
 
 /* Global funcs */
-/* void BKE_ptcache_clean(void); - not implimented yet! */
+void BKE_ptcache_remove(void);
 
-
 /* Object spesific funcs */
 int		BKE_ptcache_id_filename(struct ID *id, char *filename, int cfra, int stack_index, short do_path, short do_ext);
 FILE *	BKE_ptcache_id_fopen(struct ID *id, char mode, int cfra, int stack_index);

Modified: trunk/blender/source/blender/blenkernel/intern/pointcache.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/pointcache.c	2008-02-29 15:36:24 UTC (rev 13916)
+++ trunk/blender/source/blender/blenkernel/intern/pointcache.c	2008-02-29 15:50:28 UTC (rev 13917)
@@ -51,6 +51,12 @@
   #include "BLI_winstuff.h"
 #endif
 
+/* untitled blend's need getpid for a unique name */
+#ifdef WIN32
+#include <process.h>
+#else
+#include <unistd.h>
+#endif
 
 /*	Takes an Object ID and returns a unique name
 	- id: object id
@@ -71,15 +77,14 @@
 		if (i > 6)
 			file[i-6] = '\0';
 		
-		sprintf(filename, PTCACHE_PATH"%s/", file); /* add blend file name to pointcache dir */
+		sprintf(filename, "//"PTCACHE_PATH"%s/", file); /* add blend file name to pointcache dir */
 		BLI_convertstringcode(filename, G.sce, 0);
 		return strlen(filename);
-	} else {
-		/* use the temp path. this is weak but better then not using point cache at all */
-		/* btempdir is assumed to exist and ALWAYS has a trailing slash */
-		return sprintf(filename, "%s"PTCACHE_PATH"untitled/", btempdir);
 	}
-	return -1;
+	
+	/* use the temp path. this is weak but better then not using point cache at all */
+	/* btempdir is assumed to exist and ALWAYS has a trailing slash */
+	return sprintf(filename, "%s"PTCACHE_PATH"%d/", btempdir, abs(getpid()));
 }
 
 int BKE_ptcache_id_filename(struct ID *id, char *filename, int cfra, int stack_index, short do_path, short do_ext)
@@ -95,8 +100,6 @@
 	/* start with temp dir */
 	if (do_path) {
 		len = ptcache_path(filename);
-		if (len==-1)
-			return -1;
 		newname += len;
 	}
 	idname = (id->name+2);
@@ -144,7 +147,7 @@
 }
 
 /* youll need to close yourself after!
- * mode, 
+ * mode - PTCACHE_CLEAR_ALL, 
 
 */
 
@@ -166,8 +169,7 @@
 	case PTCACHE_CLEAR_ALL:
 	case PTCACHE_CLEAR_BEFORE:	
 	case PTCACHE_CLEAR_AFTER:
-		if (ptcache_path(path)==-1)
-			return;
+		ptcache_path(path);
 		
 		len = BKE_ptcache_id_filename(id, filename, cfra, stack_index, 0, 0); /* no path */
 		
@@ -219,3 +221,42 @@
 
 	return BLI_exists(filename);
 }
+
+/* Use this when quitting blender, with unsaved files */
+void BKE_ptcache_remove(void)
+{
+	char path[FILE_MAX];
+	char path_full[FILE_MAX];
+	int rmdir = 1;
+	
+	ptcache_path(path);
+
+	if (BLI_exists(path)) {
+	/* TODO, Check with win32, probably needs last slash removed */
+		/* The pointcache dir exists? - remove all pointcache */
+
+		DIR *dir; 
+		struct dirent *de;
+		
+		dir = opendir(path);
+		if (dir==NULL)
+			return;
+		
+		while ((de = readdir(dir)) != NULL) {
+			if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
+				/* do nothing */
+			} else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
+				BLI_join_dirfile(path_full, path, de->d_name);
+				BLI_delete(path_full, 0, 0);
+			} else {
+				rmdir = 0; /* unknown file, dont remove the dir */
+			}
+		}
+	} else { 
+		rmdir = 0; /* path dosnt exist  */
+	}
+	
+	if (rmdir) {
+		BLI_delete(path, 1, 0);
+	}
+}

Modified: trunk/blender/source/blender/blenlib/intern/util.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/util.c	2008-02-29 15:36:24 UTC (rev 13916)
+++ trunk/blender/source/blender/blenlib/intern/util.c	2008-02-29 15:50:28 UTC (rev 13917)
@@ -1471,12 +1471,20 @@
 	int sl_dir = strlen(dir);
 	BLI_strncpy(string, dir, FILE_MAX);
 	if (sl_dir > FILE_MAX-1) sl_dir = FILE_MAX-1;
+	
+	/* only add seperator if needed */
 #ifdef WIN32
-	string[sl_dir] = '\\';
+	if (string[sl_dir-1] != '\\') {
+		string[sl_dir] = '\\';
+		sl_dir++;
+	}
 #else
-	string[sl_dir] = '/';
+	if (string[sl_dir-1] != '/') {
+		string[sl_dir] = '/';
+		sl_dir++;
+	}
 #endif
-	sl_dir++;
+	
 	if (sl_dir <FILE_MAX) {
 		BLI_strncpy(string + sl_dir, file, FILE_MAX-sl_dir);
 	}

Modified: trunk/blender/source/blender/include/BIF_usiblender.h
===================================================================
--- trunk/blender/source/blender/include/BIF_usiblender.h	2008-02-29 15:36:24 UTC (rev 13916)
+++ trunk/blender/source/blender/include/BIF_usiblender.h	2008-02-29 15:50:28 UTC (rev 13917)
@@ -49,6 +49,7 @@
 void BIF_write_file(char *target);
 void BIF_write_homefile(void);
 void BIF_write_autosave(void);
+void BIF_clear_tempfiles(void);
 
 #endif
 

Modified: trunk/blender/source/blender/src/usiblender.c
===================================================================
--- trunk/blender/source/blender/src/usiblender.c	2008-02-29 15:36:24 UTC (rev 13916)
+++ trunk/blender/source/blender/src/usiblender.c	2008-02-29 15:50:28 UTC (rev 13917)
@@ -84,6 +84,7 @@
 #include "BKE_packedFile.h"
 #include "BKE_texture.h"
 #include "BKE_utildefines.h"
+#include "BKE_pointcache.h"
 
 #ifdef WITH_VERSE
 #include "BKE_verse.h"
@@ -541,6 +542,8 @@
 	retval= BKE_read_exotic(name);
 	
 	if (retval== 0) {
+		BIF_clear_tempfiles();
+		
 		/* we didn't succeed, now try to read Blender file */
 		retval= BKE_read_file(name, NULL);
 
@@ -591,6 +594,8 @@
 	int success;
 	struct TmpFont *tf;
 	
+	BIF_clear_tempfiles();
+	
 	BLI_clean(home);
 
 	tf= G.ttfdata.first;
@@ -939,6 +944,16 @@
 	BLO_write_file(tstr, write_flags, &err);
 }
 
+/* remove temp files assosiated with this blend file when quitting, loading or saving in a new path */
+void BIF_clear_tempfiles( void )
+{
+	/* TODO - remove exr files from the temp dir */
+	
+	if (!G.relbase_valid) { /* We could have pointcache saved in tyhe temp dir, if its there */
+		BKE_ptcache_remove();
+	}
+}
+
 /* if global undo; remove tempsave, otherwise rename */
 static void delete_autosave(void)
 {
@@ -1019,6 +1034,9 @@
 void exit_usiblender(void)
 {
 	struct TmpFont *tf;
+	
+	BIF_clear_tempfiles();
+	
 	tf= G.ttfdata.first;
 	while(tf)
 	{





More information about the Bf-blender-cvs mailing list