Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fileops.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 838644054afe682dbfc3dd3c7590856380b6796d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 */

/** \file
 * \ingroup bli
 */

#include <stdlib.h> /* malloc */
#include <string.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <errno.h>

#include <zlib.h>
#include <zstd.h>

#ifdef WIN32
#  include "BLI_fileops_types.h"
#  include "BLI_winstuff.h"
#  include "utf_winfunc.h"
#  include "utfconv.h"
#  include <io.h>
#  include <shellapi.h>
#  include <shobjidl.h>
#  include <windows.h>
#else
#  if defined(__APPLE__)
#    include <CoreFoundation/CoreFoundation.h>
#    include <objc/message.h>
#    include <objc/runtime.h>
#  endif
#  include <dirent.h>
#  include <sys/param.h>
#  include <sys/wait.h>
#  include <unistd.h>
#endif

#include "MEM_guardedalloc.h"

#include "BLI_fileops.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_sys_types.h" /* for intptr_t support */
#include "BLI_utildefines.h"

size_t BLI_file_zstd_from_mem_at_pos(
    void *buf, size_t len, FILE *file, size_t file_offset, int compression_level)
{
  fseek(file, file_offset, SEEK_SET);

  ZSTD_CCtx *ctx = ZSTD_createCCtx();
  ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, compression_level);

  ZSTD_inBuffer input = {buf, len, 0};

  size_t out_len = ZSTD_CStreamOutSize();
  void *out_buf = MEM_mallocN(out_len, __func__);
  size_t total_written = 0;

  /* Compress block and write it out until the input has been consumed. */
  while (input.pos < input.size) {
    ZSTD_outBuffer output = {out_buf, out_len, 0};
    size_t ret = ZSTD_compressStream2(ctx, &output, &input, ZSTD_e_continue);
    if (ZSTD_isError(ret)) {
      break;
    }
    if (fwrite(out_buf, 1, output.pos, file) != output.pos) {
      break;
    }
    total_written += output.pos;
  }

  /* Finalize the `Zstd` frame. */
  size_t ret = 1;
  while (ret != 0) {
    ZSTD_outBuffer output = {out_buf, out_len, 0};
    ret = ZSTD_compressStream2(ctx, &output, &input, ZSTD_e_end);
    if (ZSTD_isError(ret)) {
      break;
    }
    if (fwrite(out_buf, 1, output.pos, file) != output.pos) {
      break;
    }
    total_written += output.pos;
  }

  MEM_freeN(out_buf);
  ZSTD_freeCCtx(ctx);

  return ZSTD_isError(ret) ? 0 : total_written;
}

size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset)
{
  fseek(file, file_offset, SEEK_SET);

  ZSTD_DCtx *ctx = ZSTD_createDCtx();

  size_t in_len = ZSTD_DStreamInSize();
  void *in_buf = MEM_mallocN(in_len, __func__);
  ZSTD_inBuffer input = {in_buf, in_len, 0};

  ZSTD_outBuffer output = {buf, len, 0};

  size_t ret = 0;
  /* Read and decompress chunks of input data until we have enough output. */
  while (output.pos < output.size && !ZSTD_isError(ret)) {
    input.size = fread(in_buf, 1, in_len, file);
    if (input.size == 0) {
      break;
    }

    /* Consume input data until we run out or have enough output. */
    input.pos = 0;
    while (input.pos < input.size && output.pos < output.size) {
      ret = ZSTD_decompressStream(ctx, &output, &input);

      if (ZSTD_isError(ret)) {
        break;
      }
    }
  }

  MEM_freeN(in_buf);
  ZSTD_freeDCtx(ctx);

  return ZSTD_isError(ret) ? 0 : output.pos;
}

bool BLI_file_magic_is_gzip(const char header[4])
{
  /* GZIP itself starts with the magic bytes 0x1f 0x8b.
   * The third byte indicates the compression method, which is 0x08 for DEFLATE. */
  return header[0] == 0x1f && header[1] == 0x8b && header[2] == 0x08;
}

