root / src / disk / Disk.java @ 2
History | View | Annotate | Download (5.68 KB)
1 |
package disk; |
---|---|
2 |
|
3 |
import chunk.*; |
4 |
import utils.Utils; |
5 |
|
6 |
import java.io.File; |
7 |
import java.io.FileOutputStream; |
8 |
import java.io.FileInputStream; |
9 |
import java.io.IOException; |
10 |
|
11 |
/**
|
12 |
* Disk
|
13 |
*/
|
14 |
public class Disk { |
15 |
|
16 |
public static final String fileSeparator = System.getProperty("file.separator"); |
17 |
public static final String filesPath = "." + fileSeparator + "files" + fileSeparator; |
18 |
public static final String resourcesPath = filesPath + "resources" + fileSeparator; |
19 |
public static final String defaultDiskLocation = filesPath + "peers" + fileSeparator; |
20 |
public static final long defaultDiskSize = 10000000; /* Disk size in KBytes */ |
21 |
|
22 |
private long size; /* Disk size in bytes */ |
23 |
private String diskLocation; |
24 |
private File directory; |
25 |
private File backupDirectory, restoredDirectory; |
26 |
|
27 |
public void restoreFile(Chunk[] chunks, String fileName) { |
28 |
Chunk.restoreFile(chunks, getRestoredDirectoryPath() + fileSeparator + fileName); |
29 |
} |
30 |
|
31 |
public Disk(String diskName) { |
32 |
this(diskName, defaultDiskSize);
|
33 |
} |
34 |
|
35 |
public Disk(String diskName, float size) { |
36 |
this.diskLocation = defaultDiskLocation + diskName;
|
37 |
this.size = (long) (size * 1000); |
38 |
createDiskDirectory(); |
39 |
} |
40 |
|
41 |
/**
|
42 |
* @return the backupDirectory
|
43 |
*/
|
44 |
public File getBackupDirectory() { |
45 |
return backupDirectory;
|
46 |
} |
47 |
|
48 |
/**
|
49 |
* @return the defaultDiskLocation
|
50 |
*/
|
51 |
public static String getDefaultDiskLocation() { |
52 |
return defaultDiskLocation;
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* @return the directory
|
57 |
*/
|
58 |
public File getDirectory() { |
59 |
return directory;
|
60 |
} |
61 |
|
62 |
/**
|
63 |
* @return the restoredDirectory
|
64 |
*/
|
65 |
public File getRestoredDirectory() { |
66 |
return restoredDirectory;
|
67 |
} |
68 |
|
69 |
public String getRestoredDirectoryPath() { |
70 |
return restoredDirectory.getAbsolutePath();
|
71 |
} |
72 |
|
73 |
public boolean createDiskDirectory() { |
74 |
directory = new File(this.diskLocation); |
75 |
directory.mkdirs(); |
76 |
createBackupDirectory(); |
77 |
createRestoredDirectory(); |
78 |
return true; |
79 |
} |
80 |
|
81 |
public boolean createBackupDirectory() { |
82 |
backupDirectory = new File(this.diskLocation + fileSeparator + "backup"); |
83 |
if (backupDirectory.mkdirs()) {
|
84 |
return true; |
85 |
} |
86 |
return false; |
87 |
} |
88 |
|
89 |
public boolean createRestoredDirectory() { |
90 |
restoredDirectory = new File(this.diskLocation + fileSeparator + "restored"); |
91 |
if (restoredDirectory.mkdirs()) {
|
92 |
return true; |
93 |
} |
94 |
return false; |
95 |
} |
96 |
|
97 |
public String getDiskLocation() { |
98 |
return diskLocation;
|
99 |
} |
100 |
|
101 |
public long getSize() { |
102 |
return size;
|
103 |
} |
104 |
|
105 |
public long getOccupiedSpace() { |
106 |
return folderSize(this.directory); |
107 |
} |
108 |
|
109 |
public long getFreeSpace() { |
110 |
return size - getOccupiedSpace();
|
111 |
} |
112 |
|
113 |
public static long folderSize(File directory) { |
114 |
long length = 0; |
115 |
for (File file : directory.listFiles()) { |
116 |
if (file.isFile())
|
117 |
length += file.length(); |
118 |
else
|
119 |
length += folderSize(file); |
120 |
} |
121 |
return length;
|
122 |
} |
123 |
|
124 |
public File createFileFolder(String fileId) { |
125 |
File fileFolder = new File(backupDirectory.getPath() + fileSeparator + fileId); |
126 |
fileFolder.mkdirs(); |
127 |
return fileFolder;
|
128 |
} |
129 |
|
130 |
public boolean storeChunk(Chunk chunk) { |
131 |
String fileName = chunk.getChunkNo() + "-" + chunk.getRepDegree(); |
132 |
|
133 |
File folder = createFileFolder(chunk.getFileID());
|
134 |
|
135 |
File chunkFile = new File(folder.getPath() + fileSeparator + fileName); |
136 |
|
137 |
if (chunkFile.exists()) {
|
138 |
return false; |
139 |
} |
140 |
|
141 |
try (FileOutputStream fos = new FileOutputStream(chunkFile)) { |
142 |
fos.write(chunk.getData()); |
143 |
} catch (Exception e) { |
144 |
e.printStackTrace(); |
145 |
return false; |
146 |
} |
147 |
|
148 |
return true; |
149 |
} |
150 |
|
151 |
public File[] getFileChunkFiles(String fileId) { |
152 |
File fileChunkDirectory = getFileChunkDirectory(fileId);
|
153 |
return fileChunkDirectory.listFiles();
|
154 |
} |
155 |
|
156 |
public Chunk[] getFileChunks(String fileId) { |
157 |
File chunkFiles[] = getFileChunkFiles(fileId); |
158 |
|
159 |
Chunk[] chunks = new Chunk[chunkFiles.length]; |
160 |
|
161 |
for (int i = 0; i < chunkFiles.length; i++) { |
162 |
chunks[i] = parseFileToChunk(fileId, chunkFiles[i]); |
163 |
} |
164 |
|
165 |
return chunks;
|
166 |
} |
167 |
|
168 |
public File getChunkFile(String fileId, int chunkId) { |
169 |
File chunkFiles[] = getFileChunkFiles(fileId); |
170 |
for (File chunkFile : chunkFiles) { |
171 |
if (chunkFile.getName().startsWith(chunkId + "-")) { |
172 |
return chunkFile;
|
173 |
} |
174 |
} |
175 |
return null; |
176 |
} |
177 |
|
178 |
public Chunk getChunk(String fileId, int chunkId) { |
179 |
Chunk[] chunks = getFileChunks(fileId);
|
180 |
|
181 |
for (Chunk chunk : chunks) {
|
182 |
if (chunk.getChunkNo() == chunkId) {
|
183 |
return chunk;
|
184 |
} |
185 |
} |
186 |
return null; |
187 |
} |
188 |
|
189 |
public File getFileChunkDirectory(String fileId) { |
190 |
return new File(backupDirectory.getPath() + fileSeparator + fileId); |
191 |
} |
192 |
|
193 |
public static Chunk parseFileToChunk(String fileId, File chunkFile) { |
194 |
String fileName = chunkFile.getName();
|
195 |
String parsedName[] = fileName.split("-"); |
196 |
|
197 |
int chunkId = Integer.parseInt(parsedName[0]); |
198 |
int repDegree = Integer.parseInt(parsedName[1]); |
199 |
|
200 |
byte[] data = new byte[(int) chunkFile.length()]; |
201 |
|
202 |
try (FileInputStream fis = new FileInputStream(chunkFile)) { |
203 |
fis.read(data); |
204 |
} catch (Exception e) { |
205 |
|
206 |
} |
207 |
|
208 |
Chunk chunk = new Chunk(fileId, chunkId, repDegree, data);
|
209 |
|
210 |
return chunk;
|
211 |
} |
212 |
|
213 |
public void deleteFileDirectory(String fileId) { |
214 |
//File fileDir = getFileChunkDirectory(fileId);
|
215 |
/*try {
|
216 |
Utils.deleteDirectoryRecursively(fileDir);
|
217 |
} catch (IOException e) {
|
218 |
e.printStackTrace();
|
219 |
return false;
|
220 |
}
|
221 |
return true;
|
222 |
*/
|
223 |
File fileDir = new File(backupDirectory + "\\" + fileId); |
224 |
System.out.println("FileDir: \"" + fileDir + "\""); |
225 |
Utils.deleteDirectoryRecursively(fileDir); |
226 |
} |
227 |
|
228 |
public boolean deleteChunk(String fileId, int chunkId) { |
229 |
File chunkFile = getChunkFile(fileId, chunkId);
|
230 |
if (!chunkFile.delete()) {
|
231 |
System.out.println("Failed to delete " + chunkFile); |
232 |
return false; |
233 |
} |
234 |
return true; |
235 |
} |
236 |
|
237 |
} |