File: filesys.cpp

package info (click to toggle)
boinc 7.14.2+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (1001 lines) | stat: -rw-r--r-- 25,088 bytes parent folder | download | duplicates (5)
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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.

#if   defined(_WIN32) && !defined(__STDWX_H__)
#include "boinc_win.h"
#elif defined(_WIN32) && defined(__STDWX_H__)
#include "stdwx.h"
#define MAXPATHLEN 4096
#endif

#if defined(__MINGW32__)
#include <fcntl.h>
#endif

#if  defined(_MSC_VER) || defined(__MINGW32__)
#define getcwd    _getcwd
#define snprintf  _snprintf
#endif

#if !defined(_WIN32) || defined(__CYGWIN32__)
#include "config.h"
#ifdef _USING_FCGI_
#include "boinc_fcgi.h"
#else
#include <cstdio>
#endif
#include <fcntl.h>
#include <cerrno>
#include <sys/stat.h>
#include <sys/file.h>
#include <cstring>
#include <cstdlib>
#include <sys/time.h>
#include <unistd.h>
#include <dirent.h>

#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if HAVE_SYS_MOUNT_H
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/mount.h>
#endif

#if HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#define STATFS statvfs
#elif HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#define STATFS statfs
#else
#define STATFS statfs
#endif
#endif

#include "util.h"
#include "str_util.h"
#include "str_replace.h"
#include "error_numbers.h"

#include "filesys.h"

#ifdef __APPLE__
#include "mac_spawn.h"
#endif

#ifdef _WIN32
typedef BOOL (CALLBACK* FreeFn)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
#endif

using std::string;

char boinc_failed_file[MAXPATHLEN];

// routines for enumerating the entries in a directory

int is_file(const char* path) {
#ifdef _WIN32
    DWORD dwAttrib = GetFileAttributesA(path);
    return (dwAttrib != INVALID_FILE_ATTRIBUTES
        && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)
    );
#else
    struct stat sbuf;
    int retval = lstat(path, &sbuf);
    return (!retval && (((sbuf.st_mode) & S_IFMT) == S_IFREG));
#endif
}

int is_dir(const char* path) {
#ifdef _WIN32
    DWORD dwAttrib = GetFileAttributesA(path);
    return (dwAttrib != INVALID_FILE_ATTRIBUTES
        && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)
    );
#else
    struct stat sbuf;
    int retval = lstat(path, &sbuf);
    return (!retval && (((sbuf.st_mode) & S_IFMT) == S_IFDIR));
#endif
}

#ifndef _WIN32

int is_file_follow_symlinks(const char* path) {
    struct stat sbuf;
    int retval = stat(path, &sbuf);
    return (!retval && (((sbuf.st_mode) & S_IFMT) == S_IFREG));
}

int is_dir_follow_symlinks(const char* path) {
    struct stat sbuf;
    int retval = stat(path, &sbuf);
    return (!retval && (((sbuf.st_mode) & S_IFMT) == S_IFDIR));
}

int is_symlink(const char* path) {
    struct stat sbuf;
    int retval = lstat(path, &sbuf);
    return (!retval && S_ISLNK(sbuf.st_mode));
}
#endif

// Open a directory
//
DIRREF dir_open(const char* p) {
    DIRREF dirp;
#ifdef _WIN32
    if (!is_dir(p)) return NULL;
    dirp = (DIR_DESC*) calloc(sizeof(DIR_DESC), 1);
    if (!dirp) {
        fprintf(stderr, "calloc() failed in dir_open()\n");
        return NULL;
    }
    dirp->first = true;
    safe_strcpy(dirp->path, p);
    safe_strcat(dirp->path, "\\*");
    dirp->handle = INVALID_HANDLE_VALUE;
#else
    dirp = opendir(p);
    if (!dirp) {
        char b[MAXPATHLEN+1];
        boinc_getcwd(b);
        fprintf(stderr,"dir_open: Could not open directory '%s' from '%s'.\n",p,b);
        return NULL;
    }
#endif
    return dirp;
}