bool BLI_file_magic_is_zstd(const char header[4])
{
  /* ZSTD files consist of concatenated frames, each either a Zstd frame or a skippable frame.
   * Both types of frames start with a magic number: 0xFD2FB528 for Zstd frames and 0x184D2A5*
   * for skippable frames, with the * being anything from 0 to F.
   *
   * To check whether a file is Zstd-compressed, we just check whether the first frame matches
   * either. Seeking through the file until a Zstd frame is found would make things more
   * complicated and the probability of a false positive is rather low anyways.
   *
   * Note that LZ4 uses a compatible format, so even though its compressed frames have a
   * different magic number, a valid LZ4 file might also start with a skippable frame matching
   * the second check here.
   *
   * For more details, see https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
   */

  uint32_t magic = *((uint32_t *)header);
  if (magic == 0xFD2FB528) {
    return true;
  }
  if ((magic >> 4) == 0x184D2A5) {
    return true;
  }
  return false;
}

bool BLI_file_is_writable(const char *filename)
{
  bool writable;
  if (BLI_access(filename, W_OK) == 0) {
    /* file exists and I can write to it */
    writable = true;
  }
  else if (errno != ENOENT) {
    /* most likely file or containing directory cannot be accessed */
    writable = false;
  }
  else {
    /* file doesn't exist -- check I can create it in parent directory */
    char parent[FILE_MAX];
    BLI_split_dirfile(filename, parent, NULL, sizeof(parent), 0);
#ifdef WIN32
    /* windows does not have X_OK */
    writable = BLI_access(parent, W_OK) == 0;
#else
    writable = BLI_access(parent, X_OK | W_OK) == 0;
#endif
  }
  return writable;
}

bool BLI_file_touch(const char *file)
{
  FILE *f = BLI_fopen(file, "r+b");

  if (f != NULL) {
    int c = getc(f);

    if (c == EOF) {
      /* Empty file, reopen in truncate write mode... */
      fclose(f);
      f = BLI_fopen(file, "w+b");
    }
    else {
      /* Otherwise, rewrite first byte. */
      rewind(f);
      putc(c, f);
    }
  }
  else {
    f = BLI_fopen(file, "wb");
  }
  if (f) {
    fclose(f);
    return true;
  }
  return false;
}

#ifdef WIN32

static void callLocalErrorCallBack(const char *err)
{
  printf("%s\n", err);
}

FILE *BLI_fopen(const char *filename, const char *mode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return ufopen(filename, mode);
}

void BLI_get_short_name(char short_name[256], const char *filename)
{
  wchar_t short_name_16[256];
  int i = 0;

  UTF16_ENCODE(filename);

  GetShortPathNameW(filename_16, short_name_16, 256);

  for (i = 0; i < 256; i++) {
    short_name[i] = (char)short_name_16[i];
  }

  UTF16_UN_ENCODE(filename);
}

void *BLI_gzopen(const char *filename, const char *mode)
{
  gzFile gzfile;

  BLI_assert(!BLI_path_is_rel(filename));

  /* XXX: Creates file before transcribing the path. */
  if (mode[0] == 'w') {
    FILE *file = ufopen(filename, "a");
    if (file == NULL) {
      /* File couldn't be opened, e.g. due to permission error. */
      return NULL;
    }
    fclose(file);
  }

  /* temporary #if until we update all libraries to 1.2.7
   * for correct wide char path handling */
#  if ZLIB_VERNUM >= 0x1270
  UTF16_ENCODE(filename);

  gzfile = gzopen_w(filename_16, mode);

  UTF16_UN_ENCODE(filename);
#  else
  {
    char short_name[256];
    BLI_get_short_name(short_name, filename);
    gzfile = gzopen(short_name, mode);
  }
#  endif

  return gzfile;
}

int BLI_open(const char *filename, int oflag, int pmode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return uopen(filename, oflag, pmode);
}

int BLI_access(const char *filename, int mode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return uaccess(filename, mode);
}

