-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava_mirror_file_system.java
More file actions
231 lines (184 loc) · 5.12 KB
/
Copy pathjava_mirror_file_system.java
File metadata and controls
231 lines (184 loc) · 5.12 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
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
221
222
223
224
225
226
/*
* Java Mirror File System
* Copyright (C) 20014 Syed Rahman Mashwani <syed.rahman[REMOVE-IT]@upesh.edu.pk>
*
* This software is released as-is under the GPL license.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import net.fusejna.DirectoryFiller;
import net.fusejna.ErrorCodes;
import net.fusejna.FlockCommand;
import net.fusejna.FuseException;
import net.fusejna.StructFlock.FlockWrapper;
import net.fusejna.StructFuseFileInfo.FileInfoWrapper;
import net.fusejna.StructStat.StatWrapper;
import net.fusejna.types.TypeMode.ModeWrapper;
import net.fusejna.types.TypeMode.NodeType;
import net.fusejna.util.FuseFilesystemAdapterFull;
public class java_mirror_file_system extends FuseFilesystemAdapterFull
{
static File f_mountPoint = new File ("./target/mirrorFS"); // set mount point (path to a blank folder)
private final String mirroredFolder = "./target/mirrorFolder"; //set the path of a folder which you want to be mirrored.
private final static String mountPoint = f_mountPoint.getAbsolutePath();
public static void main(String args[]) throws FuseException
{
new java_mirror_file_system().log(true).mount(mountPoint);
}
@Override
public int getattr(final String path, final StatWrapper stat)
{
File f = new File(mirroredFolder+path);
//if current path is of file
if (f.isFile())
{
stat.setMode(NodeType.FILE,true,true,true,true,true,true,true,true,true);
stat.size(f.length());
stat.atime(f.lastModified()/ 1000L);
stat.mtime(0);
stat.nlink(1);
stat.uid(0);
stat.gid(0);
stat.blocks((int) ((f.length() + 511L) / 512L));
return 0;
}
//if current file is of Directory
else if(f.isDirectory())
{
stat.setMode(NodeType.DIRECTORY);
return 0;
}
return -ErrorCodes.ENOENT();
}
@Override
public int read(final String path, final ByteBuffer buffer, final long size, final long offset, final FileInfoWrapper info)
{
Path p = Paths.get(mirroredFolder+path);
try {
byte[] data = Files.readAllBytes(p);
if ((offset + size) > data.length)
{
buffer.put(data, (int) offset, data.length-(int) offset);
return (int) size;
}
else
{
buffer.put(data, (int) offset, (int) size);
return (int) size;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int write(final String path, final ByteBuffer buf, final long bufSize, final long writeOffset,
final FileInfoWrapper wrapper)
{
byte[] b = new byte[(int) bufSize];
buf.get(b);
try {
FileOutputStream output = new FileOutputStream(mirroredFolder+path, true);
output.write(b);
output.close();
return (int) bufSize;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
@Override
public int opendir(final String path, final FileInfoWrapper info)
{
return 0;
}
@Override
public int readdir(final String path, final DirectoryFiller filler)
{
//adding fillers to directory
File f = new File(mirroredFolder+path);
if(f.isDirectory())
{
File[] fList = f.listFiles();
for (File file : fList)
{
filler.add(file.toString());
}
}
return 0;
}
@Override
public int create(final String path, final ModeWrapper mode, final FileInfoWrapper info)
{
File f = new File(mirroredFolder+path);
try {
f.createNewFile();
mode.setMode(NodeType.FILE, true, true, true);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int mkdir(final String path, final ModeWrapper mode)
{
File f = new File(mirroredFolder+path);
if (f.exists())
{
f = null;
return -ErrorCodes.EEXIST();
}
else
{
f.mkdir();
}
return 0;
}
@Override
public int open(final String path, final FileInfoWrapper info)
{
return 0;
}
@Override
public int rename(final String path, final String newName)
{
File f = new File(mirroredFolder+path);
File f1 = new File(mirroredFolder+newName);
boolean bool = false;
try{
// renaming file or folder
bool = f.renameTo(f1);
System.out.print("File/folder renamed? "+bool+" Rename from "+path+" To "+newName);
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
return 0;
}
@Override
public int readlink(final String path, final ByteBuffer buffer, final long size)
{
/*
Path link = Paths.get(mountPoint+path);
if (Files.isSymbolicLink(link))
{
System.out.println("map get. symlink path= "+path+" Target file Path=" +mapSymLink.get(path));
byte[] b = mapSymLink.get(path).getBytes(Charset.forName("UTF-8"));
buffer.put(b);
}
*/
return 0;
}
@Override
public int lock(final String path, final FileInfoWrapper info, final FlockCommand command, final FlockWrapper flock)
{
return 0;
//return -ErrorCodes.ENOSYS();
}
}