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>
49
# include "BLI_winstuff.h"
50
# include "BLI_callbacks.h"
51
# include "utf_winfunc.h"
54
# include <unistd.h> // for read close
55
# include <sys/param.h>
58
# include <sys/stat.h>
61
#include "MEM_guardedalloc.h"
63
#include "BLI_blenlib.h"
64
#include "BLI_utildefines.h"
66
#include "MEM_sys_types.h" // for intptr_t support
69
/* gzip the file in from and write it to "to".
70
* return -1 if zlib fails, -2 if the originating file does not exist
71
* note: will remove the "from" file
73
int BLI_file_gzip(const char *from, const char *to)
81
/* level 1 is very close to 3 (the default) in terms of file size,
82
* but about twice as fast, best use for speedy saving - campbell */
83
gzfile = BLI_gzopen(to, "wb1");
86
file = BLI_open(from, O_BINARY | O_RDONLY, 0);
91
readsize = read(file, buffer, sizeof(buffer));
94
rval = -2; /* error happened in reading */
95
fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
98
else if (readsize == 0)
99
break; /* done reading */
101
if (gzwrite(gzfile, buffer, readsize) <= 0) {
102
rval = -1; /* error happened in writing */
103
fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err));
114
/* gzip the file in from_file and write it to memory to_mem, at most size bytes.
115
* return the unziped size
117
char *BLI_file_ungzip_to_mem(const char *from_file, int *size_r)
120
int readsize, size, alloc_size = 0;
122
const int chunk_size = 512 * 1024;
126
gzfile = BLI_gzopen(from_file, "rb");
129
mem = MEM_callocN(chunk_size, "BLI_ungzip_to_mem");
130
alloc_size = chunk_size;
133
mem = MEM_reallocN(mem, size + chunk_size);
134
alloc_size += chunk_size;
137
readsize = gzread(gzfile, mem + size, chunk_size);
150
else if (alloc_size != size)
151
mem = MEM_reallocN(mem, size);
159
/* return 1 when file can be written */
160
int BLI_file_is_writable(const char *filename)
164
/* first try to open without creating */
165
file = BLI_open(filename, O_BINARY | O_RDWR, 0666);
168
/* now try to open and create. a test without actually
169
* creating a file would be nice, but how? */
170
file = BLI_open(filename, O_BINARY | O_RDWR | O_CREAT, 0666);
176
/* success, delete the file we create */
178
BLI_delete(filename, 0, 0);
188
int BLI_file_touch(const char *file)
190
FILE *f = BLI_fopen(file, "r+b");
197
f = BLI_fopen(file, "wb");
208
static char str[MAXPATHLEN + 12];
210
FILE *BLI_fopen(const char *filename, const char *mode)
212
return ufopen(filename, mode);
215
void BLI_get_short_name(char short_name[256], const char *filename)
217
wchar_t short_name_16[256];
220
UTF16_ENCODE(filename);
222
GetShortPathNameW(filename_16, short_name_16, 256);
224
for (i = 0; i < 256; i++) {
225
short_name[i] = (char)short_name_16[i];
228
UTF16_UN_ENCODE(filename);
231
void *BLI_gzopen(const char *filename, const char *mode)
235
if (!filename || !mode) {
239
char short_name[256];
241
/* xxx Creates file before transcribing the path */
243
fclose(ufopen(filename, "a"));
245
BLI_get_short_name(short_name, filename);
247
gzfile = gzopen(short_name, mode);
253
int BLI_open(const char *filename, int oflag, int pmode)
255
return uopen(filename, oflag, pmode);
258
int BLI_delete(const char *file, int dir, int recursive)
265
callLocalErrorCallBack("Recursive delete is unsupported on Windows");
269
err = !RemoveDirectoryW(file_16);
270
if (err) printf("Unable to remove directory");
273
err = !DeleteFileW(file_16);
274
if (err) callLocalErrorCallBack("Unable to delete file");
277
UTF16_UN_ENCODE(file);
282
int BLI_move(const char *file, const char *to)
286
/* windows doesn't support moving to a directory
287
* it has to be 'mv filename filename' and not
288
* 'mv filename destdir' */
290
BLI_strncpy(str, to, sizeof(str));
291
/* points 'to' to a directory ? */
292
if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
293
if (BLI_last_slash(file) != NULL) {
294
strcat(str, BLI_last_slash(file) + 1);
300
err = !MoveFileW(file_16, str_16);
301
UTF16_UN_ENCODE(str);
302
UTF16_UN_ENCODE(file);
305
callLocalErrorCallBack("Unable to move file");
306
printf(" Move from '%s' to '%s' failed\n", file, str);
313
int BLI_copy(const char *file, const char *to)
317
/* windows doesn't support copying to a directory
318
* it has to be 'cp filename filename' and not
319
* 'cp filename destdir' */
321
BLI_strncpy(str, to, sizeof(str));
322
/* points 'to' to a directory ? */
323
if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
324
if (BLI_last_slash(file) != NULL) {
325
strcat(str, BLI_last_slash(file) + 1);
331
err = !CopyFileW(file_16, str_16, FALSE);
332
UTF16_UN_ENCODE(str);
333
UTF16_UN_ENCODE(file);
336
callLocalErrorCallBack("Unable to copy file!");
337
printf(" Copy from '%s' to '%s' failed\n", file, str);
343
int BLI_create_symlink(const char *file, const char *to)
345
callLocalErrorCallBack("Linking files is unsupported on Windows");
351
void BLI_dir_create_recursive(const char *dirname)
354
char tmp[MAXPATHLEN];
356
/* First remove possible slash at the end of the dirname.
357
* This routine otherwise tries to create
358
* blah1/blah2/ (with slash) after creating
359
* blah1/blah2 (without slash) */
361
BLI_strncpy(tmp, dirname, sizeof(tmp));
362
lslash = BLI_last_slash(tmp);
364
if (lslash && (*(lslash + 1) == '\0')) {
368
/* check special case "c:\foo", don't try create "c:", harmless but prints an error below */
369
if (isalpha(tmp[0]) && (tmp[1] == ':') && tmp[2] == '\0') return;
371
if (BLI_exists(tmp)) return;
373
lslash = BLI_last_slash(tmp);
376
/* Split about the last slash and recurse */
378
BLI_dir_create_recursive(tmp);
381
if (dirname[0]) { /* patch, this recursive loop tries to create a nameless directory */
382
if (umkdir(dirname) == -1) {
383
printf("Unable to create directory %s\n", dirname);
388
int BLI_rename(const char *from, const char *to)
390
if (!BLI_exists(from)) return 0;
392
/* make sure the filenames are different (case insensitive) before removing */
393
if (BLI_exists(to) && BLI_strcasecmp(from, to))
394
if (BLI_delete(to, 0, 0)) return 1;
396
return urename(from, to);
399
#else /* The UNIX world */
402
/* operation succeeded succeeded */
403
RecursiveOp_Callback_OK = 0,
405
/* operation requested not to perform recursive digging for current path */
406
RecursiveOp_Callback_StopRecurs = 1,
408
/* error occured in callback and recursive walking should stop immediately */
409
RecursiveOp_Callback_Error = 2
410
} recuresiveOp_Callback_Result;
412
typedef int (*RecursiveOp_Callback)(const char *from, const char *to);
414
/* appending of filename to dir (ensures for buffer size before appending) */
415
static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, const char *file)
417
size_t len = strlen(dir) + strlen(file) + 1;
420
*dst = MEM_callocN(len + 1, "join_dirfile_alloc path");
421
else if (*alloc_len < len)
422
*dst = MEM_reallocN(*dst, len + 1);
426
BLI_join_dirfile(*dst, len + 1, dir, file);
429
static char *strip_last_slash(const char *dir)
431
char *result = BLI_strdup(dir);
432
BLI_del_slash(result);
437
static int recursive_operation(const char *startfrom, const char *startto, RecursiveOp_Callback callback_dir_pre,
438
RecursiveOp_Callback callback_file, RecursiveOp_Callback callback_dir_post)
440
struct dirent **dirlist;
442
char *from = NULL, *to = NULL;
443
char *from_path = NULL, *to_path = NULL;
444
size_t from_alloc_len = -1, to_alloc_len = -1;
447
/* ensure there's no trailing slash in file path */
448
from = strip_last_slash(startfrom);
450
to = strip_last_slash(startto);
452
ret = lstat(from, &st);
454
/* source wasn't found, nothing to operate with */
458
if (!S_ISDIR(st.st_mode)) {
459
/* source isn't a directory, can't do recursive walking for it,
460
* so just call file callback and leave */
462
ret = callback_file(from, to);
464
if (ret != RecursiveOp_Callback_OK)
469
if (to) MEM_freeN(to);
475
n = scandir(startfrom, &dirlist, 0, alphasort);
477
/* error opening directory for listing */
481
if (to) MEM_freeN(to);
486
if (callback_dir_pre) {
487
/* call pre-recursive walking directory callback */
488
ret = callback_dir_pre(from, to);
490
if (ret != RecursiveOp_Callback_OK) {
494
if (ret == RecursiveOp_Callback_StopRecurs) {
495
/* callback requested not to perform recursive walking, not an error */
503
for (i = 0; i < n; i++) {
504
struct dirent *dirent = dirlist[i];
506
if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) {
511
join_dirfile_alloc(&from_path, &from_alloc_len, from, dirent->d_name);
514
join_dirfile_alloc(&to_path, &to_alloc_len, to, dirent->d_name);
516
if (dirent->d_type == DT_DIR) {
517
/* recursively dig into a folder */
518
ret = recursive_operation(from_path, to_path, callback_dir_pre, callback_file, callback_dir_post);
520
else if (callback_file) {
521
/* call file callback for current path */
522
ret = callback_file(from_path, to_path);
523
if (ret != RecursiveOp_Callback_OK)
538
if (callback_dir_post) {
539
/* call post-recursive directory callback */
540
ret = callback_dir_post(from, to);
541
if (ret != RecursiveOp_Callback_OK)
546
if (from_path) MEM_freeN(from_path);
547
if (to_path) MEM_freeN(to_path);
550
if (to) MEM_freeN(to);
555
static int delete_callback_post(const char *from, const char *UNUSED(to))
560
return RecursiveOp_Callback_Error;
563
return RecursiveOp_Callback_OK;
566
static int delete_single_file(const char *from, const char *UNUSED(to))
571
return RecursiveOp_Callback_Error;
574
return RecursiveOp_Callback_OK;
577
FILE *BLI_fopen(const char *filename, const char *mode)
579
return fopen(filename, mode);
582
void *BLI_gzopen(const char *filename, const char *mode)
584
return gzopen(filename, mode);
587
int BLI_open(const char *filename, int oflag, int pmode)
589
return open(filename, oflag, pmode);
592
int BLI_delete(const char *file, int dir, int recursive)
594
if (strchr(file, '"')) {
595
printf("Error: not deleted file %s because of quote!\n", file);
599
return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
605
return remove(file); //BLI_snprintf(str, sizeof(str), "/bin/rm -f \"%s\"", file);
611
static int check_the_same(const char *path_a, const char *path_b)
613
struct stat st_a, st_b;
615
if (lstat(path_a, &st_a))
618
if (lstat(path_b, &st_b))
621
return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
624
static int set_permissions(const char *file, struct stat *st)
626
if (chown(file, st->st_uid, st->st_gid)) {
631
if (chmod(file, st->st_mode)) {
639
/* pre-recursive callback for copying operation
640
* creates a destination directory where all source content fill be copied to */
641
static int copy_callback_pre(const char *from, const char *to)
645
if (check_the_same(from, to)) {
646
fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
647
return RecursiveOp_Callback_Error;
650
if (lstat(from, &st)) {
652
return RecursiveOp_Callback_Error;
655
/* create a directory */
656
if (mkdir(to, st.st_mode)) {
658
return RecursiveOp_Callback_Error;
661
/* set proper owner and group on new directory */
662
if (chown(to, st.st_uid, st.st_gid)) {
664
return RecursiveOp_Callback_Error;
667
return RecursiveOp_Callback_OK;
670
static int copy_single_file(const char *from, const char *to)
672
FILE *from_stream, *to_stream;
677
if (check_the_same(from, to)) {
678
fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
679
return RecursiveOp_Callback_Error;
682
if (lstat(from, &st)) {
684
return RecursiveOp_Callback_Error;
687
if (S_ISLNK(st.st_mode)) {
688
/* symbolic links should be copied in special way */
693
/* get large enough buffer to read link content */
694
if (st.st_size < sizeof(buf)) {
699
link_buffer = MEM_callocN(st.st_size + 2, "copy_single_file link_buffer");
703
link_len = readlink(from, link_buffer, st.st_size + 1);
707
if (need_free) MEM_freeN(link_buffer);
709
return RecursiveOp_Callback_Error;
712
link_buffer[link_len] = 0;
714
if (symlink(link_buffer, to)) {
716
if (need_free) MEM_freeN(link_buffer);
717
return RecursiveOp_Callback_Error;
721
MEM_freeN(link_buffer);
723
return RecursiveOp_Callback_OK;
725
else if (S_ISCHR(st.st_mode) ||
726
S_ISBLK(st.st_mode) ||
727
S_ISFIFO(st.st_mode) ||
728
S_ISSOCK(st.st_mode))
730
/* copy special type of file */
731
if (mknod(to, st.st_mode, st.st_rdev)) {
733
return RecursiveOp_Callback_Error;
736
if (set_permissions(to, &st))
737
return RecursiveOp_Callback_Error;
739
return RecursiveOp_Callback_OK;
741
else if (!S_ISREG(st.st_mode)) {
742
fprintf(stderr, "Copying of this kind of files isn't supported yet\n");
743
return RecursiveOp_Callback_Error;
746
from_stream = fopen(from, "rb");
749
return RecursiveOp_Callback_Error;
752
to_stream = fopen(to, "wb");
756
return RecursiveOp_Callback_Error;
759
while ((len = fread(buf, 1, sizeof(buf), from_stream)) > 0) {
760
fwrite(buf, 1, len, to_stream);
766
if (set_permissions(to, &st))
767
return RecursiveOp_Callback_Error;
769
return RecursiveOp_Callback_OK;
772
static int move_callback_pre(const char *from, const char *to)
774
int ret = rename(from, to);
777
return copy_callback_pre(from, to);
779
return RecursiveOp_Callback_StopRecurs;
782
static int move_single_file(const char *from, const char *to)
784
int ret = rename(from, to);
787
return copy_single_file(from, to);
789
return RecursiveOp_Callback_OK;
792
int BLI_move(const char *file, const char *to)
794
int ret = recursive_operation(file, to, move_callback_pre, move_single_file, NULL);
797
return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
803
static char *check_destination(const char *file, const char *to)
807
if (!stat(to, &st)) {
808
if (S_ISDIR(st.st_mode)) {
809
char *str, *filename, *path;
812
str = strip_last_slash(file);
813
filename = BLI_last_slash(str);
823
len = strlen(to) + strlen(filename) + 1;
824
path = MEM_callocN(len + 1, "check_destination path");
825
BLI_join_dirfile(path, len + 1, to, filename);
836
int BLI_copy(const char *file, const char *to)
838
char *actual_to = check_destination(file, to);
841
ret = recursive_operation(file, actual_to, copy_callback_pre, copy_single_file, NULL);
844
MEM_freeN(actual_to);
849
int BLI_create_symlink(const char *file, const char *to)
851
return symlink(to, file);
854
void BLI_dir_create_recursive(const char *dirname)
859
char static_buf[MAXPATHLEN];
864
if (BLI_exists(dirname)) return;
871
size = strlen(dirname) + 1;
872
tmp = MEM_callocN(size, "BLI_dir_create_recursive tmp");
876
BLI_strncpy(tmp, dirname, size);
878
lslash = BLI_last_slash(tmp);
880
/* Split about the last slash and recurse */
882
BLI_dir_create_recursive(tmp);
888
mkdir(dirname, 0777);
891
int BLI_rename(const char *from, const char *to)
893
if (!BLI_exists(from)) return 0;
896
if (BLI_delete(to, 0, 0)) return 1;
898
return rename(from, to);