static bool delete_soft(const wchar_t *path_16, const char **error_message)
{
  /* Deletes file or directory to recycling bin. The latter moves all contained files and
   * directories recursively to the recycling bin as well. */
  IFileOperation *pfo;
  IShellItem *pSI;

  HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

  if (FAILED(hr)) {
    *error_message = "Failed to initialize COM";
    goto error_1;
  }

  hr = CoCreateInstance(
      &CLSID_FileOperation, NULL, CLSCTX_ALL, &IID_IFileOperation, (void **)&pfo);
  if (FAILED(hr)) {
    *error_message = "Failed to create FileOperation instance";
    goto error_2;
  }

  /* Flags for deletion:
   * FOF_ALLOWUNDO: Enables moving file to recycling bin.
   * FOF_SILENT: Don't show progress dialog box.
   * FOF_WANTNUKEWARNING: Show dialog box if file can't be moved to recycling bin. */
  hr = pfo->lpVtbl->SetOperationFlags(pfo, FOF_ALLOWUNDO | FOF_SILENT | FOF_WANTNUKEWARNING);

  if (FAILED(hr)) {
    *error_message = "Failed to set operation flags";
    goto error_2;
  }

  hr = SHCreateItemFromParsingName(path_16, NULL, &IID_IShellItem, (void **)&pSI);
  if (FAILED(hr)) {
    *error_message = "Failed to parse path";
    goto error_2;
  }

  hr = pfo->lpVtbl->DeleteItem(pfo, pSI, NULL);
  if (FAILED(hr)) {
    *error_message = "Failed to prepare delete operation";
    goto error_2;
  }

  hr = pfo->lpVtbl->PerformOperations(pfo);

  if (FAILED(hr)) {
    *error_message = "Failed to delete file or directory";
  }

error_2:
  pfo->lpVtbl->Release(pfo);
  CoUninitialize(); /* Has to be uninitialized when CoInitializeEx returns either S_OK or S_FALSE
                     */
error_1:
  return FAILED(hr);
}

static bool delete_unique(const char *path, const bool dir)
{
  bool err;

  UTF16_ENCODE(path);

  if (dir) {
    err = !RemoveDirectoryW(path_16);
    if (err) {
      printf("Unable to remove directory\n");
    }
  }
  else {
    err = !DeleteFileW(path_16);
    if (err) {
      callLocalErrorCallBack("Unable to delete file");
    }
  }

  UTF16_UN_ENCODE(path);

  return err;
}

static bool delete_recursive(const char *dir)
{
  struct direntry *filelist, *fl;
  bool err = false;
  uint nbr, i;

  i = nbr = BLI_filelist_dir_contents(dir, &filelist);
  fl = filelist;
  while (i--) {
    const char *file = BLI_path_basename(fl->path);

    if (FILENAME_IS_CURRPAR(file)) {
      /* Skip! */
    }
    else if (S_ISDIR(fl->type)) {
      char path[FILE_MAXDIR];

      /* dir listing produces dir path without trailing slash... */
      BLI_strncpy(path, fl->path, sizeof(path));
      BLI_path_slash_ensure(path);

      if (delete_recursive(path)) {
        err = true;
      }
    }
    else {
      if (delete_unique(fl->path, false)) {
        err = true;
      }
    }
    fl++;
  }

  if (!err && delete_unique(dir, true)) {
    err = true;
  }

  BLI_filelist_free(filelist, nbr);

  return err;
}

int BLI_delete(const char *file, bool dir, bool recursive)
{
  int err;

  BLI_assert(!BLI_path_is_rel(file));

  if (recursive) {
    err = delete_recursive(file);
  }
  else {
    err = delete_unique(file, dir);
  }

  return err;
}

/**
 * Moves the files or directories to the recycling bin.
 */
int BLI_delete_soft(const char *file, const char **error_message)
{
  int err;

  BLI_assert(!BLI_path_is_rel(file));

  UTF16_ENCODE(file);

  err = delete_soft(file_16, error_message);

  UTF16_UN_ENCODE(file);

  return err;
}

/* Not used anywhere! */
#  if 0
int BLI_move(const char *file, const char *to)
{
  char str[MAXPATHLEN + 12];
  int err;

  /* windows doesn't support moving to a directory
   * it has to be 'mv filename filename' and not
   * 'mv filename destination_directory' */

  BLI_strncpy(str, to, sizeof(str));
  /* points 'to' to a directory ? */
  if (BLI_path_slash_rfind(str) == (str + strlen(str) - 1)) {
    if (BLI_path_slash_rfind(file) != NULL) {
      strcat(str, BLI_path_slash_rfind(file) + 1);
    }
  }

  UTF16_ENCODE(file);
  UTF16_ENCODE(str);
  err = !MoveFileW(file_16, str_16);
  UTF16_UN_ENCODE(str);
  UTF16_UN_ENCODE(file);

  if (err) {
    callLocalErrorCallBack("Unable to move file");
    printf(" Move from '%s' to '%s' failed\n", file, str);
  }

  return err;
}
#  endif