// Scan through a directory and return:
// 0 if an entry was found (the entry is returned in p)
// ERR_NOT_FOUND if we reached the end of the directory
// ERR_READDIR if some other error occurred
//
int dir_scan(char* p, DIRREF dirp, int p_len) {
#ifdef _WIN32
    WIN32_FIND_DATAA data;
    while (1) {
        if (dirp->first) {
            dirp->first = false;
            dirp->handle = FindFirstFileA(dirp->path, &data);
            if (dirp->handle == INVALID_HANDLE_VALUE) {
                return ERR_READDIR;
            } else {
                // does Windows have "." and ".."?  well, just in case.
                //
                if (!strcmp(data.cFileName, ".")) continue;
                if (!strcmp(data.cFileName, "..")) continue;
                if (p) strlcpy(p, data.cFileName, p_len);
                return 0;
            }
        } else {
            if (FindNextFileA(dirp->handle, &data)) {
                if (!strcmp(data.cFileName, ".")) continue;
                if (!strcmp(data.cFileName, "..")) continue;
                if (p) strlcpy(p, data.cFileName, p_len);
                return 0;
            } else {
                DWORD ret = GetLastError();
                FindClose(dirp->handle);
                dirp->handle = INVALID_HANDLE_VALUE;
                if (ret == ERROR_NO_MORE_FILES) {
                    return ERR_NOT_FOUND;
                }
                return ERR_READDIR;
            }
        }
    }
#else
    while (1) {
        errno = 0;
        dirent* dp = readdir(dirp);
        if (dp) {
            if (!strcmp(dp->d_name, ".")) continue;
            if (!strcmp(dp->d_name, "..")) continue;
            if (p) strlcpy(p, dp->d_name, p_len);
            return 0;
        } else {
            if (errno) return ERR_READDIR;
            return ERR_NOT_FOUND;
        }
    }
#endif
}

// Close a directory
//
void dir_close(DIRREF dirp) {
#ifdef _WIN32
    if (dirp->handle != INVALID_HANDLE_VALUE) {
        FindClose(dirp->handle);
        dirp->handle = INVALID_HANDLE_VALUE;
    }
    free(dirp);
#else
    if (dirp) {
        closedir(dirp);
    }
#endif
}

bool is_dir_empty(const char *p) {
    char file[MAXPATHLEN];

    DIRREF dir = dir_open(p);
    if (!dir) return true;

    bool retval = (dir_scan(file, dir, sizeof(file)) != 0);
    dir_close(dir);
    return retval;
}

DirScanner::DirScanner(string const& path) {
#ifdef _WIN32
    first = true;
    handle = INVALID_HANDLE_VALUE;
    if (!is_dir((char*)path.c_str())) {
        return;
    }
    dir = path + "\\*";
#else
    dirp = opendir(path.c_str());
#endif
}

// Scan through a directory and return the next file name in it
//
bool DirScanner::scan(string& s) {
#ifdef _WIN32
    WIN32_FIND_DATAA data;
    while (1) {
        if (first) {
            first = false;
            handle = FindFirstFileA(dir.c_str(), &data);
            if (handle == INVALID_HANDLE_VALUE) {
                return false;
            } else {
                if (data.cFileName[0] == '.') continue;
                s = data.cFileName;
                return true;
            }
        } else {
            if (FindNextFileA(handle, &data)) {
                if (data.cFileName[0] == '.') continue;
                s = data.cFileName;
                return true;
            } else {
                FindClose(handle);
                handle = INVALID_HANDLE_VALUE;
                return false;
            }
        }
    }
#else
    if (!dirp) return false;

    while (1) {
        dirent* dp = readdir(dirp);
        if (dp) {
            if (dp->d_name[0] == '.') continue;
            s = dp->d_name;
            return true;
        } else {
            return false;
        }
    }
#endif
}

