-
Notifications
You must be signed in to change notification settings - Fork 2
/
add.go
117 lines (88 loc) · 2.22 KB
/
add.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
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
package xar
// #include <stdlib.h>
// #include <xar/xar.h>
import "C"
import (
"unsafe"
"golang.org/x/sys/unix"
)
func (a *Archive) Add(path string) (*File, error) {
cStr := C.CString(path)
defer C.free(unsafe.Pointer(cStr))
handle := C.xar_add(a.archive, cStr)
if handle == nil {
return nil, ErrNil
}
return &File{
archive: a,
file: handle,
}, nil
}
func (a *Archive) AddFromBuffer(parent *File, name string, buffer []byte) (*File, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
var parentHandle C.xar_file_t
if parent != nil {
parentHandle = parent.file
}
handle := C.xar_add_frombuffer(a.archive, parentHandle, cName, (*C.char)(unsafe.Pointer(&buffer[0])), C.size_t(len(buffer)))
if handle == nil {
return nil, ErrNil
}
return &File{
archive: a,
file: handle,
}, nil
}
func (a *Archive) AddFolder(parent *File, name string, stat *unix.Stat_t) (*File, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
var parentHandle C.xar_file_t
if parent != nil {
parentHandle = parent.file
}
// translate our stat into c stat
cStat := translateStatStruct(stat)
handle := C.xar_add_folder(a.archive, parentHandle, cName, cStat)
if handle == nil {
return nil, ErrNil
}
return &File{
archive: a,
file: handle,
}, nil
}
func (a *Archive) AddFromPath(parent *File, name string, realpath string) (*File, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
cRealPath := C.CString(realpath)
defer C.free(unsafe.Pointer(cRealPath))
var parentHandle C.xar_file_t
if parent != nil {
parentHandle = parent.file
}
handle := C.xar_add_frompath(a.archive, parentHandle, cName, cRealPath)
if handle == nil {
return nil, ErrNil
}
return &File{
archive: a,
file: handle,
}, nil
}
func (a *Archive) AddFromArchive(parent *File, name string, source *File) (*File, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
var parentHandle C.xar_file_t
if parent != nil {
parentHandle = parent.file
}
handle := C.xar_add_from_archive(a.archive, parentHandle, cName, source.archive.archive, source.file)
if handle == nil {
return nil, ErrNil
}
return &File{
archive: a,
file: handle,
}, nil
}