-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystemObjects.go
184 lines (149 loc) · 3.73 KB
/
FileSystemObjects.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
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
package object_model
import (
"fmt"
"github.com/OntoLedgy/ol_common_services/code/services/identification_services/uuid_service"
"github.com/OntoLedgy/storage_interop_services/code/services/disk/file_system_service/wrappers"
"io"
"log"
"os"
"path/filepath"
)
type FileSystemObjects struct {
Uuid *uuid_service.UUIDs
Path *wrappers.AbsolutePathWrappers
}
//def __init__(
//self,
func (fileSystemObject *FileSystemObjects) Initialise(
//absolute_path_string: str):
absolutePathString string) {
var err error
fileSystemObject.Uuid = &uuid_service.UUIDs{}
//self.uuid = \
fileSystemObject.Uuid, err =
//create_new_uuid()
uuid_service.GetUUID(
1, //TODO - add enumeration
"") //TODO - wrap this inside the factory
if err != nil {
panic(err)
}
//self.__path = \
fileSystemObject.Path =
&wrappers.AbsolutePathWrappers{}
//AbsolutePathWrappers(
//absolute_path_string)
fileSystemObject.Path.Initialise(
absolutePathString)
}
//@property
//def base_name(
//self) \
//-> str:
func (fileSystemObject *FileSystemObjects) BaseName() string {
//return \
//self.__path.base_name
return fileSystemObject.Path.BaseName()
}
//@property
//def absolute_path_string(
//self) \
//-> str:
func (fileSystemObject *FileSystemObjects) AbsolutePathString() string {
//return \
//self.__path.absolute_path_string
return fileSystemObject.Path.AbsolutePathString()
}
//@property
//def absolute_level(
//self) \
//-> int:
func (fileSystemObject *FileSystemObjects) AbsoluteLevel() int {
//return \
//self.__path.absolute_level
return fileSystemObject.Path.AbsoluteLevel()
}
//@property
//def parent_absolute_path_string(
//self) \
//-> str:
func (fileSystemObject *FileSystemObjects) ParentAbsolutePathString() string {
//return \
//str(self.__path.parent)
return fileSystemObject.Path.Parent().PathString()
}
//def extend_path(
//self,
//path_extension: str) \
//-> str:
func (fileSystemObject *FileSystemObjects) ExtendPath(pathExtension string) string {
//return \
//self.__path.extend_path(
//path_extension)
return fileSystemObject.Path.ExtendPath(pathExtension)
}
//def exists(
//self) \
//-> bool:
func (fileSystemObject *FileSystemObjects) Exists() bool {
//return \
//self.__path.exists()
return fileSystemObject.Path.Exists()
}
func (fileSystemObject *FileSystemObjects) CreateIfNonExistent() (bool, error) {
fileSystemObjectExists := fileSystemObject.Exists()
if fileSystemObjectExists {
return true, nil
} else {
folderCreationError := os.Mkdir(
fileSystemObject.AbsolutePathString(),
0755)
if folderCreationError != nil {
panic(folderCreationError)
}
return true, nil
}
}
//def list_of_components(self):
func (fileSystemObject *FileSystemObjects) ListOfComponents() []string {
//return \
//self.__path.list_of_components()
return fileSystemObject.Path.ListOfComponents()
}
//def item_count(
//self) \
//-> int:
func (fileSystemObject *FileSystemObjects) ItemCount() int {
//return \
//self.__path.item_count()
return fileSystemObject.Path.ItemCount()
}
func (fileSystemObject *FileSystemObjects) Copy(targetPath string) (int64, error) {
sourceFileStat, err := os.Stat(fileSystemObject.AbsolutePathString())
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf(
"%s is not a regular file",
fileSystemObject.AbsolutePathString())
}
source, err :=
os.Open(
fileSystemObject.AbsolutePathString())
if err != nil {
return 0, err
}
defer source.Close()
directorCreationError := os.MkdirAll(filepath.Dir(targetPath), os.ModePerm)
if directorCreationError != nil {
log.Println(err)
}
destination, err := os.Create(targetPath)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}