DirScanner::~DirScanner() {
#ifdef _WIN32
    if (handle != INVALID_HANDLE_VALUE) {
        FindClose(handle);
    }
#else
    if (dirp) {
        closedir(dirp);
    }
#endif
}

static int boinc_delete_file_aux(const char* path) {
#ifdef _WIN32
    if (!DeleteFileA(path)) {
        return ERR_UNLINK;
    }
#else
    int retval = unlink(path);
    if (retval) return ERR_UNLINK;
#endif
    return 0;
}

// Delete the file located at path
//
int boinc_delete_file(const char* path) {
    int retval = 0;

    if (!boinc_file_exists(path)) {
        return 0;
    }
    retval = boinc_delete_file_aux(path);
    if (retval) {
        double start = dtime();
        do {
            boinc_sleep(drand()*2);       // avoid lockstep
            retval = boinc_delete_file_aux(path);
            if (!retval) break;
        } while (dtime() < start + FILE_RETRY_INTERVAL);
    }
    if (retval) {
        safe_strcpy(boinc_failed_file, path);
        return ERR_UNLINK;
    }
    return 0;
}

// get file size
//
int file_size(const char* path, double& size) {
#if defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__MINGW32__)
    HANDLE h = CreateFileA(path, 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, 0);
    if (h == INVALID_HANDLE_VALUE) return ERR_STAT;
    LARGE_INTEGER lisize;
    if (GetFileSizeEx(h, &lisize)) {
        size = (double) lisize.QuadPart;
        CloseHandle(h);
        return 0;
    }
    return ERR_STAT;
#else
    int retval;
    struct stat sbuf;
    retval = stat(path, &sbuf);
    if (retval) return ERR_NOT_FOUND;
    size = (double)sbuf.st_size;
    return 0;
#endif
}

int boinc_truncate(const char* path, double size) {
    int retval;
#if defined(_WIN32) && !defined(__CYGWIN32__)
    // the usual Windows nightmare.
    // There's another function, SetEndOfFile(),
    // that supposedly works with files over 2GB,
    // but it uses HANDLES
    //
    int fd = _open(path, _O_RDWR, 0);
    if (fd == -1) return ERR_TRUNCATE;
    retval = _chsize(fd, (long)size);
    _close(fd);
#else
    retval = truncate(path, (off_t)size);
#endif
    if (retval) return ERR_TRUNCATE;
    return 0;
}

// remove everything from specified directory
//
int clean_out_dir(const char* dirpath) {
    char filename[MAXPATHLEN], path[MAXPATHLEN];
    int retval;
    DIRREF dirp;

    dirp = dir_open(dirpath);
    if (!dirp) return 0;    // if dir doesn't exist, it's empty
    while (1) {
        safe_strcpy(filename, "");
        retval = dir_scan(filename, dirp, sizeof(filename));
        if (retval) break;

        snprintf(path, sizeof(path), "%s/%s", dirpath,  filename);
        path[sizeof(path)-1] = 0;

        clean_out_dir(path);
        boinc_rmdir(path);
        retval = boinc_delete_file(path);
        if (retval) {
            dir_close(dirp);
            return retval;
        }
    }
    dir_close(dirp);
    return 0;
}

// return total size of files in directory and optionally its subdirectories
// Win: use special version because stat() is slow, can be avoided
// Unix: follow symbolic links
//
int dir_size(const char* dirpath, double& size, bool recurse) {
#ifdef WIN32
    char buf[_MAX_PATH];
    char path2[_MAX_PATH];
    double dsize = 0.0;
    WIN32_FIND_DATAA findData;

    size = 0.0;
    snprintf(path2, sizeof(path2), "%s/*", dirpath);
    path2[sizeof(path2)-1] = 0;

    HANDLE hFind = ::FindFirstFileA(path2, &findData);
    if (INVALID_HANDLE_VALUE == hFind) return ERR_OPENDIR;
    do {
        if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            if (!recurse) continue;
            if (!strcmp(findData.cFileName, ".")) continue;
            if (!strcmp(findData.cFileName, "..")) continue;

            dsize = 0.0;

            snprintf(buf, sizeof(buf), "%s/%s", dirpath, findData.cFileName);
            buf[sizeof(buf)-1] = 0;

            dir_size(buf, dsize, true);
            size += dsize;
        } else {
            size += findData.nFileSizeLow + ((__int64)(findData.nFileSizeHigh) << 32);
        }
    } while (FindNextFileA(hFind, &findData));

    ::FindClose(hFind);
