* 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)
This commit is contained in:
2008-02-29 15:50:28 +00:00
parent 95b0176fde
commit 96247ce19c
5 changed files with 84 additions and 17 deletions

View File

@@ -40,11 +40,10 @@
/* 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);

View File

@@ -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 @@ static int ptcache_path(char *filename)
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 @@ int BKE_ptcache_id_filename(struct ID *id, char *filename, int cfra, int stack_i
/* 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 @@ FILE *BKE_ptcache_id_fopen(struct ID *id, char mode, int cfra, int stack_index)
}
/* youll need to close yourself after!
* mode,
* mode - PTCACHE_CLEAR_ALL,
*/
@@ -166,8 +169,7 @@ void BKE_ptcache_id_clear(struct ID *id, char mode, int cfra, int stack_index)
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 @@ int BKE_ptcache_id_exist(struct ID *id, int cfra, int stack_index)
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);
}
}

View File

@@ -1471,12 +1471,20 @@ void BLI_join_dirfile(char *string, const char *dir, const char *file)
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);
}

View File

@@ -49,6 +49,7 @@ void BIF_read_autosavefile(void);
void BIF_write_file(char *target);
void BIF_write_homefile(void);
void BIF_write_autosave(void);
void BIF_clear_tempfiles(void);
#endif

View File

@@ -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 @@ void BIF_read_file(char *name)
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 BIF_read_homefile(int from_memory)
int success;
struct TmpFont *tf;
BIF_clear_tempfiles();
BLI_clean(home);
tf= G.ttfdata.first;
@@ -939,6 +944,16 @@ void BIF_write_autosave(void)
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 @@ extern ListBase editelems;
void exit_usiblender(void)
{
struct TmpFont *tf;
BIF_clear_tempfiles();
tf= G.ttfdata.first;
while(tf)
{