int BLI_copy(const char *file, const char *to)
{
  char str[MAXPATHLEN + 12];
  int err;

  /* windows doesn't support copying to a directory
   * it has to be 'cp filename filename' and not
   * 'cp filename destdir' */

  BLI_strncpy(str, to, sizeof(str));
  /* points 'to' to a directory ? */
  if (BLI_path_slash_rfind(str) == (str + strlen(str) - 1)) {
    if (BLI_path_slash_rfind(file) != NULL) {
      strcat(str, BLI_path_slash_rfind(file) + 1);
    }
  }

  UTF16_ENCODE(file);
  UTF16_ENCODE(str);
  err = !CopyFileW(file_16, str_16, false);
  UTF16_UN_ENCODE(str);
  UTF16_UN_ENCODE(file);

  if (err) {
    callLocalErrorCallBack("Unable to copy file!");
    printf(" Copy from '%s' to '%s' failed\n", file, str);
  }

  return err;
}

#  if 0
int BLI_create_symlink(const char *file, const char *to)
{
  /* See patch from T30870, should this ever become needed. */
  callLocalErrorCallBack("Linking files is unsupported on Windows");
  (void)file;
  (void)to;
  return 1;
}
#  endif

/** \return true on success (i.e. given path now exists on FS), false otherwise. */
bool BLI_dir_create_recursive(const char *dirname)
{
  char *lslash;
  char tmp[MAXPATHLEN];
  bool ret = true;

  /* First remove possible slash at the end of the dirname.
   * This routine otherwise tries to create
   * blah1/blah2/ (with slash) after creating
   * blah1/blah2 (without slash) */

  BLI_strncpy(tmp, dirname, sizeof(tmp));
  BLI_path_slash_native(tmp);
  BLI_path_slash_rstrip(tmp);

  /* check special case "c:\foo", don't try create "c:", harmless but prints an error below */
  if (isalpha(tmp[0]) && (tmp[1] == ':') && tmp[2] == '\0') {
    return true;
  }

  if (BLI_is_dir(tmp)) {
    return true;
  }
  else if (BLI_exists(tmp)) {
    return false;
  }

  lslash = (char *)BLI_path_slash_rfind(tmp);

  if (lslash) {
    /* Split about the last slash and recurse */
    *lslash = 0;
    if (!BLI_dir_create_recursive(tmp)) {
      ret = false;
    }
  }

  if (ret && dirname[0]) { /* patch, this recursive loop tries to create a nameless directory */
    if (umkdir(dirname) == -1) {
      printf("Unable to create directory %s\n", dirname);
      ret = false;
    }
  }
  return ret;
}

int BLI_rename(const char *from, const char *to)
{
  if (!BLI_exists(from)) {
    return 0;
  }

  /* make sure the filenames are different (case insensitive) before removing */
  if (BLI_exists(to) && BLI_strcasecmp(from, to)) {
    if (BLI_delete(to, false, false)) {
      return 1;
    }
  }

  return urename(from, to);
}

#else /* The UNIX world */

/* results from recursive_operation and its callbacks */
enum {
  /* operation succeeded */
  RecursiveOp_Callback_OK = 0,

  /* operation requested not to perform recursive digging for current path */
  RecursiveOp_Callback_StopRecurs = 1,

  /* error occurred in callback and recursive walking should stop immediately */
  RecursiveOp_Callback_Error = 2,
};

typedef int (*RecursiveOp_Callback)(const char *from, const char *to);