#else
    char filename[MAXPATHLEN], subdir[MAXPATHLEN];
    int retval=0;
    DIRREF dirp;
    double x;

    size = 0.0;
    dirp = dir_open(dirpath);
    if (!dirp) return ERR_OPENDIR;
    while (1) {
        retval = dir_scan(filename, dirp, sizeof(filename));
        if (retval) break;

        snprintf(subdir, sizeof(subdir), "%s/%s", dirpath, filename);
        subdir[sizeof(subdir)-1] = 0;

        if (is_dir(subdir)) {
            if (recurse) {
                retval = dir_size(subdir, x);
                if (retval) continue;
                size += x;
            }
        } else if (is_file(subdir)) {
            retval = file_size(subdir, x);
            if (retval) continue;
            size += x;
        }
    }
    dir_close(dirp);
#endif
    return 0;
}

FILE* boinc_fopen(const char* path, const char* mode) {
    // if opening for read, and file isn't there,
    // leave now (avoid 5-second delay!!)
    //
    if (strchr(mode, 'r')) {
        if (!boinc_file_exists(path)) {
            return 0;
        }
    }
#ifndef _USING_FCGI_
    FILE *f = fopen(path, mode);
#else
    FCGI_FILE *f = FCGI::fopen(path,mode);
#endif

#ifdef _WIN32
    // on Windows: if fopen fails, try again for 5 seconds
    // (since the file might be open by FastFind, Diskeeper etc.)
    //
    if (!f) {
        double start = dtime();
        do {
            boinc_sleep(drand()*2);
            f = _fsopen(path, mode, _SH_DENYNO);
                // _SH_DENYNO makes the file sharable while open
            if (f) break;
        } while (dtime() < start + FILE_RETRY_INTERVAL);
    }
#else
    // Unix - if call was interrupted, retry a few times
    //
    if (!f) {
        for (int i=0; i<5; i++) {
            boinc_sleep(drand());
            if (errno != EINTR) break;
#ifndef _USING_FCGI_
            f = fopen(path, mode);
#else
            f = FCGI::fopen(path, mode);
#endif
            if (f) break;
        }
    }
    if (f) {
        if (-1 == fcntl(fileno(f), F_SETFD, FD_CLOEXEC)) {
            fclose(f);
            return 0;
        }
    }
#endif
    return f;
}

// returns true if anything (file, dir, whatever) exists at given path;
// name is misleading.
//
int boinc_file_exists(const char* path) {
#ifdef _WIN32
    // don't use _stat64 because it doesn't work with VS2015, XP client
    DWORD dwAttrib = GetFileAttributesA(path);
    return dwAttrib != INVALID_FILE_ATTRIBUTES;
#else
    struct stat buf;
    if (stat(path, &buf)) {
        return false;     // stat() returns zero on success
    }
    return true;
#endif
}

#if 0
// same, but doesn't traverse symlinks
//
int boinc_file_or_symlink_exists(const char* path) {
#ifdef _WIN32
    return boinc_file_exists(path);
#else
    struct stat buf;
    if (lstat(path, &buf)) {
        return false;     // stat() returns zero on success
    }
    return true;
#endif
}
#endif

