-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrename.go
64 lines (55 loc) · 2.04 KB
/
rename.go
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
// Copyright 2017 The go-darwin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin
package apfs
/*
#include <fcntl.h> // for AT_FDCWD
#include <stdio.h>
*/
import "C"
import (
"fmt"
"path/filepath"
"syscall"
)
// RENAME_FALG provides the rename flag.
type RENAME_FALG uint
var (
// RENAME_SWAP on file systems that support it (see getattrlist(2) VOL_CAP_INT_RENAME_SWAP), it will cause the source and target to be atomically swapped.
//
// Source and target need not be of the same type, i.e. it is possible to swap a file with a directory.
//
// EINVAL is returned in case of bitwise-inclusive OR with RENAME_EXCL.
RENAME_SWAP RENAME_FALG = 0x00000002
// RENAME_EXCL on file systems that support it (see getattrlist(2) VOL_CAP_INT_RENAME_EXCL), it will cause EEXIST to be returned if the destination already exists.
//
// EINVAL is returned in case of bitwise-inclusive OR with RENAME_SWAP.
RENAME_EXCL RENAME_FALG = 0x00000004
)
// RenamexNp system calls are similar to rename() and renameat() counterparts except that they take a flags argument.
// int
// renamex_np(const char *from, const char *to, unsigned int flags);
func RenamexNp(src, dst string, flag RENAME_FALG) error {
if err := C.renamex_np(C.CString(src), C.CString(dst), C.unsigned(flag)); err != 0 {
return fmt.Errorf("error: C.renamex_np: %v", (syscall.Errno(err)))
}
return nil
}
// RenameatxNp system calls are similar to rename() and renameat() counterparts except that they take a flags argument.
// int
// renameatx_np(int fromfd, const char *from, int tofd, const char *to, unsigned int flags);
func RenameatxNp(src, dst string, flag RENAME_FALG) error {
var srcFd C.int
if !filepath.IsAbs(src) {
srcFd = C.AT_FDCWD
}
var dstFd C.int
if !filepath.IsAbs(dst) {
dstFd = C.AT_FDCWD
}
if err := C.renameatx_np(srcFd, C.CString(src), dstFd, C.CString(dst), C.unsigned(flag)); err != 0 {
return fmt.Errorf("error: C.renameatx_np: %v", syscall.Errno(err))
}
return nil
}