forked from gluster/glusterfs-java-filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
179 lines (153 loc) · 7.7 KB
/
Example.java
File metadata and controls
179 lines (153 loc) · 7.7 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
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
package com.peircean.glusterfs.example;
import com.peircean.glusterfs.GlusterFileSystem;
import com.peircean.glusterfs.GlusterPath;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.spi.FileSystemProvider;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author <a href="http://about.me/louiszuckerman">Louis Zuckerman</a>
*/
public class Example {
public static FileSystemProvider getProvider(String scheme) {
for (FileSystemProvider fsp : FileSystemProvider.installedProviders()) {
if (fsp.getScheme().equals(scheme)) {
return fsp;
}
}
throw new IllegalArgumentException("No provider found for scheme: " + scheme);
}
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
Properties properties = new Properties();
properties.load(Example.class.getClassLoader().getResourceAsStream("example.properties"));
String vagrantBox = properties.getProperty("glusterfs.server");
String volname = properties.getProperty("glusterfs.volume");
System.out.println(getProvider("gluster").toString());
String mountUri = "gluster://" + vagrantBox + ":" + volname + "/";
String testUri = "gluster://" + vagrantBox + ":" + volname + "/baz";
Path mountPath = Paths.get(new URI(mountUri));
FileSystem fileSystem = FileSystems.newFileSystem(new URI(mountUri), null);
FileStore store = fileSystem.getFileStores().iterator().next();
System.out.println("TOTAL SPACE: " + store.getTotalSpace());
System.out.println("USABLE SPACE: " + store.getUsableSpace());
System.out.println("UNALLOCATED SPACE: " + store.getUnallocatedSpace());
System.out.println(fileSystem.toString());
String hidden = "/foo/.bar";
boolean isHidden = fileSystem.provider().isHidden(new GlusterPath(((GlusterFileSystem) fileSystem), hidden));
System.out.println("Is " + hidden + " hidden? " + isHidden);
hidden = "/foo/bar";
isHidden = fileSystem.provider().isHidden(new GlusterPath(((GlusterFileSystem) fileSystem), hidden));
System.out.println("Is " + hidden + " hidden? " + isHidden);
Set<PosixFilePermission> posixFilePermissions = PosixFilePermissions.fromString("rw-rw-rw-");
FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(posixFilePermissions);
Path glusterPath = Paths.get(new URI(testUri));
System.out.println(glusterPath.getClass());
System.out.println(glusterPath);
System.out.println(glusterPath.getFileSystem().toString());
try {
Files.createFile(glusterPath, attrs);
System.out.println("File created");
} catch (IOException e) {
System.out.println("File exists, created at " + Files.getLastModifiedTime(glusterPath));
}
String hello = "Hello, ";
Files.write(glusterPath, hello.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
String world = "world!";
Files.write(glusterPath, world.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
long bazSize = Files.size(glusterPath);
System.out.println("SIZE: " + bazSize);
byte[] readBytes = Files.readAllBytes(glusterPath);
System.out.println(hello + world + " == " + new String(readBytes));
System.out.println("Last modified: " + Files.getLastModifiedTime(glusterPath) + " (should be now)");
fileSystem.provider().checkAccess(glusterPath, AccessMode.READ, AccessMode.WRITE);
System.out.println("Can read & write file");
try {
fileSystem.provider().checkAccess(glusterPath, AccessMode.EXECUTE);
System.out.println("Uh oh, file is executable, that's bad.");
} catch (AccessDeniedException e) {
System.out.println("Can't execute file, that's good.");
}
Path symlinkPath = Paths.get(new URI(mountUri + "symlink"));
Path symlinkTarget = Paths.get(new URI(mountUri + "symlinktarget"));
Files.createSymbolicLink(symlinkPath, symlinkTarget);
System.out.println("SYMLINK: " + symlinkPath.toString() + " => " + Files.readSymbolicLink(symlinkPath));
Path copyPath = glusterPath.resolveSibling("copy");
// Files.createFile(copyPath, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-rw-rw-")));
Files.copy(glusterPath, copyPath, StandardCopyOption.REPLACE_EXISTING);
long copySize = Files.size(copyPath);
System.out.println("Source and copy are " + (bazSize == copySize ? "" : "NOT") + " equal.");
try {
Files.newDirectoryStream(mountPath.resolve("bazzzzz"));
} catch (NotDirectoryException e) {
System.out.println("Can't list directory of a file, good.");
}
DirectoryStream.Filter<? super Path> filter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return entry.endsWith("1");
}
};
DirectoryStream<Path> stream = Files.newDirectoryStream(mountPath, filter);
System.out.println("Mount contents:");
for (Path p : stream) {
System.out.println(p.toString());
}
filter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return entry.endsWith("a");
}
};
stream = Files.newDirectoryStream(mountPath, filter);
System.out.println("Mount contents:");
for (Path p : stream) {
System.out.println(p.toString());
}
stream = Files.newDirectoryStream(mountPath);
System.out.println("Mount contents:");
PathMatcher matcher = fileSystem.getPathMatcher("glob:**/*z");
for (Path p : stream) {
System.out.println(p.toString());
if (matcher.matches(p)) {
System.out.println(" **** MATCH ****");
}
}
stream = Files.newDirectoryStream(mountPath, "*z");
System.out.println("Mount contents:");
for (Path p : stream) {
System.out.println(p.toString());
}
WatchService watchService = fileSystem.newWatchService();
Path one = Paths.get(new URI("gluster://" + vagrantBox + ":" + volname + "/one"));
System.out.println("STARTSWITH empty: " + one.startsWith("/"));
one.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
for (int i = 0; i < 10; i++) {
WatchKey take = watchService.poll(1, TimeUnit.SECONDS);
if (null == take) {
continue;
}
List<WatchEvent<?>> events = take.pollEvents();
for (WatchEvent e : events) {
Path path = (Path) e.context();
Path absolutePath = one.resolve(path).toAbsolutePath();
boolean exists = Files.exists(absolutePath);
System.out.println("EXISTS? " + exists);
if (exists) {
System.out.println("SIZE: " + Files.size(absolutePath));
}
System.out.println(absolutePath);
System.out.println(e.toString());
}
take.reset();
}
fileSystem.close();
}
}