-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSync.java
More file actions
125 lines (124 loc) · 4.26 KB
/
Sync.java
File metadata and controls
125 lines (124 loc) · 4.26 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
/* Alec Snyder
* cs162
* Class that syncs all in the ~/gdrive folder with drive
* revision system so that if you change a file, it gets updated
* metadata in XML format stored under ~/gdrive/gdrive.xml */
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
public class Sync
{
public static ArrayList<DriveFile> populateFiles()
{
ArrayList<DriveFile> files=new ArrayList<DriveFile>();
String home=System.getProperty("user.home")+"/gdrive/";
EasyReader reader=new EasyReader(home+".drive.xml");
String line=reader.readLine();
reader.readLine();
line=reader.readLine();
while(!line.equals("</files>"))
{
line=reader.readLine();
String name=line.substring(8,line.length()-7);
line=reader.readLine();
String id=line.substring(6,line.length()-5);
line=reader.readLine();
String mdsum=line.substring(9,line.length()-8);
DriveFile f=new DriveFile();
f.name=name;
f.fid=id;
f.md5sum=mdsum;
files.add(f);
reader.readLine();
line=reader.readLine();
}
reader.close();
return files;
}
public static void main(String[] args)
{
ArrayList<DriveFile> files=Sync.populateFiles();
String home=System.getProperty("user.home")+"/gdrive/";
ArrayList<String> ls=new ArrayList<String>();
File driveDir=new File(System.getProperty("user.home")+"/gdrive");
File[] fs=driveDir.listFiles();
for(int i=0; i<fs.length; i++)
{
if(!fs[i].isHidden())
ls.add(fs[i].getAbsolutePath());
}
for(int i=0; i<ls.size(); i++)
{
if(DriveFile.isIn(files, ls.get(i))==null)
{
try
{
String id=DriveInsert.up(ls.get(i));
DriveFile newFile=new DriveFile();
newFile.name=ls.get(i);
newFile.fid=id;
try
{
newFile.md5sum=DriveFile.getMdSum(newFile.name);
}
catch(IOException e)
{
System.out.println("error creating md5sum for "+ls.get(i));
newFile.md5sum="temp";
}
files.add(newFile);
}
catch(IOException e)
{
System.out.println("Error, unable to upload "+ls.get(i));
}
}
else
{
try
{
DriveFile match=DriveFile.isIn(files,ls.get(i));
String sum=DriveFile.getMdSum(ls.get(i));
if(!sum.equals(match.md5sum))
{
DriveRemove.removeFid(match.fid);
match.fid=DriveInsert.up(ls.get(i));
match.md5sum=sum;
}
}
catch(IOException e)
{
System.out.println("Unable to resolve md5 update for "+ls.get(i));
}
}
}
for(int i=0; i<files.size(); i++)
{
if(!ls.contains(files.get(i).name))
{
try
{
DriveRemove.removeFid(files.get(i).fid);
files.remove(i);
}
catch(IOException e)
{
System.out.println("Error deleting file: "+files.get(i).name);
}
}
}
EasyWriter writer=new EasyWriter(home+".drive.xml");
writer.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
writer.print("<files>\n");
for(int i=0; i<files.size(); i++)
{
writer.print("\t<file>\n");
writer.print("\t\t<name>"+files.get(i).name+"</name>\n");
writer.print("\t\t<id>"+files.get(i).fid+"</id>\n");
writer.print("\t\t<mdsum>"+files.get(i).md5sum+"</mdsum>\n");
writer.print("\t</file>\n");
}
writer.print("</files>");
writer.close();
}
}