// returns zero on success, nonzero if didn't touch file
//
int boinc_touch_file(const char *path) {
    if (boinc_file_exists(path)) {
        return 0;
    }
#ifndef _USING_FCGI_
    FILE *fp = fopen(path, "w");
#else
    FCGI_FILE *fp = FCGI::fopen(path, "w");
#endif
    if (fp) {
        fclose(fp);
        return 0;
    }
    return -1;
}

int boinc_copy(const char* orig, const char* newf) {
#ifdef _WIN32
    if (!CopyFileA(orig, newf, FALSE)) {     // FALSE means overwrite OK
        return GetLastError();
    }
    return 0;
#elif defined(__EMX__)
    char cmd[2*MAXPATHLEN+5];
    snprintf(cmd, sizeof(cmd), "copy \"%s\" \"%s\"", orig, newf);
    cmd[sizeof(cmd)-1] = 0;
    return system(cmd);
#else
    // POSIX requires that shells run from an application will use the 
    // real UID and GID if different from the effective UID and GID.  
    // Mac OS 10.4 did not enforce this, but OS 10.5 does.  Since 
    // system() invokes a shell, it may not properly copy the file's 
    // ownership or permissions when called from the BOINC Client 
    // under sandbox security, so we copy the file directly.
    //
    FILE *src, *dst;
    int m, n;
    int retval = 0;
    unsigned char buf[65536];
    src = boinc_fopen(orig, "r");
    if (!src) return ERR_FOPEN;
    dst = boinc_fopen(newf, "w");
    if (!dst) {
        fclose(src);
        return ERR_FOPEN;
    }
    while (1) {
        n = fread(buf, 1, sizeof(buf), src);
        if (n <= 0) {
            // could be either EOF or an error.
            // Check for error case.
            //
            if (!feof(src)) {
                retval = ERR_FREAD;
            }
            break;
        }
        m = fwrite(buf, 1, n, dst);
        if (m != n) {
            retval = ERR_FWRITE;
            break;
        }
    }
    if (fclose(src)){
       fclose(dst);
       return ERR_FCLOSE;
    }

    if (fclose(dst)){
       return ERR_FCLOSE;
    }
    return retval;
#endif
}

#ifndef _WIN32
// Copy file's ownership and permissions to the extent we are allowed
//
int boinc_copy_attributes(const char* orig, const char* newf) {
    struct stat sbuf;

    // Get source file's info
    //
    if (lstat(orig, &sbuf)) {
        return ERR_STAT;
    }
    if (chmod(newf, sbuf.st_mode)) {
        return ERR_CHMOD;
    }
    if (chown(newf, sbuf.st_uid, sbuf.st_gid)) {
        return ERR_CHOWN;
    }
    return 0;
}
#endif

