root / src / Storage / FileSystem.java @ 2
History | View | Annotate | Download (5.47 KB)
1 | 2 | up20160340 | package Storage; |
---|---|---|---|
2 | |||
3 | |||
4 | import Peers.Peer; |
||
5 | |||
6 | import java.io.*; |
||
7 | import java.nio.file.Files; |
||
8 | import java.nio.file.Path; |
||
9 | import java.nio.file.Paths; |
||
10 | import java.nio.file.attribute.BasicFileAttributes; |
||
11 | import java.nio.file.attribute.UserPrincipal; |
||
12 | import java.security.MessageDigest; |
||
13 | import java.security.NoSuchAlgorithmException; |
||
14 | import java.util.HashMap; |
||
15 | import java.util.List; |
||
16 | |||
17 | public class FileSystem implements Serializable { |
||
18 | |||
19 | private final Peer peer; |
||
20 | private int maxSize; |
||
21 | private int inUse = 0; |
||
22 | |||
23 | private int peerID; |
||
24 | |||
25 | private String base; |
||
26 | |||
27 | public FileSystem(int maxSize, String directory, int peerID, Peer peer) { |
||
28 | |||
29 | this.maxSize = maxSize;
|
||
30 | this.peerID = peerID;
|
||
31 | this.base = directory + peerID;
|
||
32 | this.peer = peer;
|
||
33 | |||
34 | createDir(); |
||
35 | |||
36 | } |
||
37 | |||
38 | public int availableSpace() { |
||
39 | return maxSize - inUse;
|
||
40 | } |
||
41 | |||
42 | public void setSpace(int inc) { |
||
43 | maxSize = inc; |
||
44 | } |
||
45 | |||
46 | public int getInUse() { |
||
47 | return inUse;
|
||
48 | } |
||
49 | |||
50 | public String createFileID(String filepath, BasicFileAttributes attr, byte[] info) throws NoSuchAlgorithmException { |
||
51 | String id = ""; |
||
52 | String[] fileName = filepath.split("/"); |
||
53 | |||
54 | try {
|
||
55 | Path path = Paths.get(filepath); |
||
56 | UserPrincipal name = Files.getOwner(path); |
||
57 | id += name.toString(); |
||
58 | byte[] sh = new byte[(int) attr.size() / 3]; |
||
59 | System.arraycopy(sh, 0, info, 0, (int) attr.size() / 3); |
||
60 | id += attr.fileKey() + fileName[fileName.length - 1] + (new String(sh)); |
||
61 | } catch (FileNotFoundException e) { |
||
62 | e.printStackTrace(); |
||
63 | } catch (IOException e) { |
||
64 | e.printStackTrace(); |
||
65 | } |
||
66 | MessageDigest mDigest = MessageDigest.getInstance("SHA-256"); |
||
67 | byte[] result = mDigest.digest(id.getBytes()); |
||
68 | |||
69 | StringBuffer hexString = new StringBuffer(); |
||
70 | for (int i = 0; i < result.length; i++) { |
||
71 | hexString.append(Integer.toHexString(0xFF & result[i])); |
||
72 | } |
||
73 | |||
74 | |||
75 | return hexString.toString();
|
||
76 | } |
||
77 | |||
78 | public void saveFile(String file, byte[] info) throws Exception { |
||
79 | |||
80 | if(info.length > availableSpace())
|
||
81 | throw new Exception("file is huge......... NO space"); |
||
82 | |||
83 | File nfile = new File(base + "/restored/" + file); |
||
84 | System.out.println(info.length);
|
||
85 | |||
86 | try {
|
||
87 | |||
88 | if (nfile.createNewFile()) {
|
||
89 | FileOutputStream writer = new FileOutputStream(base + "/restored/" + file); |
||
90 | writer.write(info, 0, info.length);
|
||
91 | |||
92 | inUse -= info.length; |
||
93 | writer.close(); |
||
94 | } else {
|
||
95 | System.out.println("Fail creating file"); |
||
96 | } |
||
97 | } catch (Exception e) { |
||
98 | e.printStackTrace(); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | public boolean saveChunk(Chunk chunk, String fileId) { |
||
103 | if (chunk.getInfo().length > availableSpace())
|
||
104 | return false; |
||
105 | |||
106 | try {
|
||
107 | File dir = new File(base + "/" + "backup/" + fileId); |
||
108 | |||
109 | File nfile = new File(base + "/" + "backup/" + fileId + "/" + chunk.getId()); |
||
110 | dir.mkdir(); |
||
111 | |||
112 | if (nfile.createNewFile()) {
|
||
113 | |||
114 | FileOutputStream writer = new FileOutputStream(nfile); |
||
115 | writer.write(chunk.getInfo(), 0, chunk.getInfo().length);
|
||
116 | |||
117 | writer.close(); |
||
118 | |||
119 | inUse += chunk.getInfo().length; |
||
120 | |||
121 | return true; |
||
122 | } else {
|
||
123 | System.out.println("Fail creating file"); |
||
124 | } |
||
125 | |||
126 | |||
127 | } catch (Exception e) { |
||
128 | e.printStackTrace(); |
||
129 | } |
||
130 | |||
131 | |||
132 | return false; |
||
133 | } |
||
134 | |||
135 | public HashMap<String, byte[]> getChunks(String fileId) { |
||
136 | |||
137 | HashMap<String, byte[]> chunksInfo = new HashMap<>(); |
||
138 | String name;
|
||
139 | String dir = base + "/backup/" + fileId; |
||
140 | File[] files = new File(dir).listFiles(); |
||
141 | try {
|
||
142 | for (File abc : files) { |
||
143 | if (!abc.isDirectory()) {
|
||
144 | name = abc.getName(); |
||
145 | FileInputStream in = new FileInputStream(dir + "/" + name); |
||
146 | byte[] info = in.readAllBytes(); |
||
147 | chunksInfo.put(name, info); |
||
148 | } |
||
149 | } |
||
150 | } catch (Exception e) { |
||
151 | // e.printStackTrace();
|
||
152 | } |
||
153 | |||
154 | return chunksInfo;
|
||
155 | } |
||
156 | |||
157 | private void createDir() { |
||
158 | |||
159 | Path pathres = Paths.get(base + "/restored");
|
||
160 | Path pathchunks = Paths.get(base + "/backup");
|
||
161 | |||
162 | try {
|
||
163 | Files.createDirectories(pathres); |
||
164 | Files.createDirectory(pathchunks); |
||
165 | } catch (IOException e) { |
||
166 | // e.printStackTrace();
|
||
167 | } |
||
168 | |||
169 | } |
||
170 | |||
171 | |||
172 | public void delete(String fileID) { |
||
173 | try {
|
||
174 | |||
175 | String dir = base + "/backup/" + fileID; |
||
176 | |||
177 | File l = new File(dir); |
||
178 | System.out.println(dir);
|
||
179 | if (l.isDirectory()) {
|
||
180 | File[] file = l.listFiles(); |
||
181 | |||
182 | for (File i : file) { |
||
183 | this.inUse -= i.length();
|
||
184 | i.delete(); |
||
185 | |||
186 | } |
||
187 | |||
188 | l.delete(); |
||
189 | } |
||
190 | } catch (Exception e) { |
||
191 | e.printStackTrace(); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | public boolean reclaimSpace(int reclaimed) { |
||
196 | if(reclaimed >= inUse ) {
|
||
197 | maxSize = reclaimed; |
||
198 | return true; |
||
199 | } |
||
200 | else{
|
||
201 | try{
|
||
202 | peer.getControl().reduceChunks( inUse - reclaimed); |
||
203 | return true; |
||
204 | }catch (Exception e){ |
||
205 | e.printStackTrace(); |
||
206 | |||
207 | } |
||
208 | |||
209 | return false; |
||
210 | } |
||
211 | |||
212 | } |
||
213 | |||
214 | } |