/* appending of filename to dir (ensures for buffer size before appending) */
static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, const char *file)
{
  size_t len = strlen(dir) + strlen(file) + 1;

  if (*dst == NULL) {
    *dst = MEM_mallocN(len + 1, "join_dirfile_alloc path");
  }
  else if (*alloc_len < len) {
    *dst = MEM_reallocN(*dst, len + 1);
  }

  *alloc_len = len;

  BLI_join_dirfile(*dst, len + 1, dir, file);
}

static char *strip_last_slash(const char *dir)
{
  char *result = BLI_strdup(dir);
  BLI_path_slash_rstrip(result);

  return result;
}

/**
 * Scans \a startfrom, generating a corresponding destination name for each item found by
 * prefixing it with startto, recursively scanning subdirectories, and invoking the specified
 * callbacks for files and subdirectories found as appropriate.
 *
 * \param startfrom: Top-level source path.
 * \param startto: Top-level destination path.
 * \param callback_dir_pre: Optional, to be invoked before entering a subdirectory, can return
 *                          RecursiveOp_Callback_StopRecurs to skip the subdirectory.
 * \param callback_file: Optional, to be invoked on each file found.
 * \param callback_dir_post: optional, to be invoked after leaving a subdirectory.
 * \return
 */
static int recursive_operation(const char *startfrom,
                               const char *startto,
                               RecursiveOp_Callback callback_dir_pre,
                               RecursiveOp_Callback callback_file,
                               RecursiveOp_Callback callback_dir_post)
{
  struct stat st;
  char *from = NULL, *to = NULL;
  char *from_path = NULL, *to_path = NULL;
  struct dirent **dirlist = NULL;
  size_t from_alloc_len = -1, to_alloc_len = -1;
  int i, n = 0, ret = 0;

  do { /* once */
    /* ensure there's no trailing slash in file path */
    from = strip_last_slash(startfrom);
    if (startto) {
      to = strip_last_slash(startto);
    }

    ret = lstat(from, &st);
    if (ret < 0) {
      /* source wasn't found, nothing to operate with */
      break;
    }

    if (!S_ISDIR(st.st_mode)) {
      /* source isn't a directory, can't do recursive walking for it,
       * so just call file callback and leave */
      if (callback_file != NULL) {
        ret = callback_file(from, to);
        if (ret != RecursiveOp_Callback_OK) {
          ret = -1;
        }
      }
      break;
    }

    n = scandir(startfrom, &dirlist, NULL, alphasort);
    if (n < 0) {
      /* error opening directory for listing */
      perror("scandir");
      ret = -1;
      break;
    }

    if (callback_dir_pre != NULL) {
      ret = callback_dir_pre(from, to);
      if (ret != RecursiveOp_Callback_OK) {
        if (ret == RecursiveOp_Callback_StopRecurs) {
          /* callback requested not to perform recursive walking, not an error */
          ret = 0;
        }
        else {
          ret = -1;
        }
        break;
      }
    }

    for (i = 0; i < n; i++) {
      const struct dirent *const dirent = dirlist[i];

      if (FILENAME_IS_CURRPAR(dirent->d_name)) {
        continue;
      }

      join_dirfile_alloc(&from_path, &from_alloc_len, from, dirent->d_name);
      if (to) {
        join_dirfile_alloc(&to_path, &to_alloc_len, to, dirent->d_name);
      }

      bool is_dir;

#  ifdef __HAIKU__
      {
        struct stat st_dir;
        char filename[FILE_MAX];
        BLI_path_join(filename, sizeof(filename), startfrom, dirent->d_name, NULL);
        lstat(filename, &st_dir);
        is_dir = S_ISDIR(st_dir.st_mode);
      }
#  else
      is_dir = (dirent->d_type == DT_DIR);
#  endif

      if (is_dir) {
        /* Recurse into sub-directories. */
        ret = recursive_operation(
            from_path, to_path, callback_dir_pre, callback_file, callback_dir_post);
      }
      else if (callback_file != NULL) {
        ret = callback_file(from_path, to_path);
        if (ret != RecursiveOp_Callback_OK) {
          ret = -1;
        }
      }

      if (ret != 0) {
        break;
      }
    }
    if (ret != 0) {
      break;
    }

    if (callback_dir_post != NULL) {
      ret = callback_dir_post(from, to);
      if (ret != RecursiveOp_Callback_OK) {
        ret = -1;
      }
    }
  } while (false);

  if (dirlist != NULL) {
    for (i = 0; i < n; i++) {
      free(dirlist[i]);
    }
    free(dirlist);
  }
  if (from_path != NULL) {
    MEM_freeN(from_path);
  }
  if (to_path != NULL) {
    MEM_freeN(to_path);
  }
  if (from != NULL) {
    MEM_freeN(from);
  }
  if (to != NULL) {
    MEM_freeN(to);
  }

  return ret;
}

