43
#include "BLI_winstuff.h"
46
# include "BLI_winstuff.h"
47
# include "BLI_callbacks.h"
48
# include "utf_winfunc.h"
45
#include <unistd.h> // for read close
46
#include <sys/param.h>
51
# include <unistd.h> // for read close
52
# include <sys/param.h>
55
# include <sys/stat.h>
58
#include "MEM_guardedalloc.h"
50
60
#include "BLI_blenlib.h"
61
#include "BLI_utildefines.h"
52
63
#include "BKE_utildefines.h"
54
65
#include "BLO_sys_types.h" // for intptr_t support
56
/* implementations: */
57
char *first_slash(char *string) {
58
char *ffslash, *fbslash;
60
ffslash= strchr(string, '/');
61
fbslash= strchr(string, '\\');
63
if (!ffslash) return fbslash;
64
else if (!fbslash) return ffslash;
66
if ((intptr_t)ffslash < (intptr_t)fbslash) return ffslash;
70
char *BLI_last_slash(const char *string) {
71
char *lfslash, *lbslash;
73
lfslash= strrchr(string, '/');
74
lbslash= strrchr(string, '\\');
76
if (!lfslash) return lbslash;
77
else if (!lbslash) return lfslash;
79
if ((intptr_t)lfslash < (intptr_t)lbslash) return lbslash;
83
/* adds a slash if there isnt one there already */
84
int BLI_add_slash(char *string) {
85
int len = strlen(string);
87
if (len==0 || string[len-1]!='\\') {
93
if (len==0 || string[len-1]!='/') {
102
/* removes a slash if there is one */
103
void BLI_del_slash(char *string) {
104
int len = strlen(string);
107
if (string[len-1]=='\\') {
109
if (string[len-1]=='/') {
111
string[len-1] = '\0';
119
68
/* gzip the file in from and write it to "to".
120
return -1 if zlib fails, -2 if the originating file does not exist
121
note: will remove the "from" file
123
int BLI_gzip(char *from, char *to) {
69
* return -1 if zlib fails, -2 if the originating file does not exist
70
* note: will remove the "from" file
72
int BLI_file_gzip(const char *from, const char *to)
124
74
char buffer[10240];
130
gzfile = gzopen(to, "wb");
80
/* level 1 is very close to 3 (the default) in terms of file size,
81
* but about twice as fast, best use for speedy saving - campbell */
82
gzfile = BLI_gzopen(to, "wb1");
134
file = open(from, O_BINARY|O_RDONLY);
85
file = BLI_open(from, O_BINARY|O_RDONLY,0);
139
readsize = read(file, buffer, 10240);
90
readsize = read(file, buffer, sizeof(buffer));
142
93
rval= -2; /* error happened in reading */
143
94
fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
146
else if(readsize == 0)
97
else if (readsize == 0)
147
98
break; /* done reading */
149
if(gzwrite(gzfile, buffer, readsize) <= 0) {
100
if (gzwrite(gzfile, buffer, readsize) <= 0) {
150
101
rval= -1; /* error happened in writing */
151
102
fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err));
283
int BLI_link(char *file, char *to) {
377
int BLI_create_symlink(const char *file, const char *to)
284
379
callLocalErrorCallBack("Linking files is unsupported on Windows");
289
void BLI_recurdir_fileops(char *dirname) {
385
void BLI_dir_create_recursive(const char *dirname)
291
388
char tmp[MAXPATHLEN];
293
// First remove possible slash at the end of the dirname.
294
// This routine otherwise tries to create
295
// blah1/blah2/ (with slash) after creating
296
// blah1/blah2 (without slash)
390
/* First remove possible slash at the end of the dirname.
391
* This routine otherwise tries to create
392
* blah1/blah2/ (with slash) after creating
393
* blah1/blah2 (without slash) */
298
strcpy(tmp, dirname);
395
BLI_strncpy(tmp, dirname, sizeof(tmp));
299
396
lslash= BLI_last_slash(tmp);
301
398
if (lslash == tmp + strlen(tmp) - 1) {
305
402
if (BLI_exists(tmp)) return;
307
404
lslash= BLI_last_slash(tmp);
309
/* Split about the last slash and recurse */
406
/* Split about the last slash and recurse */
311
BLI_recurdir_fileops(tmp);
408
BLI_dir_create_recursive(tmp);
314
if(dirname[0]) /* patch, this recursive loop tries to create a nameless directory */
315
if (!CreateDirectory(dirname, NULL))
316
callLocalErrorCallBack("Unable to create directory\n");
411
if (dirname[0]) /* patch, this recursive loop tries to create a nameless directory */
412
if (umkdir(dirname)==-1)
413
printf("Unable to create directory %s\n",dirname);
319
int BLI_rename(char *from, char *to) {
416
int BLI_rename(const char *from, const char *to)
320
418
if (!BLI_exists(from)) return 0;
322
420
/* make sure the filenames are different (case insensitive) before removing */
323
421
if (BLI_exists(to) && BLI_strcasecmp(from, to))
324
if(BLI_delete(to, 0, 0)) return 1;
326
return rename(from, to);
329
#else /* The weirdo UNIX world */
332
* but the UNIX world is tied to the interface, and the system
333
* timer, and... We implement a callback mechanism. The system will
334
* have to initialise the callback before the functions will work!
336
static char str[MAXPATHLEN+12];
338
int BLI_delete(char *file, int dir, int recursive)
340
if(strchr(file, '"')) {
422
if (BLI_delete(to, 0, 0)) return 1;
424
return urename(from, to);
427
#else /* The UNIX world */
430
/* operation succeeded succeeded */
431
recursiveOp_Callback_OK = 0,
433
/* operation requested not to perform recursive digging for current path */
434
recursiveOp_Callback_StopRecurs = 1,
436
/* error occured in callback and recursive walking should stop immediately */
437
recursiveOp_Callback_Error = 2
438
} recuresiveOp_Callback_Result;
440
typedef int (*recursiveOp_Callback) (const char *from, const char *to);
442
/* appending of filename to dir (ensures for buffer size before appending) */
443
static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, const char *file)
445
size_t len = strlen(dir) + strlen(file) + 1;
448
*dst = MEM_callocN(len + 1, "join_dirfile_alloc path");
449
else if (*alloc_len < len)
450
*dst = MEM_reallocN(*dst, len + 1);
454
BLI_join_dirfile(*dst, len + 1, dir, file);
457
static char *strip_last_slash(const char *dir)
459
char *result = BLI_strdup(dir);
460
BLI_del_slash(result);
465
static int recursive_operation(const char *startfrom, const char *startto, recursiveOp_Callback callback_dir_pre,
466
recursiveOp_Callback callback_file, recursiveOp_Callback callback_dir_post)
468
struct dirent **dirlist;
470
char *from = NULL, *to = NULL;
471
char *from_path = NULL, *to_path = NULL;
472
size_t from_alloc_len = -1, to_alloc_len = -1;
475
/* ensure there's no trailing slash in file path */
476
from = strip_last_slash(startfrom);
478
to = strip_last_slash(startto);
480
ret = lstat(from, &st);
482
/* source wasn't found, nothing to operate with */
486
if (!S_ISDIR(st.st_mode)) {
487
/* source isn't a directory, can't do recursive walking for it,
488
* so just call file callback and leave */
490
ret = callback_file(from, to);
492
if (ret != recursiveOp_Callback_OK)
497
if (to) MEM_freeN(to);
503
n = scandir(startfrom, &dirlist, 0, alphasort);
505
/* error opening directory for listing */
509
if (to) MEM_freeN(to);
514
if (callback_dir_pre) {
515
/* call pre-recursive walking directory callback */
516
ret = callback_dir_pre(from, to);
518
if (ret != recursiveOp_Callback_OK) {
522
if (ret == recursiveOp_Callback_StopRecurs) {
523
/* callback requested not to perform recursive walking, not an error */
531
for (i = 0; i < n; i++) {
532
struct dirent *dirent = dirlist[i];
534
if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) {
539
join_dirfile_alloc(&from_path, &from_alloc_len, from, dirent->d_name);
542
join_dirfile_alloc(&to_path, &to_alloc_len, to, dirent->d_name);
544
if (dirent->d_type == DT_DIR) {
545
/* recursively dig into a folder */
546
ret = recursive_operation(from_path, to_path, callback_dir_pre, callback_file, callback_dir_post);
548
else if (callback_file) {
549
/* call file callback for current path */
550
ret = callback_file(from_path, to_path);
551
if (ret != recursiveOp_Callback_OK)
565
if (callback_dir_post) {
566
/* call post-recursive directory callback */
567
ret = callback_dir_post(from, to);
568
if (ret != recursiveOp_Callback_OK)
573
if (from_path) MEM_freeN(from_path);
574
if (to_path) MEM_freeN(to_path);
577
if (to) MEM_freeN(to);
582
static int delete_callback_post(const char *from, const char *UNUSED(to))
587
return recursiveOp_Callback_Error;
590
return recursiveOp_Callback_OK;
593
static int delete_single_file(const char *from, const char *UNUSED(to))
598
return recursiveOp_Callback_Error;
601
return recursiveOp_Callback_OK;
604
FILE *BLI_fopen(const char *filename, const char *mode)
606
return fopen(filename, mode);
609
void *BLI_gzopen(const char *filename, const char *mode)
611
return gzopen(filename, mode);
614
int BLI_open(const char *filename, int oflag, int pmode)
616
return open(filename, oflag, pmode);
619
int BLI_delete(const char *file, int dir, int recursive)
621
if (strchr(file, '"')) {
341
622
printf("Error: not deleted file %s because of quote!\n", file);
345
sprintf(str, "/bin/rm -rf \"%s\"", file);
626
return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
349
sprintf(str, "/bin/rmdir \"%s\"", file);
353
return remove(file); //sprintf(str, "/bin/rm -f \"%s\"", file);
632
return remove(file); //BLI_snprintf(str, sizeof(str), "/bin/rm -f \"%s\"", file);
359
int BLI_move(char *file, char *to) {
360
sprintf(str, "/bin/mv -f \"%s\" \"%s\"", file, to);
365
int BLI_copy_fileops(char *file, char *to) {
366
sprintf(str, "/bin/cp -rf \"%s\" \"%s\"", file, to);
371
int BLI_link(char *file, char *to) {
372
sprintf(str, "/bin/ln -f \"%s\" \"%s\"", file, to);
377
void BLI_recurdir_fileops(char *dirname) {
638
static int check_the_same(const char *path_a, const char *path_b)
640
struct stat st_a, st_b;
642
if (lstat(path_a, &st_a))
645
if (lstat(path_b, &st_b))
648
return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
651
static int set_permissions(const char *file, struct stat *st)
653
if (chown(file, st->st_uid, st->st_gid)) {
658
if (chmod(file, st->st_mode)) {
666
/* pre-recursive callback for copying operation
667
* creates a destination directory where all source content fill be copied to */
668
static int copy_callback_pre(const char *from, const char *to)
672
if (check_the_same(from, to)) {
673
fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
674
return recursiveOp_Callback_Error;
677
if (lstat(from, &st)) {
679
return recursiveOp_Callback_Error;
682
/* create a directory */
683
if (mkdir(to, st.st_mode)) {
685
return recursiveOp_Callback_Error;
688
/* set proper owner and group on new directory */
689
if (chown(to, st.st_uid, st.st_gid)) {
691
return recursiveOp_Callback_Error;
694
return recursiveOp_Callback_OK;
697
static int copy_single_file(const char *from, const char *to)
699
FILE *from_stream, *to_stream;
704
if (check_the_same(from, to)) {
705
fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
706
return recursiveOp_Callback_Error;
709
if (lstat(from, &st)) {
711
return recursiveOp_Callback_Error;
714
if (S_ISLNK(st.st_mode)) {
715
/* symbolic links should be copied in special way */
720
/* get large enough buffer to read link content */
721
if (st.st_size < sizeof(buf)) {
726
link_buffer = MEM_callocN(st.st_size+2, "copy_single_file link_buffer");
730
link_len = readlink(from, link_buffer, st.st_size+1);
734
if (need_free) MEM_freeN(link_buffer);
736
return recursiveOp_Callback_Error;
739
link_buffer[link_len] = 0;
741
if (symlink(link_buffer, to)) {
743
if (need_free) MEM_freeN(link_buffer);
744
return recursiveOp_Callback_Error;
748
MEM_freeN(link_buffer);
750
return recursiveOp_Callback_OK;
752
else if (S_ISCHR (st.st_mode) ||
753
S_ISBLK (st.st_mode) ||
754
S_ISFIFO (st.st_mode) ||
755
S_ISSOCK (st.st_mode))
757
/* copy special type of file */
758
if (mknod(to, st.st_mode, st.st_rdev)) {
760
return recursiveOp_Callback_Error;
763
if (set_permissions(to, &st))
764
return recursiveOp_Callback_Error;
766
return recursiveOp_Callback_OK;
768
else if (!S_ISREG(st.st_mode)) {
769
fprintf(stderr, "Copying of this kind of files isn't supported yet\n");
770
return recursiveOp_Callback_Error;
773
from_stream = fopen(from, "rb");
776
return recursiveOp_Callback_Error;
779
to_stream = fopen(to, "wb");
783
return recursiveOp_Callback_Error;
786
while ((len = fread(buf, 1, sizeof(buf), from_stream)) > 0) {
787
fwrite(buf, 1, len, to_stream);
793
if (set_permissions(to, &st))
794
return recursiveOp_Callback_Error;
796
return recursiveOp_Callback_OK;
799
static int move_callback_pre(const char *from, const char *to)
801
int ret = rename(from, to);
804
return copy_callback_pre(from, to);
806
return recursiveOp_Callback_StopRecurs;
809
static int move_single_file(const char *from, const char *to)
811
int ret = rename(from, to);
814
return copy_single_file(from, to);
816
return recursiveOp_Callback_OK;
819
int BLI_move(const char *file, const char *to)
821
int ret = recursive_operation(file, to, move_callback_pre, move_single_file, NULL);
824
return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
830
static char *check_destination(const char *file, const char *to)
834
if (!stat(to, &st)) {
835
if (S_ISDIR(st.st_mode)) {
836
char *str, *filename, *path;
839
str = strip_last_slash(file);
840
filename = BLI_last_slash(str);
850
len = strlen(to) + strlen(filename) + 1;
851
path = MEM_callocN(len + 1, "check_destination path");
852
BLI_join_dirfile(path, len + 1, to, filename);
863
int BLI_copy(const char *file, const char *to)
865
char *actual_to = check_destination(file, to);
868
ret = recursive_operation(file, actual_to, copy_callback_pre, copy_single_file, NULL);
871
MEM_freeN(actual_to);
876
int BLI_create_symlink(const char *file, const char *to)
878
return symlink(to, file);
881
void BLI_dir_create_recursive(const char *dirname)
379
char tmp[MAXPATHLEN];
886
char static_buf[MAXPATHLEN];
381
891
if (BLI_exists(dirname)) return;
383
strcpy(tmp, dirname);
898
size = strlen(dirname)+1;
899
tmp = MEM_callocN(size, "BLI_dir_create_recursive tmp");
903
BLI_strncpy(tmp, dirname, size);
385
905
lslash= BLI_last_slash(tmp);
387
907
/* Split about the last slash and recurse */
389
BLI_recurdir_fileops(tmp);
909
BLI_dir_create_recursive(tmp);
392
915
mkdir(dirname, 0777);
395
int BLI_rename(char *from, char *to) {
918
int BLI_rename(const char *from, const char *to)
396
920
if (!BLI_exists(from)) return 0;
398
if (BLI_exists(to)) if(BLI_delete(to, 0, 0)) return 1;
923
if (BLI_delete(to, 0, 0)) return 1;
400
925
return rename(from, to);