Blender  V2.93
fileops.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include <stdlib.h> /* malloc */
25 #include <string.h>
26 
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include <errno.h>
32 
33 #include "zlib.h"
34 
35 #ifdef WIN32
36 # include "BLI_fileops_types.h"
37 # include "BLI_winstuff.h"
38 # include "utf_winfunc.h"
39 # include "utfconv.h"
40 # include <io.h>
41 # include <shellapi.h>
42 # include <shobjidl.h>
43 # include <windows.h>
44 #else
45 # if defined(__APPLE__)
46 # include <CoreFoundation/CoreFoundation.h>
47 # include <objc/message.h>
48 # include <objc/runtime.h>
49 # endif
50 # include <dirent.h>
51 # include <sys/param.h>
52 # include <sys/wait.h>
53 # include <unistd.h>
54 #endif
55 
56 #include "MEM_guardedalloc.h"
57 
58 #include "BLI_fileops.h"
59 #include "BLI_path_util.h"
60 #include "BLI_string.h"
61 #include "BLI_sys_types.h" /* for intptr_t support */
62 #include "BLI_utildefines.h"
63 
64 #if 0 /* UNUSED */
65 /* gzip the file in from and write it to "to".
66  * return -1 if zlib fails, -2 if the originating file does not exist
67  * note: will remove the "from" file
68  */
69 int BLI_file_gzip(const char *from, const char *to)
70 {
71  char buffer[10240];
72  int file;
73  int readsize = 0;
74  int rval = 0, err;
75  gzFile gzfile;
76 
77  /* level 1 is very close to 3 (the default) in terms of file size,
78  * but about twice as fast, best use for speedy saving - campbell */
79  gzfile = BLI_gzopen(to, "wb1");
80  if (gzfile == NULL) {
81  return -1;
82  }
83  file = BLI_open(from, O_BINARY | O_RDONLY, 0);
84  if (file == -1) {
85  return -2;
86  }
87 
88  while (1) {
89  readsize = read(file, buffer, sizeof(buffer));
90 
91  if (readsize < 0) {
92  rval = -2; /* error happened in reading */
93  fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
94  break;
95  }
96  else if (readsize == 0) {
97  break; /* done reading */
98  }
99 
100  if (gzwrite(gzfile, buffer, readsize) <= 0) {
101  rval = -1; /* error happened in writing */
102  fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err));
103  break;
104  }
105  }
106 
107  gzclose(gzfile);
108  close(file);
109 
110  return rval;
111 }
112 #endif
113 
114 /* gzip the file in from_file and write it to memory to_mem, at most size bytes.
115  * return the unzipped size
116  */
117 char *BLI_file_ungzip_to_mem(const char *from_file, int *r_size)
118 {
119  gzFile gzfile;
120  int readsize, size, alloc_size = 0;
121  char *mem = NULL;
122  const int chunk_size = 512 * 1024;
123 
124  size = 0;
125 
126  gzfile = BLI_gzopen(from_file, "rb");
127  for (;;) {
128  if (mem == NULL) {
129  mem = MEM_callocN(chunk_size, "BLI_ungzip_to_mem");
130  alloc_size = chunk_size;
131  }
132  else {
133  mem = MEM_reallocN(mem, size + chunk_size);
134  alloc_size += chunk_size;
135  }
136 
137  readsize = gzread(gzfile, mem + size, chunk_size);
138  if (readsize > 0) {
139  size += readsize;
140  }
141  else {
142  break;
143  }
144  }
145 
146  gzclose(gzfile);
147 
148  if (size == 0) {
149  MEM_freeN(mem);
150  mem = NULL;
151  }
152  else if (alloc_size != size) {
153  mem = MEM_reallocN(mem, size);
154  }
155 
156  *r_size = size;
157 
158  return mem;
159 }
160 
161 #define CHUNK (256 * 1024)
162 
163 /* gzip byte array from memory and write it to file at certain position.
164  * return size of gzip stream.
165  */
167  void *buf, size_t len, FILE *file, size_t gz_stream_offset, int compression_level)
168 {
169  int ret, flush;
170  unsigned have;
171  z_stream strm;
172  unsigned char out[CHUNK];
173 
174  fseek(file, gz_stream_offset, 0);
175 
176  strm.zalloc = Z_NULL;
177  strm.zfree = Z_NULL;
178  strm.opaque = Z_NULL;
179  ret = deflateInit(&strm, compression_level);
180  if (ret != Z_OK) {
181  return 0;
182  }
183 
184  strm.avail_in = len;
185  strm.next_in = (Bytef *)buf;
186  flush = Z_FINISH;
187 
188  do {
189  strm.avail_out = CHUNK;
190  strm.next_out = out;
191  ret = deflate(&strm, flush);
192  if (ret == Z_STREAM_ERROR) {
193  return 0;
194  }
195  have = CHUNK - strm.avail_out;
196  if (fwrite(out, 1, have, file) != have || ferror(file)) {
197  deflateEnd(&strm);
198  return 0;
199  }
200  } while (strm.avail_out == 0);
201 
202  if (strm.avail_in != 0 || ret != Z_STREAM_END) {
203  return 0;
204  }
205 
206  deflateEnd(&strm);
207  return (size_t)strm.total_out;
208 }
209 
210 /* read and decompress gzip stream from file at certain position to buffer.
211  * return size of decompressed data.
212  */
213 size_t BLI_ungzip_file_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t gz_stream_offset)
214 {
215  int ret;
216  z_stream strm;
217  size_t chunk = 256 * 1024;
218  unsigned char in[CHUNK];
219 
220  fseek(file, gz_stream_offset, 0);
221 
222  strm.zalloc = Z_NULL;
223  strm.zfree = Z_NULL;
224  strm.opaque = Z_NULL;
225  strm.avail_in = 0;
226  strm.next_in = Z_NULL;
227  ret = inflateInit(&strm);
228  if (ret != Z_OK) {
229  return 0;
230  }
231 
232  do {
233  strm.avail_in = fread(in, 1, chunk, file);
234  strm.next_in = in;
235  if (ferror(file)) {
236  inflateEnd(&strm);
237  return 0;
238  }
239 
240  do {
241  strm.avail_out = len;
242  strm.next_out = (Bytef *)buf + strm.total_out;
243 
244  ret = inflate(&strm, Z_NO_FLUSH);
245  if (ret == Z_STREAM_ERROR) {
246  return 0;
247  }
248  } while (strm.avail_out == 0);
249 
250  } while (ret != Z_STREAM_END);
251 
252  inflateEnd(&strm);
253  return (size_t)strm.total_out;
254 }
255 
256 #undef CHUNK
257 
265 bool BLI_file_is_writable(const char *filename)
266 {
267  bool writable;
268  if (BLI_access(filename, W_OK) == 0) {
269  /* file exists and I can write to it */
270  writable = true;
271  }
272  else if (errno != ENOENT) {
273  /* most likely file or containing directory cannot be accessed */
274  writable = false;
275  }
276  else {
277  /* file doesn't exist -- check I can create it in parent directory */
278  char parent[FILE_MAX];
279  BLI_split_dirfile(filename, parent, NULL, sizeof(parent), 0);
280 #ifdef WIN32
281  /* windows does not have X_OK */
282  writable = BLI_access(parent, W_OK) == 0;
283 #else
284  writable = BLI_access(parent, X_OK | W_OK) == 0;
285 #endif
286  }
287  return writable;
288 }
289 
294 bool BLI_file_touch(const char *file)
295 {
296  FILE *f = BLI_fopen(file, "r+b");
297 
298  if (f != NULL) {
299  int c = getc(f);
300 
301  if (c == EOF) {
302  /* Empty file, reopen in truncate write mode... */
303  fclose(f);
304  f = BLI_fopen(file, "w+b");
305  }
306  else {
307  /* Otherwise, rewrite first byte. */
308  rewind(f);
309  putc(c, f);
310  }
311  }
312  else {
313  f = BLI_fopen(file, "wb");
314  }
315  if (f) {
316  fclose(f);
317  return true;
318  }
319  return false;
320 }
321 
322 #ifdef WIN32
323 
324 static void callLocalErrorCallBack(const char *err)
325 {
326  printf("%s\n", err);
327 }
328 
329 FILE *BLI_fopen(const char *filename, const char *mode)
330 {
331  BLI_assert(!BLI_path_is_rel(filename));
332 
333  return ufopen(filename, mode);
334 }
335 
336 void BLI_get_short_name(char short_name[256], const char *filename)
337 {
338  wchar_t short_name_16[256];
339  int i = 0;
340 
341  UTF16_ENCODE(filename);
342 
343  GetShortPathNameW(filename_16, short_name_16, 256);
344 
345  for (i = 0; i < 256; i++) {
346  short_name[i] = (char)short_name_16[i];
347  }
348 
349  UTF16_UN_ENCODE(filename);
350 }
351 
352 void *BLI_gzopen(const char *filename, const char *mode)
353 {
354  gzFile gzfile;
355 
356  BLI_assert(!BLI_path_is_rel(filename));
357 
358  /* xxx Creates file before transcribing the path */
359  if (mode[0] == 'w') {
360  FILE *file = ufopen(filename, "a");
361  if (file == NULL) {
362  /* File couldn't be opened, e.g. due to permission error. */
363  return NULL;
364  }
365  fclose(file);
366  }
367 
368  /* temporary #if until we update all libraries to 1.2.7
369  * for correct wide char path handling */
370 # if ZLIB_VERNUM >= 0x1270
371  UTF16_ENCODE(filename);
372 
373  gzfile = gzopen_w(filename_16, mode);
374 
375  UTF16_UN_ENCODE(filename);
376 # else
377  {
378  char short_name[256];
379  BLI_get_short_name(short_name, filename);
380  gzfile = gzopen(short_name, mode);
381  }
382 # endif
383 
384  return gzfile;
385 }
386 
387 int BLI_open(const char *filename, int oflag, int pmode)
388 {
389  BLI_assert(!BLI_path_is_rel(filename));
390 
391  return uopen(filename, oflag, pmode);
392 }
393 
394 int BLI_access(const char *filename, int mode)
395 {
396  BLI_assert(!BLI_path_is_rel(filename));
397 
398  return uaccess(filename, mode);
399 }
400 
401 static bool delete_soft(const wchar_t *path_16, const char **error_message)
402 {
403  /* Deletes file or directory to recycling bin. The latter moves all contained files and
404  * directories recursively to the recycling bin as well. */
405  IFileOperation *pfo;
406  IShellItem *pSI;
407 
408  HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
409 
410  if (FAILED(hr)) {
411  *error_message = "Failed to initialize COM";
412  goto error_1;
413  }
414 
415  hr = CoCreateInstance(
416  &CLSID_FileOperation, NULL, CLSCTX_ALL, &IID_IFileOperation, (void **)&pfo);
417  if (FAILED(hr)) {
418  *error_message = "Failed to create FileOperation instance";
419  goto error_2;
420  }
421 
422  /* Flags for deletion:
423  * FOF_ALLOWUNDO: Enables moving file to recycling bin.
424  * FOF_SILENT: Don't show progress dialog box.
425  * FOF_WANTNUKEWARNING: Show dialog box if file can't be moved to recycling bin. */
426  hr = pfo->lpVtbl->SetOperationFlags(pfo, FOF_ALLOWUNDO | FOF_SILENT | FOF_WANTNUKEWARNING);
427 
428  if (FAILED(hr)) {
429  *error_message = "Failed to set operation flags";
430  goto error_2;
431  }
432 
433  hr = SHCreateItemFromParsingName(path_16, NULL, &IID_IShellItem, (void **)&pSI);
434  if (FAILED(hr)) {
435  *error_message = "Failed to parse path";
436  goto error_2;
437  }
438 
439  hr = pfo->lpVtbl->DeleteItem(pfo, pSI, NULL);
440  if (FAILED(hr)) {
441  *error_message = "Failed to prepare delete operation";
442  goto error_2;
443  }
444 
445  hr = pfo->lpVtbl->PerformOperations(pfo);
446 
447  if (FAILED(hr)) {
448  *error_message = "Failed to delete file or directory";
449  }
450 
451 error_2:
452  pfo->lpVtbl->Release(pfo);
453  CoUninitialize(); /* Has to be uninitialized when CoInitializeEx returns either S_OK or S_FALSE
454  */
455 error_1:
456  return FAILED(hr);
457 }
458 
459 static bool delete_unique(const char *path, const bool dir)
460 {
461  bool err;
462 
463  UTF16_ENCODE(path);
464 
465  if (dir) {
466  err = !RemoveDirectoryW(path_16);
467  if (err) {
468  printf("Unable to remove directory\n");
469  }
470  }
471  else {
472  err = !DeleteFileW(path_16);
473  if (err) {
474  callLocalErrorCallBack("Unable to delete file");
475  }
476  }
477 
478  UTF16_UN_ENCODE(path);
479 
480  return err;
481 }
482 
483 static bool delete_recursive(const char *dir)
484 {
485  struct direntry *filelist, *fl;
486  bool err = false;
487  uint nbr, i;
488 
489  i = nbr = BLI_filelist_dir_contents(dir, &filelist);
490  fl = filelist;
491  while (i--) {
492  const char *file = BLI_path_basename(fl->path);
493 
494  if (FILENAME_IS_CURRPAR(file)) {
495  /* Skip! */
496  }
497  else if (S_ISDIR(fl->type)) {
498  char path[FILE_MAXDIR];
499 
500  /* dir listing produces dir path without trailing slash... */
501  BLI_strncpy(path, fl->path, sizeof(path));
503 
504  if (delete_recursive(path)) {
505  err = true;
506  }
507  }
508  else {
509  if (delete_unique(fl->path, false)) {
510  err = true;
511  }
512  }
513  fl++;
514  }
515 
516  if (!err && delete_unique(dir, true)) {
517  err = true;
518  }
519 
520  BLI_filelist_free(filelist, nbr);
521 
522  return err;
523 }
524 
525 int BLI_delete(const char *file, bool dir, bool recursive)
526 {
527  int err;
528 
530 
531  if (recursive) {
532  err = delete_recursive(file);
533  }
534  else {
535  err = delete_unique(file, dir);
536  }
537 
538  return err;
539 }
540 
544 int BLI_delete_soft(const char *file, const char **error_message)
545 {
546  int err;
547 
549 
551 
552  err = delete_soft(file_16, error_message);
553 
555 
556  return err;
557 }
558 
559 /* Not used anywhere! */
560 # if 0
561 int BLI_move(const char *file, const char *to)
562 {
563  char str[MAXPATHLEN + 12];
564  int err;
565 
566  /* windows doesn't support moving to a directory
567  * it has to be 'mv filename filename' and not
568  * 'mv filename destination_directory' */
569 
570  BLI_strncpy(str, to, sizeof(str));
571  /* points 'to' to a directory ? */
572  if (BLI_path_slash_rfind(str) == (str + strlen(str) - 1)) {
573  if (BLI_path_slash_rfind(file) != NULL) {
574  strcat(str, BLI_path_slash_rfind(file) + 1);
575  }
576  }
577 
579  UTF16_ENCODE(str);
580  err = !MoveFileW(file_16, str_16);
583 
584  if (err) {
585  callLocalErrorCallBack("Unable to move file");
586  printf(" Move from '%s' to '%s' failed\n", file, str);
587  }
588 
589  return err;
590 }
591 # endif
592 
593 int BLI_copy(const char *file, const char *to)
594 {
595  char str[MAXPATHLEN + 12];
596  int err;
597 
598  /* windows doesn't support copying to a directory
599  * it has to be 'cp filename filename' and not
600  * 'cp filename destdir' */
601 
602  BLI_strncpy(str, to, sizeof(str));
603  /* points 'to' to a directory ? */
604  if (BLI_path_slash_rfind(str) == (str + strlen(str) - 1)) {
605  if (BLI_path_slash_rfind(file) != NULL) {
606  strcat(str, BLI_path_slash_rfind(file) + 1);
607  }
608  }
609 
611  UTF16_ENCODE(str);
612  err = !CopyFileW(file_16, str_16, false);
615 
616  if (err) {
617  callLocalErrorCallBack("Unable to copy file!");
618  printf(" Copy from '%s' to '%s' failed\n", file, str);
619  }
620 
621  return err;
622 }
623 
624 # if 0
625 int BLI_create_symlink(const char *file, const char *to)
626 {
627  /* See patch from T30870, should this ever become needed. */
628  callLocalErrorCallBack("Linking files is unsupported on Windows");
629  (void)file;
630  (void)to;
631  return 1;
632 }
633 # endif
634 
636 bool BLI_dir_create_recursive(const char *dirname)
637 {
638  char *lslash;
639  char tmp[MAXPATHLEN];
640  bool ret = true;
641 
642  /* First remove possible slash at the end of the dirname.
643  * This routine otherwise tries to create
644  * blah1/blah2/ (with slash) after creating
645  * blah1/blah2 (without slash) */
646 
647  BLI_strncpy(tmp, dirname, sizeof(tmp));
649 
650  /* check special case "c:\foo", don't try create "c:", harmless but prints an error below */
651  if (isalpha(tmp[0]) && (tmp[1] == ':') && tmp[2] == '\0') {
652  return true;
653  }
654 
655  if (BLI_is_dir(tmp)) {
656  return true;
657  }
658  else if (BLI_exists(tmp)) {
659  return false;
660  }
661 
662  lslash = (char *)BLI_path_slash_rfind(tmp);
663 
664  if (lslash) {
665  /* Split about the last slash and recurse */
666  *lslash = 0;
667  if (!BLI_dir_create_recursive(tmp)) {
668  ret = false;
669  }
670  }
671 
672  if (ret && dirname[0]) { /* patch, this recursive loop tries to create a nameless directory */
673  if (umkdir(dirname) == -1) {
674  printf("Unable to create directory %s\n", dirname);
675  ret = false;
676  }
677  }
678  return ret;
679 }
680 
681 int BLI_rename(const char *from, const char *to)
682 {
683  if (!BLI_exists(from)) {
684  return 0;
685  }
686 
687  /* make sure the filenames are different (case insensitive) before removing */
688  if (BLI_exists(to) && BLI_strcasecmp(from, to)) {
689  if (BLI_delete(to, false, false)) {
690  return 1;
691  }
692  }
693 
694  return urename(from, to);
695 }
696 
697 #else /* The UNIX world */
698 
699 /* results from recursive_operation and its callbacks */
700 enum {
701  /* operation succeeded */
703 
704  /* operation requested not to perform recursive digging for current path */
706 
707  /* error occurred in callback and recursive walking should stop immediately */
709 };
710 
711 typedef int (*RecursiveOp_Callback)(const char *from, const char *to);
712 
713 /* appending of filename to dir (ensures for buffer size before appending) */
714 static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, const char *file)
715 {
716  size_t len = strlen(dir) + strlen(file) + 1;
717 
718  if (*dst == NULL) {
719  *dst = MEM_mallocN(len + 1, "join_dirfile_alloc path");
720  }
721  else if (*alloc_len < len) {
722  *dst = MEM_reallocN(*dst, len + 1);
723  }
724 
725  *alloc_len = len;
726 
727  BLI_join_dirfile(*dst, len + 1, dir, file);
728 }
729 
730 static char *strip_last_slash(const char *dir)
731 {
732  char *result = BLI_strdup(dir);
734 
735  return result;
736 }
737 
751 static int recursive_operation(const char *startfrom,
752  const char *startto,
753  RecursiveOp_Callback callback_dir_pre,
754  RecursiveOp_Callback callback_file,
755  RecursiveOp_Callback callback_dir_post)
756 {
757  struct stat st;
758  char *from = NULL, *to = NULL;
759  char *from_path = NULL, *to_path = NULL;
760  struct dirent **dirlist = NULL;
761  size_t from_alloc_len = -1, to_alloc_len = -1;
762  int i, n = 0, ret = 0;
763 
764  do { /* once */
765  /* ensure there's no trailing slash in file path */
766  from = strip_last_slash(startfrom);
767  if (startto) {
768  to = strip_last_slash(startto);
769  }
770 
771  ret = lstat(from, &st);
772  if (ret < 0) {
773  /* source wasn't found, nothing to operate with */
774  break;
775  }
776 
777  if (!S_ISDIR(st.st_mode)) {
778  /* source isn't a directory, can't do recursive walking for it,
779  * so just call file callback and leave */
780  if (callback_file != NULL) {
781  ret = callback_file(from, to);
782  if (ret != RecursiveOp_Callback_OK) {
783  ret = -1;
784  }
785  }
786  break;
787  }
788 
789  n = scandir(startfrom, &dirlist, NULL, alphasort);
790  if (n < 0) {
791  /* error opening directory for listing */
792  perror("scandir");
793  ret = -1;
794  break;
795  }
796 
797  if (callback_dir_pre != NULL) {
798  ret = callback_dir_pre(from, to);
799  if (ret != RecursiveOp_Callback_OK) {
801  /* callback requested not to perform recursive walking, not an error */
802  ret = 0;
803  }
804  else {
805  ret = -1;
806  }
807  break;
808  }
809  }
810 
811  for (i = 0; i < n; i++) {
812  const struct dirent *const dirent = dirlist[i];
813 
815  continue;
816  }
817 
818  join_dirfile_alloc(&from_path, &from_alloc_len, from, dirent->d_name);
819  if (to) {
820  join_dirfile_alloc(&to_path, &to_alloc_len, to, dirent->d_name);
821  }
822 
823  bool is_dir;
824 
825 # ifdef __HAIKU__
826  {
827  struct stat st_dir;
828  char filename[FILE_MAX];
829  BLI_path_join(filename, sizeof(filename), startfrom, dirent->d_name, NULL);
830  lstat(filename, &st_dir);
831  is_dir = S_ISDIR(st_dir.st_mode);
832  }
833 # else
834  is_dir = (dirent->d_type == DT_DIR);
835 # endif
836 
837  if (is_dir) {
838  /* recursively dig into a subfolder */
840  from_path, to_path, callback_dir_pre, callback_file, callback_dir_post);
841  }
842  else if (callback_file != NULL) {
843  ret = callback_file(from_path, to_path);
844  if (ret != RecursiveOp_Callback_OK) {
845  ret = -1;
846  }
847  }
848 
849  if (ret != 0) {
850  break;
851  }
852  }
853  if (ret != 0) {
854  break;
855  }
856 
857  if (callback_dir_post != NULL) {
858  ret = callback_dir_post(from, to);
859  if (ret != RecursiveOp_Callback_OK) {
860  ret = -1;
861  }
862  }
863  } while (false);
864 
865  if (dirlist != NULL) {
866  for (i = 0; i < n; i++) {
867  free(dirlist[i]);
868  }
869  free(dirlist);
870  }
871  if (from_path != NULL) {
872  MEM_freeN(from_path);
873  }
874  if (to_path != NULL) {
875  MEM_freeN(to_path);
876  }
877  if (from != NULL) {
878  MEM_freeN(from);
879  }
880  if (to != NULL) {
881  MEM_freeN(to);
882  }
883 
884  return ret;
885 }
886 
887 static int delete_callback_post(const char *from, const char *UNUSED(to))
888 {
889  if (rmdir(from)) {
890  perror("rmdir");
891 
893  }
894 
896 }
897 
898 static int delete_single_file(const char *from, const char *UNUSED(to))
899 {
900  if (unlink(from)) {
901  perror("unlink");
902 
904  }
905 
907 }
908 
909 # ifdef __APPLE__
910 static int delete_soft(const char *file, const char **error_message)
911 {
912  int ret = -1;
913 
914  Class NSAutoreleasePoolClass = objc_getClass("NSAutoreleasePool");
915  SEL allocSel = sel_registerName("alloc");
916  SEL initSel = sel_registerName("init");
917  id poolAlloc = ((id(*)(Class, SEL))objc_msgSend)(NSAutoreleasePoolClass, allocSel);
918  id pool = ((id(*)(id, SEL))objc_msgSend)(poolAlloc, initSel);
919 
920  Class NSStringClass = objc_getClass("NSString");
921  SEL stringWithUTF8StringSel = sel_registerName("stringWithUTF8String:");
922  id pathString = ((id(*)(Class, SEL, const char *))objc_msgSend)(
923  NSStringClass, stringWithUTF8StringSel, file);
924 
925  Class NSFileManagerClass = objc_getClass("NSFileManager");
926  SEL defaultManagerSel = sel_registerName("defaultManager");
927  id fileManager = ((id(*)(Class, SEL))objc_msgSend)(NSFileManagerClass, defaultManagerSel);
928 
929  Class NSURLClass = objc_getClass("NSURL");
930  SEL fileURLWithPathSel = sel_registerName("fileURLWithPath:");
931  id nsurl = ((id(*)(Class, SEL, id))objc_msgSend)(NSURLClass, fileURLWithPathSel, pathString);
932 
933  SEL trashItemAtURLSel = sel_registerName("trashItemAtURL:resultingItemURL:error:");
934  BOOL deleteSuccessful = ((BOOL(*)(id, SEL, id, id, id))objc_msgSend)(
935  fileManager, trashItemAtURLSel, nsurl, nil, nil);
936 
937  if (deleteSuccessful) {
938  ret = 0;
939  }
940  else {
941  *error_message = "The Cocoa API call to delete file or directory failed";
942  }
943 
944  SEL drainSel = sel_registerName("drain");
945  ((void (*)(id, SEL))objc_msgSend)(pool, drainSel);
946 
947  return ret;
948 }
949 # else
950 static int delete_soft(const char *file, const char **error_message)
951 {
952  const char *args[5];
953  const char *process_failed;
954 
955  char *xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
956  char *xdg_session_desktop = getenv("XDG_SESSION_DESKTOP");
957 
958  if ((xdg_current_desktop != NULL && STREQ(xdg_current_desktop, "KDE")) ||
959  (xdg_session_desktop != NULL && STREQ(xdg_session_desktop, "KDE"))) {
960  args[0] = "kioclient5";
961  args[1] = "move";
962  args[2] = file;
963  args[3] = "trash:/";
964  args[4] = NULL;
965  process_failed = "kioclient5 reported failure";
966  }
967  else {
968  args[0] = "gio";
969  args[1] = "trash";
970  args[2] = file;
971  args[3] = NULL;
972  process_failed = "gio reported failure";
973  }
974 
975  int pid = fork();
976 
977  if (pid != 0) {
978  /* Parent process */
979  int wstatus = 0;
980 
981  waitpid(pid, &wstatus, 0);
982 
983  if (!WIFEXITED(wstatus)) {
984  *error_message =
985  "Blender may not support moving files or directories to trash on your system.";
986  return -1;
987  }
988  if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus)) {
989  *error_message = process_failed;
990  return -1;
991  }
992 
993  return 0;
994  }
995 
996  execvp(args[0], (char **)args);
997 
998  *error_message = "Forking process failed.";
999  return -1; /* This should only be reached if execvp fails and stack isn't replaced. */
1000 }
1001 # endif
1002 
1003 FILE *BLI_fopen(const char *filename, const char *mode)
1004 {
1005  BLI_assert(!BLI_path_is_rel(filename));
1006 
1007  return fopen(filename, mode);
1008 }
1009 
1010 void *BLI_gzopen(const char *filename, const char *mode)
1011 {
1012  BLI_assert(!BLI_path_is_rel(filename));
1013 
1014  return gzopen(filename, mode);
1015 }
1016 
1017 int BLI_open(const char *filename, int oflag, int pmode)
1018 {
1019  BLI_assert(!BLI_path_is_rel(filename));
1020 
1021  return open(filename, oflag, pmode);
1022 }
1023 
1024 int BLI_access(const char *filename, int mode)
1025 {
1026  BLI_assert(!BLI_path_is_rel(filename));
1027 
1028  return access(filename, mode);
1029 }
1030 
1037 int BLI_delete(const char *file, bool dir, bool recursive)
1038 {
1040 
1041  if (recursive) {
1043  }
1044  if (dir) {
1045  return rmdir(file);
1046  }
1047  return remove(file);
1048 }
1049 
1056 int BLI_delete_soft(const char *file, const char **error_message)
1057 {
1059 
1060  return delete_soft(file, error_message);
1061 }
1062 
1066 static bool check_the_same(const char *path_a, const char *path_b)
1067 {
1068  struct stat st_a, st_b;
1069 
1070  if (lstat(path_a, &st_a)) {
1071  return false;
1072  }
1073 
1074  if (lstat(path_b, &st_b)) {
1075  return false;
1076  }
1077 
1078  return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
1079 }
1080 
1084 static int set_permissions(const char *file, const struct stat *st)
1085 {
1086  if (chown(file, st->st_uid, st->st_gid)) {
1087  perror("chown");
1088  return -1;
1089  }
1090 
1091  if (chmod(file, st->st_mode)) {
1092  perror("chmod");
1093  return -1;
1094  }
1095 
1096  return 0;
1097 }
1098 
1099 /* pre-recursive callback for copying operation
1100  * creates a destination directory where all source content fill be copied to */
1101 static int copy_callback_pre(const char *from, const char *to)
1102 {
1103  struct stat st;
1104 
1105  if (check_the_same(from, to)) {
1106  fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
1108  }
1109 
1110  if (lstat(from, &st)) {
1111  perror("stat");
1113  }
1114 
1115  /* create a directory */
1116  if (mkdir(to, st.st_mode)) {
1117  perror("mkdir");
1119  }
1120 
1121  /* set proper owner and group on new directory */
1122  if (chown(to, st.st_uid, st.st_gid)) {
1123  perror("chown");
1125  }
1126 
1127  return RecursiveOp_Callback_OK;
1128 }
1129 
1130 static int copy_single_file(const char *from, const char *to)
1131 {
1132  FILE *from_stream, *to_stream;
1133  struct stat st;
1134  char buf[4096];
1135  size_t len;
1136 
1137  if (check_the_same(from, to)) {
1138  fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
1140  }
1141 
1142  if (lstat(from, &st)) {
1143  perror("lstat");
1145  }
1146 
1147  if (S_ISLNK(st.st_mode)) {
1148  /* symbolic links should be copied in special way */
1149  char *link_buffer;
1150  int need_free;
1151  ssize_t link_len;
1152 
1153  /* get large enough buffer to read link content */
1154  if ((st.st_size + 1) < sizeof(buf)) {
1155  link_buffer = buf;
1156  need_free = 0;
1157  }
1158  else {
1159  link_buffer = MEM_callocN(st.st_size + 2, "copy_single_file link_buffer");
1160  need_free = 1;
1161  }
1162 
1163  link_len = readlink(from, link_buffer, st.st_size + 1);
1164  if (link_len < 0) {
1165  perror("readlink");
1166 
1167  if (need_free) {
1168  MEM_freeN(link_buffer);
1169  }
1170 
1172  }
1173 
1174  link_buffer[link_len] = '\0';
1175 
1176  if (symlink(link_buffer, to)) {
1177  perror("symlink");
1178  if (need_free) {
1179  MEM_freeN(link_buffer);
1180  }
1182  }
1183 
1184  if (need_free) {
1185  MEM_freeN(link_buffer);
1186  }
1187 
1188  return RecursiveOp_Callback_OK;
1189  }
1190  if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
1191  /* copy special type of file */
1192  if (mknod(to, st.st_mode, st.st_rdev)) {
1193  perror("mknod");
1195  }
1196 
1197  if (set_permissions(to, &st)) {
1199  }
1200 
1201  return RecursiveOp_Callback_OK;
1202  }
1203  if (!S_ISREG(st.st_mode)) {
1204  fprintf(stderr, "Copying of this kind of files isn't supported yet\n");
1206  }
1207 
1208  from_stream = fopen(from, "rb");
1209  if (!from_stream) {
1210  perror("fopen");
1212  }
1213 
1214  to_stream = fopen(to, "wb");
1215  if (!to_stream) {
1216  perror("fopen");
1217  fclose(from_stream);
1219  }
1220 
1221  while ((len = fread(buf, 1, sizeof(buf), from_stream)) > 0) {
1222  fwrite(buf, 1, len, to_stream);
1223  }
1224 
1225  fclose(to_stream);
1226  fclose(from_stream);
1227 
1228  if (set_permissions(to, &st)) {
1230  }
1231 
1232  return RecursiveOp_Callback_OK;
1233 }
1234 
1235 /* Not used anywhere! */
1236 # if 0
1237 static int move_callback_pre(const char *from, const char *to)
1238 {
1239  int ret = rename(from, to);
1240 
1241  if (ret) {
1242  return copy_callback_pre(from, to);
1243  }
1244 
1246 }
1247 
1248 static int move_single_file(const char *from, const char *to)
1249 {
1250  int ret = rename(from, to);
1251 
1252  if (ret) {
1253  return copy_single_file(from, to);
1254  }
1255 
1256  return RecursiveOp_Callback_OK;
1257 }
1258 
1259 /* if *file represents a directory, moves all its contents into *to, else renames
1260  * file itself to *to. */
1261 int BLI_move(const char *file, const char *to)
1262 {
1263  int ret = recursive_operation(file, to, move_callback_pre, move_single_file, NULL);
1264 
1265  if (ret && ret != -1) {
1267  }
1268 
1269  return ret;
1270 }
1271 # endif
1272 
1273 static const char *check_destination(const char *file, const char *to)
1274 {
1275  struct stat st;
1276 
1277  if (!stat(to, &st)) {
1278  if (S_ISDIR(st.st_mode)) {
1279  char *str, *path;
1280  const char *filename;
1281  size_t len = 0;
1282 
1284  filename = BLI_path_slash_rfind(str);
1285 
1286  if (!filename) {
1287  MEM_freeN(str);
1288  return (char *)to;
1289  }
1290 
1291  /* skip slash */
1292  filename += 1;
1293 
1294  len = strlen(to) + strlen(filename) + 1;
1295  path = MEM_callocN(len + 1, "check_destination path");
1296  BLI_join_dirfile(path, len + 1, to, filename);
1297 
1298  MEM_freeN(str);
1299 
1300  return path;
1301  }
1302  }
1303 
1304  return to;
1305 }
1306 
1307 int BLI_copy(const char *file, const char *to)
1308 {
1309  const char *actual_to = check_destination(file, to);
1310  int ret;
1311 
1313 
1314  if (actual_to != to) {
1315  MEM_freeN((void *)actual_to);
1316  }
1317 
1318  return ret;
1319 }
1320 
1321 # if 0
1322 int BLI_create_symlink(const char *file, const char *to)
1323 {
1324  return symlink(to, file);
1325 }
1326 # endif
1327 
1330 {
1331  char *lslash;
1332  size_t size;
1333 # ifdef MAXPATHLEN
1334  char static_buf[MAXPATHLEN];
1335 # endif
1336  char *tmp;
1337  bool ret = true;
1338 
1339  if (BLI_is_dir(dirname)) {
1340  return true;
1341  }
1342  if (BLI_exists(dirname)) {
1343  return false;
1344  }
1345 
1346 # ifdef MAXPATHLEN
1347  size = MAXPATHLEN;
1348  tmp = static_buf;
1349 # else
1350  size = strlen(dirname) + 1;
1351  tmp = MEM_callocN(size, __func__);
1352 # endif
1353 
1354  BLI_strncpy(tmp, dirname, size);
1355 
1356  /* Avoids one useless recursion in case of '/foo/bar/' path... */
1357  BLI_path_slash_rstrip(tmp);
1358 
1359  lslash = (char *)BLI_path_slash_rfind(tmp);
1360  if (lslash) {
1361  /* Split about the last slash and recurse */
1362  *lslash = 0;
1363  if (!BLI_dir_create_recursive(tmp)) {
1364  ret = false;
1365  }
1366  }
1367 
1368 # ifndef MAXPATHLEN
1369  MEM_freeN(tmp);
1370 # endif
1371 
1372  if (ret) {
1373  ret = (mkdir(dirname, 0777) == 0);
1374  }
1375  return ret;
1376 }
1377 
1381 int BLI_rename(const char *from, const char *to)
1382 {
1383  if (!BLI_exists(from)) {
1384  return 1;
1385  }
1386 
1387  if (BLI_exists(to)) {
1388  if (BLI_delete(to, false, false)) {
1389  return 1;
1390  }
1391  }
1392 
1393  return rename(from, to);
1394 }
1395 
1396 #endif
#define BLI_assert(a)
Definition: BLI_assert.h:58
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:349
#define O_BINARY
Definition: BLI_fileops.h:182
void BLI_filelist_free(struct direntry *filelist, const unsigned int nrentries)
Definition: BLI_filelist.c:467
unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **r_filelist)
Definition: BLI_filelist.c:238
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:436
Some types for dealing with directories.
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1868
bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:411
#define FILE_MAX
void BLI_join_dirfile(char *__restrict dst, const size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1737
#define FILENAME_IS_CURRPAR(_n)
size_t BLI_path_join(char *__restrict dst, const size_t dst_len, const char *path_first,...) ATTR_NONNULL(1
void BLI_path_slash_rstrip(char *string) ATTR_NONNULL()
Definition: path_util.c:1995
int BLI_path_slash_ensure(char *string) ATTR_NONNULL()
Definition: path_util.c:1981
const char * BLI_path_slash_rfind(const char *string) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1962
void BLI_split_dirfile(const char *string, char *dir, char *file, const size_t dirlen, const size_t filelen)
Definition: path_util.c:1654
#define FILE_MAXDIR
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:666
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define STREQ(a, b)
Compatibility-like things for windows.
SSIZE_T ssize_t
Definition: BLI_winstuff.h:87
#define S_ISDIR(x)
Definition: BLI_winstuff.h:64
const char * dirname(char *path)
#define MAXPATHLEN
Definition: BLI_winstuff.h:58
#define S_ISREG(x)
Definition: BLI_winstuff.h:61
Read Guarded memory(de)allocation.
#define MEM_reallocN(vmemh, len)
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
StackEntry * from
FILE * file
#define str(s)
static int set_permissions(const char *file, const struct stat *st)
Definition: fileops.c:1084
int BLI_delete_soft(const char *file, const char **error_message)
Definition: fileops.c:1056
char * BLI_file_ungzip_to_mem(const char *from_file, int *r_size)
Definition: fileops.c:117
#define CHUNK
Definition: fileops.c:161
FILE * BLI_fopen(const char *filename, const char *mode)
Definition: fileops.c:1003
static bool check_the_same(const char *path_a, const char *path_b)
Definition: fileops.c:1066
size_t BLI_ungzip_file_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t gz_stream_offset)
Definition: fileops.c:213
int BLI_open(const char *filename, int oflag, int pmode)
Definition: fileops.c:1017
bool BLI_file_is_writable(const char *filename)
Definition: fileops.c:265
int BLI_access(const char *filename, int mode)
Definition: fileops.c:1024
int(* RecursiveOp_Callback)(const char *from, const char *to)
Definition: fileops.c:711
static const char * check_destination(const char *file, const char *to)
Definition: fileops.c:1273
void * BLI_gzopen(const char *filename, const char *mode)
Definition: fileops.c:1010
int BLI_copy(const char *file, const char *to)
Definition: fileops.c:1307
bool BLI_dir_create_recursive(const char *dirname)
Definition: fileops.c:1329
static char * strip_last_slash(const char *dir)
Definition: fileops.c:730
static int copy_single_file(const char *from, const char *to)
Definition: fileops.c:1130
static int delete_single_file(const char *from, const char *UNUSED(to))
Definition: fileops.c:898
static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, const char *file)
Definition: fileops.c:714
bool BLI_file_touch(const char *file)
Definition: fileops.c:294
static int recursive_operation(const char *startfrom, const char *startto, RecursiveOp_Callback callback_dir_pre, RecursiveOp_Callback callback_file, RecursiveOp_Callback callback_dir_post)
Definition: fileops.c:751
size_t BLI_gzip_mem_to_file_at_pos(void *buf, size_t len, FILE *file, size_t gz_stream_offset, int compression_level)
Definition: fileops.c:166
@ RecursiveOp_Callback_StopRecurs
Definition: fileops.c:705
@ RecursiveOp_Callback_OK
Definition: fileops.c:702
@ RecursiveOp_Callback_Error
Definition: fileops.c:708
static int delete_callback_post(const char *from, const char *UNUSED(to))
Definition: fileops.c:887
int BLI_rename(const char *from, const char *to)
Definition: fileops.c:1381
static int delete_soft(const char *file, const char **error_message)
Definition: fileops.c:950
static int copy_callback_pre(const char *from, const char *to)
Definition: fileops.c:1101
int BLI_delete(const char *file, bool dir, bool recursive)
Definition: fileops.c:1037
static FT_Error err
Definition: freetypefont.c:52
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned c
Definition: RandGen.cpp:97
return ret
char * d_name
Definition: BLI_winstuff.h:96
const char * path
int umkdir(const char *pathname)
Definition: utf_winfunc.c:101
FILE * ufopen(const char *filename, const char *mode)
Definition: utf_winfunc.c:30
int uopen(const char *filename, int oflag, int pmode)
Definition: utf_winfunc.c:52
int uaccess(const char *filename, int mode)
Definition: utf_winfunc.c:72
int urename(const char *oldname, const char *newname)
Definition: utf_winfunc.c:86
#define UTF16_ENCODE(in8str)
Definition: utfconv.h:96
#define UTF16_UN_ENCODE(in8str)
Definition: utfconv.h:100
uint len