static int delete_callback_post(const char *from, const char *UNUSED(to))
{
  if (rmdir(from)) {
    perror("rmdir");

    return RecursiveOp_Callback_Error;
  }

  return RecursiveOp_Callback_OK;
}

static int delete_single_file(const char *from, const char *UNUSED(to))
{
  if (unlink(from)) {
    perror("unlink");

    return RecursiveOp_Callback_Error;
  }

  return RecursiveOp_Callback_OK;
}

#  ifdef __APPLE__
static int delete_soft(const char *file, const char **error_message)
{
  int ret = -1;

  Class NSAutoreleasePoolClass = objc_getClass("NSAutoreleasePool");
  SEL allocSel = sel_registerName("alloc");
  SEL initSel = sel_registerName("init");
  id poolAlloc = ((id(*)(Class, SEL))objc_msgSend)(NSAutoreleasePoolClass, allocSel);
  id pool = ((id(*)(id, SEL))objc_msgSend)(poolAlloc, initSel);

  Class NSStringClass = objc_getClass("NSString");
  SEL stringWithUTF8StringSel = sel_registerName("stringWithUTF8String:");
  id pathString = ((
      id(*)(Class, SEL, const char *))objc_msgSend)(NSStringClass, stringWithUTF8StringSel, file);

  Class NSFileManagerClass = objc_getClass("NSFileManager");
  SEL defaultManagerSel = sel_registerName("defaultManager");
  id fileManager = ((id(*)(Class, SEL))objc_msgSend)(NSFileManagerClass, defaultManagerSel);

  Class NSURLClass = objc_getClass("NSURL");
  SEL fileURLWithPathSel = sel_registerName("fileURLWithPath:");
  id nsurl = ((id(*)(Class, SEL, id))objc_msgSend)(NSURLClass, fileURLWithPathSel, pathString);

  SEL trashItemAtURLSel = sel_registerName("trashItemAtURL:resultingItemURL:error:");
  BOOL deleteSuccessful = ((
      BOOL(*)(id, SEL, id, id, id))objc_msgSend)(fileManager, trashItemAtURLSel, nsurl, nil, nil);

  if (deleteSuccessful) {
    ret = 0;
  }
  else {
    *error_message = "The Cocoa API call to delete file or directory failed";
  }

  SEL drainSel = sel_registerName("drain");
  ((void (*)(id, SEL))objc_msgSend)(pool, drainSel);

  return ret;
}
#  else
static int delete_soft(const char *file, const char **error_message)
{
  const char *args[5];
  const char *process_failed;

  char *xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP");
  char *xdg_session_desktop = getenv("XDG_SESSION_DESKTOP");

  if ((xdg_current_desktop != NULL && STREQ(xdg_current_desktop, "KDE")) ||
      (xdg_session_desktop != NULL && STREQ(xdg_session_desktop, "KDE"))) {
    args[0] = "kioclient5";
    args[1] = "move";
    args[2] = file;
    args[3] = "trash:/";
    args[4] = NULL;
    process_failed = "kioclient5 reported failure";
  }
  else {
    args[0] = "gio";
    args[1] = "trash";
    args[2] = file;
    args[3] = NULL;
    process_failed = "gio reported failure";
  }

  int pid = fork();

  if (pid != 0) {
    /* Parent process */
    int wstatus = 0;

    waitpid(pid, &wstatus, 0);

    if (!WIFEXITED(wstatus)) {
      *error_message =
          "Blender may not support moving files or directories to trash on your system.";
      return -1;
    }
    if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus)) {
      *error_message = process_failed;
      return -1;
    }

    return 0;
  }

  execvp(args[0], (char **)args);

  *error_message = "Forking process failed.";
  return -1; /* This should only be reached if execvp fails and stack isn't replaced. */
}
#  endif