static int boinc_rename_aux(const char* old, const char* newf) {
#ifdef _WIN32
    // MOVEFILE_COPY_ALLOWED is needed if destination is on another volume (move is simulated by copy&delete)
    if (MoveFileExA(old, newf, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) return 0;
    return GetLastError();
#else
    // rename() doesn't work between filesystems.
    // So if it fails, try the "mv" command, which does work
    //
    int retval = rename(old, newf);
    if (retval) {
        char buf[MAXPATHLEN+MAXPATHLEN+1+7];
        sprintf(buf, "mv \"%s\" \"%s\"", old, newf);
#ifdef __APPLE__
        // system() is deprecated in Mac OS 10.10.
        // Apple says to call posix_spawn instead.
        retval = callPosixSpawn(buf);
#else
        retval = system(buf);
#endif
    }
    if (retval) return ERR_RENAME;
    return 0;
#endif
}

int boinc_rename(const char* old, const char* newf) {
    int retval=0;

    retval = boinc_rename_aux(old, newf);
    if (retval) {
        // if the rename failed, and the file exists,
        // retry a few times
        //
        if (!boinc_file_exists(old)) return ERR_FILE_MISSING;
        double start = dtime();
        do {
            boinc_sleep(drand()*2);       // avoid lockstep
            retval = boinc_rename_aux(old, newf);
            if (!retval) break;
        } while (dtime() < start + FILE_RETRY_INTERVAL);
    }
    return retval;
}

// make a dir that's owner and group RWX
//
int boinc_mkdir(const char* path) {
    if (is_dir(path)) return 0;
#ifdef _WIN32
    if (!CreateDirectoryA(path, NULL)) {
        return GetLastError();
    }
#else
    mode_t old_mask = umask(0);
    int retval = mkdir(path, 0771);
    umask(old_mask);
    if (retval) return ERR_MKDIR;
#endif
    return 0;
}

int boinc_rmdir(const char* name) {
#ifdef _WIN32
    if (!RemoveDirectoryA(name)) {
        return ERR_RMDIR;
    }
#else
    int retval = rmdir(name);
    if (retval) return ERR_RMDIR;
#endif
    return 0;
}

#ifndef _WIN32
int boinc_chown(const char* path, gid_t gid) {
    if (gid) {
        if (chown(path, (uid_t)-1, gid)) {
            return ERR_CHOWN;
        }
    }
    return 0;
}
#endif

// if "filepath" is of the form a/b/c,
// create directories dirpath/a, dirpath/a/b etc.
//
int boinc_make_dirs(const char* dirpath, const char* filepath) {
    char buf[MAXPATHLEN], oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
    int retval;
    char *p, *q;

    if (strlen(filepath) + strlen(dirpath) > MAXPATHLEN-1) return ERR_BUFFER_OVERFLOW;
    safe_strcpy(buf, filepath);
    safe_strcpy(oldpath, dirpath);

    q = buf;
    while(*q) {
        p = strchr(q, '/');
        if (!p) break;
        *p = 0;
        snprintf(newpath, sizeof(newpath), "%s/%s", oldpath, q);
        newpath[sizeof(newpath)-1] = 0;
        retval = boinc_mkdir(newpath);
        if (retval) return retval;
        safe_strcpy(oldpath, newpath);
        q = p+1;
    }
    return 0;
}


FILE_LOCK::FILE_LOCK() {
#if defined(_WIN32) && !defined(__CYGWIN32__)
  handle = INVALID_HANDLE_VALUE;
#else
    fd = -1;
#endif
    locked = false;
}

FILE_LOCK::~FILE_LOCK() {
#if !defined(_WIN32) || defined(__CYGWIN32__)
    if (fd >= 0) close(fd);
#endif
}

int FILE_LOCK::lock(const char* filename) {
#if defined(_WIN32) && !defined(__CYGWIN32__)
    handle = CreateFileA(
        filename, GENERIC_WRITE,
        0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
    );
    if (handle == INVALID_HANDLE_VALUE) {
        return GetLastError();
    }
#else
    if (fd<0) {
        fd = open(filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
    }
    if (fd<0) {
        return ERR_OPEN;
    }

    struct flock fl;
    fl.l_type = F_WRLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    if (fcntl(fd, F_SETLK, &fl) == -1) {
        // ENOSYS means file locking is not implemented in this FS.
        // In this case just return success (i.e. don't actually do locking)
        //
        if (errno != ENOSYS) {
            return ERR_FCNTL;
        }
    }
#endif
    locked = true;
    return 0;
}

int FILE_LOCK::unlock(const char* filename) {
#if defined(_WIN32) && !defined(__CYGWIN32__)
    CloseHandle(handle);
#else
    close(fd);
    fd = -1;
#endif
    boinc_delete_file(filename);
    locked = false;
    return 0;
}

void boinc_getcwd(char* path) {
#ifdef _WIN32
    getcwd(path, MAXPATHLEN);
#else
    char* p 
#ifdef __GNUC__
      __attribute__ ((unused))
#endif
      = getcwd(path, MAXPATHLEN);
#endif
}

void relative_to_absolute(const char* relname, char* path) {
    boinc_getcwd(path);
    if (strlen(relname)) {
        strcat(path, "/");
        strcat(path, relname);
    }
}


#if defined(_WIN32)
int boinc_allocate_file(const char* path, double size) {
    int retval = 0;
    HANDLE h = CreateFileA(
        path,
        GENERIC_WRITE,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );
    if (h == INVALID_HANDLE_VALUE) return ERR_FOPEN;
    LARGE_INTEGER sz;
    sz.LowPart = fmod(size, 4294967296.);
    sz.HighPart = (LONG)(size/4294967296.);
    if (SetFilePointerEx(h, sz, NULL, FILE_BEGIN) == 0) {
        retval = ERR_FOPEN;
    }
    if (!retval && SetEndOfFile(h) == 0) {
        retval = ERR_FOPEN;
    }
    CloseHandle(h);
    return retval;
}

FILE* boinc_temp_file(
    const char* dir, const char* prefix, char* temp_path, double size
) {
    GetTempFileNameA(dir, prefix, 0, temp_path);
    boinc_allocate_file(temp_path, size);
    return boinc_fopen(temp_path, "wb");
}

#else

// Unix version: use mkstemp.  tempnam() prioritizes an env var
// in deciding where to put temp file

FILE* boinc_temp_file(const char* dir, const char* prefix, char* temp_path) {
    sprintf(temp_path, "%s/%s_XXXXXX", dir, prefix);
    int fd = mkstemp(temp_path);
    if (fd < 0) {
        return 0;
    }
    return fdopen(fd, "wb");
}

#endif

void boinc_path_to_dir(const char* path, char* dir) {
    strcpy(dir, path);
    char* p = strrchr(dir, '/');
    if (p) {
        *p = 0;
    } else {
        strcpy(dir, ".");
    }
}

// get total and free space on current filesystem (in bytes)
//
#ifdef _WIN32
int get_filesystem_info(double &total_space, double &free_space, char*) {
    char cwd[MAXPATHLEN];
    ULARGE_INTEGER TotalNumberOfFreeBytes;
    ULARGE_INTEGER TotalNumberOfBytes;
    ULARGE_INTEGER FreeBytesAvailable;
    signed __int64 uMB;

    boinc_getcwd(cwd);
    GetDiskFreeSpaceExA(
        cwd,
        &FreeBytesAvailable,
        &TotalNumberOfBytes,
        &TotalNumberOfFreeBytes
    );

    uMB = FreeBytesAvailable.QuadPart / (1024 * 1024);
    free_space = uMB * 1024.0 * 1024.0;
    uMB = TotalNumberOfBytes.QuadPart / (1024 * 1024);
    total_space = uMB * 1024.0 * 1024.0;

#else
int get_filesystem_info(double &total_space, double &free_space, char* path) {
#ifdef STATFS
    struct STATFS fs_info;

    int retval = STATFS(path, &fs_info);
    if (retval) {
#ifndef _USING_FCGI_
        perror("statvfs");
#else
        FCGI::perror("statvfs");
#endif
        return ERR_STATFS;
    }
#if HAVE_SYS_STATVFS_H
    total_space = (double)fs_info.f_frsize * (double)fs_info.f_blocks;
    free_space = (double)fs_info.f_frsize * (double)fs_info.f_bavail;
#else
    total_space = (double)fs_info.f_bsize * (double)fs_info.f_blocks;
    free_space = (double)fs_info.f_bsize * (double)fs_info.f_bavail;
#endif
#else
#error Need to specify a method to obtain free/total disk space
#endif
#endif
    return 0;
}

bool is_path_absolute(const std::string path) {
#ifdef _WIN32
    if (path.length() >= 3 && isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) {
        // c:\file
        return true;
    }
    if (path.length() >= 2 && (path[0] == '\\' || path[0] == '/') && (path[1] == '\\' || path[1] == '/')) {
        // \\server\file
        // \\?\c:\file
        return true;
    }
    return false;
#else
    return path.length() >= 1 && path[0] == '/';
#endif
}