-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrew_darwin.go
More file actions
94 lines (80 loc) · 2.02 KB
/
screw_darwin.go
File metadata and controls
94 lines (80 loc) · 2.02 KB
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
//go:build darwin
package screw
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#include <stdlib.h>
char *GetCanonicalPath(char *cInputPath) {
NSString *inputPath = [NSString stringWithUTF8String:cInputPath];
if (!inputPath) {
return NULL;
}
NSString *canonicalPath = [[[NSURL fileURLWithPath:inputPath] fileReferenceURL] path];
if (!canonicalPath) {
return NULL;
}
const char *tempString = [canonicalPath UTF8String];
char *ret = malloc(strlen(tempString) + 1);
memcpy(ret, tempString, strlen(tempString) + 1);
return ret;
}
*/
import "C"
import (
"errors"
"fmt"
"os"
"path/filepath"
"unsafe"
)
// osRename is a test seam so darwin-only tests can deterministically exercise
// fallback and rollback branches in doRename without relying on filesystem quirks.
var osRename = os.Rename
func sneakyLog(line string) {
// nothing
}
// If `path` exists, and
func TrueBaseName(path string) string {
cInputPath := C.CString(path)
defer C.free(unsafe.Pointer(cInputPath))
cPath := C.GetCanonicalPath(cInputPath)
if uintptr(unsafe.Pointer(cPath)) == 0 {
return ""
}
defer C.free(unsafe.Pointer(cPath))
actualPath := C.GoString(cPath)
return filepath.Base(actualPath)
}
func doRename(oldpath, newpath string) error {
err := osRename(oldpath, newpath)
if err != nil {
// on macOS, renaming "foo" to "FOO" is fine,
// but renaming "foo/" to "FOO/" fails with "file exists"
if os.IsExist(err) {
originalErr := err
tmppath := fmt.Sprintf("%s__rename_pid%d", oldpath, os.Getpid())
err = osRename(oldpath, tmppath)
if err != nil {
return originalErr
}
err = osRename(tmppath, newpath)
if err != nil {
// attempt to rollback, ignore returned error,
// because what else can we do at this point?
rollbackErr := osRename(tmppath, oldpath)
if rollbackErr != nil {
return errors.Join(err, rollbackErr)
}
return err
}
// two-stage rename worked!
return nil
}
return err
}
return nil
}
func IsCaseInsensitiveFS() bool {
return true
}