FILE *BLI_fopen(const char *filename, const char *mode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return fopen(filename, mode);
}

void *BLI_gzopen(const char *filename, const char *mode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return gzopen(filename, mode);
}

int BLI_open(const char *filename, int oflag, int pmode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return open(filename, oflag, pmode);
}

int BLI_access(const char *filename, int mode)
{
  BLI_assert(!BLI_path_is_rel(filename));

  return access(filename, mode);
}

int BLI_delete(const char *file, bool dir, bool recursive)
{
  BLI_assert(!BLI_path_is_rel(file));

  if (recursive) {
    return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
  }
  if (dir) {
    return rmdir(file);
  }
  return remove(file);
}

int BLI_delete_soft(const char *file, const char **error_message)
{
  BLI_assert(!BLI_path_is_rel(file));

  return delete_soft(file, error_message);
}

/**
 * Do the two paths denote the same file-system object?
 */
static bool check_the_same(const char *path_a, const char *path_b)
{
  struct stat st_a, st_b;

  if (lstat(path_a, &st_a)) {
    return false;
  }

  if (lstat(path_b, &st_b)) {
    return false;
  }

  return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
}

/**
 * Sets the mode and ownership of file to the values from st.
 */
static int set_permissions(const char *file, const struct stat *st)
{
  if (chown(file, st->st_uid, st->st_gid)) {
    perror("chown");
    return -1;
  }

  if (chmod(file, st->st_mode)) {
    perror("chmod");
    return -1;
  }

  return 0;
}

/* pre-recursive callback for copying operation
 * creates a destination directory where all source content fill be copied to */
static int copy_callback_pre(const char *from, const char *to)
{
  struct stat st;

  if (check_the_same(from, to)) {
    fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
    return RecursiveOp_Callback_Error;
  }

  if (lstat(from, &st)) {
    perror("stat");
    return RecursiveOp_Callback_Error;
  }

  /* create a directory */
  if (mkdir(to, st.st_mode)) {
    perror("mkdir");
    return RecursiveOp_Callback_Error;
  }

  /* set proper owner and group on new directory */
  if (chown(to, st.st_uid, st.st_gid)) {
    perror("chown");
    return RecursiveOp_Callback_Error;
  }

  return RecursiveOp_Callback_OK;
}

static int copy_single_file(const char *from, const char *to)
{
  FILE *from_stream, *to_stream;
  struct stat st;
  char buf[4096];
  size_t len;

  if (check_the_same(from, to)) {
    fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
    return RecursiveOp_Callback_Error;
  }

  if (lstat(from, &st)) {
    perror("lstat");
    return RecursiveOp_Callback_Error;
  }

  if (S_ISLNK(st.st_mode)) {
    /* symbolic links should be copied in special way */
    char *link_buffer;
    int need_free;
    ssize_t link_len;

    /* get large enough buffer to read link content */
    if ((st.st_size + 1) < sizeof(buf)) {
      link_buffer = buf;
      need_free = 0;
    }
    else {
      link_buffer = MEM_callocN(st.st_size + 2, "copy_single_file link_buffer");
      need_free = 1;
    }

    link_len = readlink(from, link_buffer, st.st_size + 1);
    if (link_len < 0) {
      perror("readlink");

      if (need_free) {
        MEM_freeN(link_buffer);
      }

      return RecursiveOp_Callback_Error;
    }

    link_buffer[link_len] = '\0';

    if (symlink(link_buffer, to)) {
      perror("symlink");
      if (need_free) {
        MEM_freeN(link_buffer);
      }
      return RecursiveOp_Callback_Error;
    }

    if (need_free) {
      MEM_freeN(link_buffer);
    }

    return RecursiveOp_Callback_OK;
  }
  if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
    /* copy special type of file */
    if (mknod(to, st.st_mode, st.st_rdev)) {
      perror("mknod");
      return RecursiveOp_Callback_Error;
    }

    if (set_permissions(to, &st)) {
      return RecursiveOp_Callback_Error;
    }

    return RecursiveOp_Callback_OK;
  }
  if (!S_ISREG(st.st_mode)) {
    fprintf(stderr, "Copying of this kind of files isn't supported yet\n");
    return RecursiveOp_Callback_Error;
  }

  from_stream = fopen(from, "rb");
  if (!from_stream) {
    perror("fopen");
    return RecursiveOp_Callback_Error;
  }

  to_stream = fopen(to, "wb");
  if (!to_stream) {
    perror("fopen");
    fclose(from_stream);
    return RecursiveOp_Callback_Error;
  }

  while ((len = fread(buf, 1, sizeof(buf), from_stream)) > 0) {
    fwrite(buf, 1, len, to_stream);
  }

  fclose(to_stream);
  fclose(from_stream);

  if (set_permissions(to, &st)) {
    return RecursiveOp_Callback_Error;
  }

  return RecursiveOp_Callback_OK;
}

