2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenlib/intern/fileops.c
36 #include <sys/types.h>
46 #include "BLI_winstuff.h"
47 #include "BLI_callbacks.h"
49 #include <unistd.h> // for read close
50 #include <sys/param.h>
53 #include "MEM_guardedalloc.h"
55 #include "BLI_blenlib.h"
57 #include "BKE_utildefines.h"
59 #include "BLO_sys_types.h" // for intptr_t support
62 /* gzip the file in from and write it to "to".
63 return -1 if zlib fails, -2 if the originating file does not exist
64 note: will remove the "from" file
66 int BLI_file_gzip(const char *from, const char *to)
74 /* level 1 is very close to 3 (the default) in terms of file size,
75 * but about twice as fast, best use for speedy saving - campbell */
76 gzfile = gzopen(to, "wb1");
80 file = open(from, O_BINARY|O_RDONLY);
85 readsize = read(file, buffer, sizeof(buffer));
88 rval= -2; /* error happened in reading */
89 fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
92 else if(readsize == 0)
93 break; /* done reading */
95 if(gzwrite(gzfile, buffer, readsize) <= 0) {
96 rval= -1; /* error happened in writing */
97 fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err));
108 /* gzip the file in from_file and write it to memery to_mem, at most size bytes.
109 return the unziped size
111 char *BLI_file_ungzip_to_mem(const char *from_file, int *size_r)
114 int readsize, size, alloc_size=0;
116 const int chunk_size= 512*1024;
120 gzfile = gzopen( from_file, "rb" );
124 mem= MEM_callocN(chunk_size, "BLI_ungzip_to_mem");
125 alloc_size= chunk_size;
127 mem= MEM_reallocN(mem, size+chunk_size);
128 alloc_size+= chunk_size;
131 readsize= gzread(gzfile, mem+size, chunk_size);
142 else if(alloc_size!=size)
143 mem= MEM_reallocN(mem, size);
151 /* return 1 when file can be written */
152 int BLI_file_is_writable(const char *filename)
156 /* first try to open without creating */
157 file = open(filename, O_BINARY | O_RDWR, 0666);
160 /* now try to open and create. a test without actually
161 * creating a file would be nice, but how? */
162 file = open(filename, O_BINARY | O_RDWR | O_CREAT, 0666);
168 /* success, delete the file we create */
170 BLI_delete(filename, 0, 0);
180 int BLI_file_touch(const char *file)
182 FILE *f = fopen(file,"r+b");
188 f = fopen(file,"wb");
199 static char str[MAXPATHLEN+12];
201 int BLI_delete(const char *file, int dir, int recursive)
206 callLocalErrorCallBack("Recursive delete is unsupported on Windows");
209 err= !RemoveDirectory(file);
210 if (err) printf ("Unable to remove directory");
212 err= !DeleteFile(file);
213 if (err) callLocalErrorCallBack("Unable to delete file");
219 int BLI_move(const char *file, const char *to)
223 // windows doesn't support moveing to a directory
224 // it has to be 'mv filename filename' and not
225 // 'mv filename destdir'
227 BLI_strncpy(str, to, sizeof(str));
228 // points 'to' to a directory ?
229 if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
230 if (BLI_last_slash(file) != NULL) {
231 strcat(str, BLI_last_slash(file) + 1);
235 err= !MoveFile(file, str);
237 callLocalErrorCallBack("Unable to move file");
238 printf(" Move from '%s' to '%s' failed\n", file, str);
245 int BLI_copy(const char *file, const char *to)
249 // windows doesn't support copying to a directory
250 // it has to be 'cp filename filename' and not
251 // 'cp filename destdir'
253 BLI_strncpy(str, to, sizeof(str));
254 // points 'to' to a directory ?
255 if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
256 if (BLI_last_slash(file) != NULL) {
257 strcat(str, BLI_last_slash(file) + 1);
261 err= !CopyFile(file,str,FALSE);
264 callLocalErrorCallBack("Unable to copy file!");
265 printf(" Copy from '%s' to '%s' failed\n", file, str);
271 int BLI_create_symlink(const char *file, const char *to)
273 callLocalErrorCallBack("Linking files is unsupported on Windows");
279 void BLI_dir_create_recursive(const char *dirname)
282 char tmp[MAXPATHLEN];
284 // First remove possible slash at the end of the dirname.
285 // This routine otherwise tries to create
286 // blah1/blah2/ (with slash) after creating
287 // blah1/blah2 (without slash)
289 BLI_strncpy(tmp, dirname, sizeof(tmp));
290 lslash= BLI_last_slash(tmp);
292 if (lslash == tmp + strlen(tmp) - 1) {
296 if (BLI_exists(tmp)) return;
298 lslash= BLI_last_slash(tmp);
300 /* Split about the last slash and recurse */
302 BLI_dir_create_recursive(tmp);
305 if(dirname[0]) /* patch, this recursive loop tries to create a nameless directory */
306 if (!CreateDirectory(dirname, NULL))
307 callLocalErrorCallBack("Unable to create directory\n");
310 int BLI_rename(const char *from, const char *to)
312 if (!BLI_exists(from)) return 0;
314 /* make sure the filenames are different (case insensitive) before removing */
315 if (BLI_exists(to) && BLI_strcasecmp(from, to))
316 if(BLI_delete(to, 0, 0)) return 1;
318 return rename(from, to);
321 #else /* The UNIX world */
324 * but the UNIX world is tied to the interface, and the system
325 * timer, and... We implement a callback mechanism. The system will
326 * have to initialise the callback before the functions will work!
328 static char str[12 + (MAXPATHLEN * 2)];
330 int BLI_delete(const char *file, int dir, int recursive)
332 if(strchr(file, '"')) {
333 printf("Error: not deleted file %s because of quote!\n", file);
337 BLI_snprintf(str, sizeof(str), "/bin/rm -rf \"%s\"", file);
341 BLI_snprintf(str, sizeof(str), "/bin/rmdir \"%s\"", file);
345 return remove(file); //BLI_snprintf(str, sizeof(str), "/bin/rm -f \"%s\"", file);
351 int BLI_move(const char *file, const char *to)
353 BLI_snprintf(str, sizeof(str), "/bin/mv -f \"%s\" \"%s\"", file, to);
358 int BLI_copy(const char *file, const char *to)
360 BLI_snprintf(str, sizeof(str), "/bin/cp -rf \"%s\" \"%s\"", file, to);
365 int BLI_create_symlink(const char *file, const char *to)
367 BLI_snprintf(str, sizeof(str), "/bin/ln -f \"%s\" \"%s\"", file, to);
372 void BLI_dir_create_recursive(const char *dirname)
375 char tmp[MAXPATHLEN];
377 if (BLI_exists(dirname)) return;
379 BLI_strncpy(tmp, dirname, sizeof(tmp));
381 lslash= BLI_last_slash(tmp);
383 /* Split about the last slash and recurse */
385 BLI_dir_create_recursive(tmp);
388 mkdir(dirname, 0777);
391 int BLI_rename(const char *from, const char *to)
393 if (!BLI_exists(from)) return 0;
395 if (BLI_exists(to)) if(BLI_delete(to, 0, 0)) return 1;
397 return rename(from, to);