-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResources.java
More file actions
executable file
·460 lines (295 loc) · 11.8 KB
/
Resources.java
File metadata and controls
executable file
·460 lines (295 loc) · 11.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
import sun.plugin.javascript.navig.Array;
import javax.imageio.IIOException;
import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.io.*;
public class Resources {
public static String path = "/Users/k/git/Network-Project-1/";
public static String foldername = "DriveCloud/";
public static File dir = new File (path+foldername);
public static Hashtable hash_table_for_files = new Hashtable();
public Resources(){}
public static Boolean is_changed(){
// changes if any file is changed
ArrayList temp= get_changes_files();
ArrayList<File> changed_files =get_changes_files();
ArrayList<String> deleted_files =get_deleted_filenames();
if (changed_files.isEmpty() && deleted_files.isEmpty()){
return false;
}else{
return true;
}
}
public static void get_data_from_metafile () throws IOException
{
// reads the metafile and checks the current status for changes
File metafile = new File(path+ foldername+"metafile.txt");
if(metafile.createNewFile()){
//System.out.println("File Created");
}
Scanner sc = new Scanner(metafile);
while (sc.hasNextLine()){
String[] line =sc.nextLine().split(" ");
hash_table_for_files.put(line[0],Long.parseLong(line[1]));
}
}
public static void create_metafile ()throws IOException{
//creating a metafile to store changes
File metafile = new File(path+foldername+"metafile.txt");
//checking if the file already exists
if (!metafile.createNewFile()) {
// truncating the file if it already exist
try (FileChannel outChan = new FileOutputStream(metafile, true).getChannel()) {
outChan.truncate(0);
}
}
// update the metafile information
File [] files = dir.listFiles();
FileWriter fileWriter = new FileWriter(metafile);
PrintWriter printWriter = new PrintWriter(fileWriter);
long hashcode = -1;
String str = "";
for (File file_entry : files)
{
{
// adding hashcode to the meta file to compare files later on
hashcode=file_entry.lastModified();
str=str+file_entry.getName()+ " ";
if(!str.contains("metafile.txt")&&!str.contains(".DS_Store")){
//System.out.println(file_entry.getName());
str=str+hashcode;
printWriter.println(str);
}
str = "";
}
}
printWriter.close();
}
public static ArrayList<File> get_changes_files() {
try {
get_data_from_metafile ();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<File> changed_files =new ArrayList<File>();
//get current files
File [] current_files =dir.listFiles();
long hashcode = 0;
if (current_files != null) {
//this part is to find added and modified files
for (File file_entry : current_files)
{
//We dont need to compare metafile
if(!file_entry.getName().equals("metafile.txt")&&!file_entry.getName().equals(".DS_Store"))
{
hashcode=file_entry.lastModified();
if (hash_table_for_files.containsKey(file_entry.getName()) && (long)hash_table_for_files.get(file_entry.getName())!=hashcode) {
// System.out.println("Modified file "+file_entry.getName());
changed_files.add(file_entry);
}else if(!hash_table_for_files.containsKey(file_entry.getName())){
// System.out.println("Added file "+file_entry.getName());
changed_files.add(file_entry);
}
hash_table_for_files.remove(hashcode);
}
}
}
return changed_files;
}
public static ArrayList<String> get_deleted_filenames() {
try {
get_data_from_metafile ();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<File> changed_files =new ArrayList<File>();
ArrayList<String> deleted_files=new ArrayList<String>();
//get current files
File [] current_files =dir.listFiles();
long hashcode = 0;
if (current_files != null) {
for (File file_entry : current_files)
{
//this part is to find added and modified files
//We dont need to compare metafile
if(!file_entry.getName().equals("metafile.txt")&&!file_entry.getName().equals(".DS_Store"))
{
hashcode=file_entry.lastModified();
if (hash_table_for_files.containsKey(file_entry.getName()) && (long)hash_table_for_files.get(file_entry.getName())!=hashcode) {
changed_files.add(file_entry);
}else if(!hash_table_for_files.containsKey(file_entry.getName())){
changed_files.add(file_entry);
}
hash_table_for_files.remove(file_entry.getName());
}
}
}
//this part is for finding deleted files
//if it is not empty it means that there are some deleted files. Because we have deleted all modified and unmodified elements before.
if(!hash_table_for_files .isEmpty())
{
//System.out.println("Finding deleted files.");
//This iteration is for finding the deleted file.
for(File file: current_files)
{
if(hash_table_for_files.containsKey(file.getName()))
{
hash_table_for_files.remove(file.getName());
}
}
}
if(!hash_table_for_files .isEmpty())
{
for (Object deletedfile: hash_table_for_files.keySet()){
deleted_files.add((String)deletedfile);
}
}
return deleted_files;
}
public static String get_changes_names() {
ArrayList<File> changed_files =get_changes_files();
ArrayList<String> deleted_files =get_deleted_filenames();
String str = "";
for(File changed_file: changed_files)
{
str = str + changed_file.getName()+",";
}
for(String deleted_file: deleted_files)
{
str = str + deleted_file+",";
}
if(str.length()>=1){
str = str.substring(0,str.length()-1);
}
return str;
}
public static String get_changes_sizes() {
ArrayList<File> changed_files = get_changes_files();
ArrayList<String> deleted_files =get_deleted_filenames();
String str = "";
for(File changed_file: changed_files)
{
str=str + changed_file.length()+",";
}
for(String deleted_file: deleted_files)
{
str=str + (-999)+",";
}
if(str.length()>=1){
str =str.substring(0,str.length()-1);
}
return str;
}
public static String get_checksums(){
ArrayList<File> changed_files = get_changes_files();
String str = "";
for(File changed_file: changed_files)
{
str=str + changed_file.hashCode()+",";
}
if(str.length()>=1){
str = str.substring(0,str.length()-1);
}
return str;
}
public static String get_checksum_for(String filename){
ArrayList<File> changed_files = get_changes_files();
File [] files = dir.listFiles();
String str = "";
for(File file: files)
{
if(file.getName().equals(filename)){
str=str+file.hashCode();
}
}
return str;
}
public static void send_files(Socket socket, ArrayList<File> files) {
BufferedInputStream bufferedInputStream;
FileInputStream fileInputStream;
OutputStream outputStream;
byte[] byteArray;
int byteArrayLen;
for (File file : files) {
try {
byteArray = new byte[(int) file.length()];
byteArrayLen = byteArray.length;
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(byteArray, 0, byteArrayLen);
outputStream = socket.getOutputStream();
outputStream.write(byteArray, 0, byteArrayLen);
System.out.flush();
outputStream.flush();
System.out.flush();
bufferedInputStream.close();
fileInputStream.close();
} catch (Exception e) {
// System.out.println("Send Error:"+e);
}
}
}
public static boolean receive_files(Socket socket, String filesToReceive, String size_filesToReceive) {
InputStream inputStream;
FileOutputStream fileOutputStream;
BufferedOutputStream bufferedOutputStream;
String[] files = filesToReceive.split(",");
String[] filesSize = size_filesToReceive.split(",");
// String[] fileCs = fileChecksums.split(",");
int currentPtr = 0;
int readBytes;
byte[] byteArr;
try {
inputStream = socket.getInputStream();
for (int i = 0; i < files.length; i++){
String file = files[i];
String file_path = path+foldername+file;
File file_obj = new File(file_path);
file_obj.delete();
if (!filesSize[i].equals("-999") && filesSize[i].length()!=0) {
int size = Integer.parseInt(filesSize[i]);
byteArr = new byte[size];
fileOutputStream = new FileOutputStream(file_path);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
readBytes = inputStream.read(byteArr, 0, byteArr.length);
currentPtr = readBytes;
do {
int newPtr = byteArr.length - currentPtr;
readBytes = inputStream.read(byteArr, currentPtr, newPtr);
if (readBytes > 0) currentPtr += readBytes;
// System.out.println(readBytes);
} while (readBytes != 0);
bufferedOutputStream.write(byteArr, 0, currentPtr);
bufferedOutputStream.flush();
bufferedOutputStream.close();
fileOutputStream.close();
//String this_checksum = Resources.get_checksum_for(file);
//String that_checksum = fileCs[i];
// if (!this_checksum.equals(that_checksum)) return false;
}
}
return true;
} catch (IOException e) {
System.out.println("Receive Error");
}
return false;
}
public static void main(String[] args) throws IOException,InterruptedException
{
//
//get_data_from_metafile ();
//Thread.sleep(30*1000);
ArrayList<File> test = get_changes_files();
ArrayList<String> test2 = get_deleted_filenames();
//get_changes_names();
//get_changes_sizes();
//create_metafile();
}
}