/* Not used anywhere! */
#  if 0
static int move_callback_pre(const char *from, const char *to)
{
  int ret = rename(from, to);

  if (ret) {
    return copy_callback_pre(from, to);
  }

  return RecursiveOp_Callback_StopRecurs;
}

static int move_single_file(const char *from, const char *to)
{
  int ret = rename(from, to);

  if (ret) {
    return copy_single_file(from, to);
  }

  return RecursiveOp_Callback_OK;
}

/* if *file represents a directory, moves all its contents into *to, else renames
 * file itself to *to. */
int BLI_move(const char *file, const char *to)
{
  int ret = recursive_operation(file, to, move_callback_pre, move_single_file, NULL);

  if (ret && ret != -1) {
    return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
  }

  return ret;
}
#  endif

static const char *check_destination(const char *file, const char *to)
{
  struct stat st;

  if (!stat(to, &st)) {
    if (S_ISDIR(st.st_mode)) {
      char *str, *path;
      const char *filename;
      size_t len = 0;

      str = strip_last_slash(file);
      filename = BLI_path_slash_rfind(str);

      if (!filename) {
        MEM_freeN(str);
        return (char *)to;
      }

      /* skip slash */
      filename += 1;

      len = strlen(to) + strlen(filename) + 1;
      path = MEM_callocN(len + 1, "check_destination path");
      BLI_join_dirfile(path, len + 1, to, filename);

      MEM_freeN(str);

      return path;
    }
  }

  return to;
}

int BLI_copy(const char *file, const char *to)
{
  const char *actual_to = check_destination(file, to);
  int ret;

  ret = recursive_operation(file, actual_to, copy_callback_pre, copy_single_file, NULL);

  if (actual_to != to) {
    MEM_freeN((void *)actual_to);
  }

  return ret;
}

#  if 0
int BLI_create_symlink(const char *file, const char *to)
{
  return symlink(to, file);
}
#  endif

bool BLI_dir_create_recursive(const char *dirname)
{
  char *lslash;
  size_t size;
#  ifdef MAXPATHLEN
  char static_buf[MAXPATHLEN];
#  endif
  char *tmp;
  bool ret = true;

  if (BLI_is_dir(dirname)) {
    return true;
  }
  if (BLI_exists(dirname)) {
    return false;
  }

#  ifdef MAXPATHLEN
  size = MAXPATHLEN;
  tmp = static_buf;
#  else
  size = strlen(dirname) + 1;
  tmp = MEM_callocN(size, __func__);
#  endif

  BLI_strncpy(tmp, dirname, size);

  /* Avoids one useless recursion in case of '/foo/bar/' path... */
  BLI_path_slash_rstrip(tmp);

  lslash = (char *)BLI_path_slash_rfind(tmp);
  if (lslash) {
    /* Split about the last slash and recurse */
    *lslash = 0;
    if (!BLI_dir_create_recursive(tmp)) {
      ret = false;
    }
  }

#  ifndef MAXPATHLEN
  MEM_freeN(tmp);
#  endif

  if (ret) {
    ret = (mkdir(dirname, 0777) == 0);
  }
  return ret;
}

int BLI_rename(const char *from, const char *to)
{
  if (!BLI_exists(from)) {
    return 1;
  }

  if (BLI_exists(to)) {
    if (BLI_delete(to, false, false)) {
      return 1;
    }
  }

  return rename(from, to);
}

#endif