/
FsResource.java
220 lines (182 loc) · 5.07 KB
/
FsResource.java
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
package com.jsync;
import java.io.*;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
public class FsResource implements IResource {
private class FileProperties {
int uid;
int gid;
int mode;
long atime;
long mtime;
public FileProperties() {
uid = -1;
gid = -1;
mode = -1;
atime = -1;
mtime = -1;
}
}
File file;
private FileProperties props;
public FsResource(File file) {
this.file = file;
this.props = null;
}
public FsResource(String fn) {
this(new File(fn));
}
private FileProperties getProperties() {
if (props == null) {
props = new FileProperties();
if (exists()) {
JSyncPosixTools.ShortStat stat = JSyncPosixTools.stat(file.getPath());
props.uid = stat.uid;
props.gid = stat.gid;
props.mode = stat.mode;
props.mtime = stat.mtime.longValue();
props.atime = stat.atime.longValue();
}
}
return props;
}
public Boolean isDirectory() {
return file.isDirectory();
}
public Boolean isFile() {
return file.isFile();
}
public Boolean exists() {
return file.exists();
}
public void mkdirs() {
if (isDirectory()) {
file.mkdirs();
} else {
File parent = new File(file.getParent());
parent.mkdirs();
}
}
public String getName() {
return file.getName();
}
public FsResource join(String name) {
return new FsResource(new File(file, name));
}
public IResource[] list() {
if (file.isDirectory()) {
String[] files = file.list();
if (files == null) {
return new FsResource[0];
} else {
ArrayList<FsResource> resultList;
resultList = new ArrayList<FsResource>();
for (String child: files) {
File resource = new File(file, child);
resultList.add(new FsResource(resource));
}
FsResource[] out = new FsResource[resultList.size()];
return resultList.toArray(out);
}
} else if (file.isFile()) {
return new FsResource[0];
}
return null;
}
public OutputStream getWriter() {
try {
return new FileOutputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public InputStream getReader() {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
return null;
}
}
public String toString() {
return file.toString();
}
public Boolean finishWrite() {
if (this.props != null) {
if (props.uid != -1 && props.gid != -1) {
JSyncPosixTools.chown(file.getPath(), props.uid, props.gid);
}
if (props.mode != -1) {
JSyncPosixTools.chmod(file.getPath(), props.mode);
}
if (props.atime != -1 && props.mtime != -1){
JSyncPosixTools.setTimes(
file.getPath(),
props.atime,
props.mtime);
}
}
return true;
}
public void delete() {
file.delete();
}
public long lastModified () {
return file.lastModified();
}
public long getSize() {
return file.length();
}
public void setSize(long size) {
}
public String getMimeType() {
return URLConnection.guessContentTypeFromName(file.getName());
}
public void setMimeType(String mimeType) {
}
public int getUid() {
return getProperties().uid;
}
public void setUid(int uid) {
getProperties();
props.uid = uid;
}
public int getGid() {
return getProperties().gid;
}
public void setGid(int gid) {
getProperties();
props.gid = gid;
}
public int getMode() {
return getProperties().mode;
}
public void setMode(int mode) {
getProperties();
props.mode = mode;
}
public long getModifiedTime() {
return getProperties().mtime;
}
public void setModifiedTime(long modifiedTime) {
getProperties();
props.mtime = modifiedTime;
}
public long getAccessTime() {
return getProperties().atime;
}
public void setAccessTime(long accessTime) {
getProperties();
props.atime = accessTime;
}
public boolean isSymlink() {
return (JSyncPosixTools.getLinkTarget(file.getPath()) != null);
}
public String getSymlinkTarget() {
return JSyncPosixTools.getLinkTarget(file.getPath());
}
public void createSymlink(String target) {
JSyncPosixTools.createSymlink(this.file.getPath(), target);
}
}