[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [28020] trunk/blender/source/blender/ blenlib: Making sure updates are not lost.

gsr b3d gsr.b3d at infernal-iceberg.com
Tue Apr 6 03:01:00 CEST 2010


Revision: 28020
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=28020
Author:   gsrb3d
Date:     2010-04-06 03:00:59 +0200 (Tue, 06 Apr 2010)

Log Message:
-----------
Making sure updates are not lost.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_bfile.h
    trunk/blender/source/blender/blenlib/intern/BLI_bfile.c

Modified: trunk/blender/source/blender/blenlib/BLI_bfile.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_bfile.h	2010-04-05 22:37:09 UTC (rev 28019)
+++ trunk/blender/source/blender/blenlib/BLI_bfile.h	2010-04-06 01:00:59 UTC (rev 28020)
@@ -1,4 +1,5 @@
-/*
+/* -*- indent-tabs-mode:t; tab-width:4; -*-
+ *
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -33,35 +34,28 @@
 /**
  Defines for the bflags param.
  */
-/* Special handling: */
+/* Special handling, pick one of: */
 /* For "symmetry" of flags */
 #define BFILE_NORMAL (0)
 /* No supervision, just translate // if needed, RISKY */
 #define BFILE_RAW    (1<<0)
-/* Path is based in env vars specified by "envvars" */
-#define BFILE_CONFIG (1<<1)
 /* Path is for current session temp files */
-#define BFILE_TEMP   (1<<2)
+#define BFILE_TEMP   (1<<1)
+/* Path is based in env vars of matching name */
+#define BFILE_CONFIG_BASE      (1<<2)
+#define BFILE_CONFIG_DATAFILES (1<<3)
+#define BFILE_CONFIG_PYTHON    (1<<4)
+#define BFILE_CONFIG_PLUGINS   (1<<5)
 
 /* Config handling, special cases: */
-#define BFILE_USERONLY (1<<3)
-#define BFILE_SYSONLY  (1<<4)
+#define BFILE_USERONLY (1<<6)
+#define BFILE_SYSONLY  (1<<7)
 
 /* Compression to apply on close: */
-#define BFILE_GZIP (1<<5)
+#define BFILE_GZIP (1<<8)
+#define BFILE_LZMA (1<<9)
 
 /**
- For the envvars param.
- */
-typedef enum BEnvVarFamilies {
-	BENV_NONE,
-	BENV_BASE,
-	BENV_DATAFILES,
-	BENV_PYTHON,
-	BENV_PLUGINS
-} BEnvVarFam;
-
-/**
  File descriptor for Blender abstracted file access.
  */
 typedef struct {
@@ -70,7 +64,6 @@
 
 	/* Anything below should not be touched directly */
 	int uflags;       /* Special options requested by upper level, copy of bflags */
-	BEnvVarFam evars; /* What kind of file, describe the env vars to use */
 	char *fpath;      /* Final/requested path name */
 	char *tpath;      /* Temp path name if applicable */
 	int classf;       /* Own flags, common classification of open and fopen */
@@ -80,12 +73,12 @@
 /**
  Open a BFILE* with fopen()-like syntax.
  */
-BFILE *BLI_bfile_fopen(const char *path, const char *mode, int bflags, BEnvVarFam envvars);
+BFILE *BLI_bfile_fopen(const char *path, const char *mode, int bflags, const char *relpath);
 
 /**
  Open a BFILE* with open()-like syntax.
  */
-BFILE *BLI_bfile_open(const char *pathname, int flags, int bflags, BEnvVarFam envvars);
+BFILE *BLI_bfile_open(const char *pathname, int flags, int bflags, const char *relpath);
 
 /**
  Get the FILE* associated with the BFILE*.

Modified: trunk/blender/source/blender/blenlib/intern/BLI_bfile.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_bfile.c	2010-04-05 22:37:09 UTC (rev 28019)
+++ trunk/blender/source/blender/blenlib/intern/BLI_bfile.c	2010-04-06 01:00:59 UTC (rev 28020)
@@ -1,4 +1,5 @@
-/*
+/* -*- indent-tabs-mode:t; tab-width:4; -*-
+ *
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -62,17 +63,19 @@
 
 
 /* Declaration of internal functions */
-void chomp(char* line);
-void expand_envvars(char* src, char* dst);
-void fill_paths(BFILE *bfile, const char *path);
-char* find_in_pathlist(char* filename, char* pathlist);
-void init_vars_from_file(const char* path);
-void setup_temp();
+static void chomp(char* line);
+static void expand_envvars(char* src, char* dst);
+static void fill_paths(BFILE *bfile, const char *path, const char *relpath);
+static char* find_in_pathlist(char* filename, char* pathlist);
+static void init_vars_from_file(const char* path);
+static void free_paths(BFILE* bfile);
+static void setup_temp();
 
+
 /*** Exported functions ***/
 
 BFILE *BLI_bfile_fopen(const char *path, const char *mode, int bflags,
-					   BEnvVarFam envvars)
+                       const char *relpath)
 {
 	BFILE *bfile;
 
@@ -88,50 +91,85 @@
 	a  BCF_AT_END | BCF_WRITE
 	a+ BCF_AT_END | BCF_WRITE | BCF_READ
 	*/
-	if(strchr(mode, 'r'))
+	if (strchr(mode, 'r'))
 		bfile->classf |= BCF_READ;
-	if(strchr(mode, 'w'))
+	if (strchr(mode, 'w'))
 		bfile->classf |= (BCF_DISCARD | BCF_WRITE);
-	if(strchr(mode, 'a'))
+	if (strchr(mode, 'a'))
 		bfile->classf |= (BCF_AT_END | BCF_WRITE);
-	if(strchr(mode, '+'))
+	if (strchr(mode, '+'))
 		bfile->classf |= (BCF_READ | BCF_WRITE);
 
-	fill_paths(bfile, path);
+	fill_paths(bfile, path, relpath);
 
 	bfile->stream = fopen(bfile->tpath, mode);
-	// detect failed fopen
+	if (!(bfile->stream)) {
+		free_paths(bfile);
+		MEM_freeN(bfile);
+		return NULL;
+	}
+
 	bfile->fd = fileno(bfile->stream);
+
 	return bfile;
 }
 
 
 BFILE *BLI_bfile_open(const char *pathname, int flags, int bflags,
-					  BEnvVarFam envvars)
+                      const char *relpath)
 {
 	BFILE *bfile;
+	char fopen_mode[3];
 
 	bfile = MEM_mallocN(sizeof(BFILE), "bfile-open");
 	bfile->classf = BCF_OPEN;
 	bfile->uflags = bflags;
 
 	/* Easy mapping for open() */
-	if(flags & O_RDONLY)
+	if (flags & O_RDONLY)
 		bfile->classf |= BCF_READ;
-	if(flags & O_WRONLY)
+	if (flags & O_WRONLY)
 		bfile->classf |= BCF_WRITE;
-	if(flags & O_RDWR)
+	if (flags & O_RDWR)
 		bfile->classf |= (BCF_READ | BCF_WRITE);
-	if(flags & O_APPEND)
+	if (flags & O_APPEND)
 		bfile->classf |= BCF_AT_END;
-	if(flags & O_TRUNC)
+	if (flags & O_TRUNC)
 		bfile->classf |= BCF_DISCARD;
 
-	fill_paths(bfile, pathname);
+	fill_paths(bfile, pathname, relpath);
 
 	bfile->fd = open(bfile->tpath, flags);
-	// detect failed open
-//	bfile->stream = fdopen(bfile->fd, XXX); /* MSWindows _fdopen? */
+	if (bfile->fd == -1) {
+		free_paths(bfile);
+		MEM_freeN(bfile);
+		return NULL;
+	}
+
+	fopen_mode[0] = 'r';
+	fopen_mode[1] = '\0';
+	fopen_mode[2] = '\0';
+	if (bfile->classf & BCF_DISCARD) {
+		fopen_mode[0] = 'w';
+		if (bfile->classf & BCF_READ) {
+			fopen_mode[1] = '+';
+		}
+	} else if (bfile->classf & BCF_AT_END) {
+		fopen_mode[0] = 'a';
+		if (bfile->classf & BCF_READ) {
+			fopen_mode[1] = '+';
+		}
+	} else if (bfile->classf & BCF_WRITE) {
+		fopen_mode[1] = '+';
+	}
+
+	bfile->stream = fdopen(bfile->fd, fopen_mode); /* MSWindows _fdopen? */
+	if (!(bfile->stream)) {
+		free_paths(bfile);
+		MEM_freeN(bfile);
+		return NULL;
+	}
+
 	return bfile;
 }
 
@@ -171,12 +209,15 @@
 
 
 size_t BLI_bfile_fwrite(const void *ptr, size_t size, size_t nmemb,
-						BFILE *f)
+                        BFILE *f)
 {
 	size_t ret;
 
+	if (f == NULL)
+		return 0
+
 	ret = fwrite(ptr, size, nmemb, f->stream);
-	if (ret < 0) {
+	if (ret <= 0) {
 		f->error = 1;
 	}
 
@@ -187,8 +228,11 @@
 size_t BLI_bfile_fread(void *ptr, size_t size, size_t nmemb, BFILE *f) {
 	size_t ret;
 
+	if (f == NULL)
+		return 0;
+
 	ret = fread(ptr, size, nmemb, f->stream);
-	if ((ret < 0) && ferror(f->stream)) {
+	if ((ret <= 0) && ferror(f->stream)) {
 		f->error = 1;
 	}
 
@@ -197,21 +241,23 @@
 
 
 void BLI_bfile_close(BFILE *bfile) {
-	if((bfile->classf | BCF_WRITE) &&
-	   !(bfile->uflags | BFILE_RAW)) {
+	if ((bfile->classf | BCF_WRITE) &&
+	    !(bfile->uflags | BFILE_RAW)) {
+		int error;
 		/* Make sure data is on disk */
+		error = fsync(bfile->fd);
+		/* fsync the directory too? */
 		/* Move to final name if no errors */
+		if (!(bfile->error) && !error) {
+			rename(bfile->tpath, bfile->fpath);
+		}
 	}
 
 	/* Normal close */
 
 	/* Cleanup */
-	if(bfile->fpath) {
-		MEM_freeN(bfile->fpath);
-	}
-	if(bfile->tpath) {
-		MEM_freeN(bfile->tpath);
-	}
+	free_paths(bfile);
+	MEM_freeN(bfile);
 }
 
 
@@ -240,7 +286,7 @@
 
 	/* Is this unpack&run? */
 	sprintf(temp, "%s/%d/environment", dirname(bprogname), BLENDER_VERSION);
-	if(BLI_exist(temp)) {
+	if (BLI_exist(temp)) {
 		BLI_setenv_if_new("BLENDER_SHARE", dirname(bprogname));
 	} else {
 		BLI_setenv_if_new("BLENDER_SHARE", (const char*)GHOST_getSystemDir());
@@ -255,12 +301,12 @@
 		temp[3] = '\0';
 		BLI_setenv("BLENDER_VERSION_PREV", temp);
 		/* 2nd line, read previous session path if needed */
-		if(!getenv("BLENDER_TEMP")) {
+		if (!getenv("BLENDER_TEMP")) {
 			if ((fgets(temp, MAXPATHLEN, fp) != NULL)) {
 				/* Clean any \n */
 				chomp(temp);
 				/* Check the dir is still there or generate new one */
-				if(!BLI_exist(temp)) {
+				if (!BLI_exist(temp)) {
 					setup_temp();
 				}
 			} else {
@@ -274,10 +320,18 @@
 		setup_temp();
 	}
 
-	if(fp) {
+	if (fp) {
 		fclose(fp);
 	}
 
+	/* Loaded session info (or created), so time to store current data */
+	// TODO use own fuctions to get safe saving
+	fp = fopen(file, "w");
+	if (fp) {
+		fprintf(fp, "%s\n%s\n", getenv("BLENDER_VERSION"), getenv("BLENDER_TEMP"));
+		fclose(fp);
+	}
+
 	/* Load vars from user and system files */
 	strcpy(file, (const char *)GHOST_getUserDir());
 	BLI_add_slash(file);
@@ -295,7 +349,7 @@
  Eliminate trailing EOL by writing a \0 over it.
  Name taken from Perl.
  */
-void chomp(char* line) {
+static void chomp(char* line) {
 	int len = strlen(line);
 #ifndef WIN32
 	if (line[len - 1] == '\n') {
@@ -319,7 +373,7 @@
 #define MAX_LINE 4096
 #define ENV_VAR 256
 #define VAR_LEN 8192
-void init_vars_from_file(const char* path) {
+static void init_vars_from_file(const char* path) {
 	char line[MAX_LINE];
 	char name[ENV_VAR];
 	FILE *fp;
@@ -336,7 +390,7 @@
 
 		/* Split into envvar name and contents */
 		separator = strchr(line, '=');
-		if(separator && ((separator - line) < ENV_VAR)) {
+		if (separator && ((separator - line) < ENV_VAR)) {
 			/* First remove EOL */
 			chomp(line);
 			strncpy(name, line, separator - line);
@@ -366,7 +420,7 @@
  #define ENVVAR_SUFFIX "%"
  #define ENVVAR_S_SIZE 1
 #endif /* WIN32 */
-void expand_envvars(char* src, char* dst) {
+static void expand_envvars(char* src, char* dst) {
 	char* hit1;
 	char* hit2;
 	char name[ENV_VAR];
@@ -427,7 +481,7 @@
 #else
  #define SEPARATOR ':'
 #endif
-char* find_in_pathlist(char* filename, char* pathlist) {
+static char* find_in_pathlist(char* filename, char* pathlist) {
 	char first[FILE_MAX + 10];
 	char* rest = NULL;
 
@@ -445,12 +499,12 @@
 	/* Check if combination exists */
 	BLI_add_slash(first);
 	strcat(first, filename);
-	if(BLI_exist(first)) {
+	if (BLI_exist(first)) {
 		return strdup(first);
 	}
 
 	/* First path failed, try with rest of paths if possible */
-	if(rest) {
+	if (rest) {
 		return find_in_pathlist(filename, rest);
 	} else {
 		return NULL;
@@ -461,35 +515,42 @@
 /**
  Setup fpath and tpath based in the needs of the bfile.
  */
-void fill_paths(BFILE *bfile, const char *path) {
+static void fill_paths(BFILE *bfile, const char *path, const char *relpath) {
 	char* source_path = NULL;
 	char* temp_path = NULL;
 	int bflags = bfile->uflags;
 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list