-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage.cs
180 lines (147 loc) · 4.7 KB
/
Package.cs
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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace AWPM
{
internal enum ReturnStates
{
PACKAGE_SETUP,
PREPEAR,
INSTALL,
POST_INSTALL,
ERR_PREP,
ERR_CHECKSUM,
ERR_INSTALL_NO_DEST_DIR,
ERR_POST_INSTALL,
ERR_CLEAN,
ERR_PACKAGE_ROOT_CONTATINS_FILES,
ERR_PACKAGE_DIRS_VERIFICATION_FAILED,
OK
}
internal struct Package
{
public string url;
public string packageName;
public int versionMajor;
public int versionMinor;
public int versionPatch;
public int versionBuild;
public string tempWorkingDirectory;
public string packageFileName;
public ReturnStates state;
public string errMessage;
public byte[] checksum;
bool optional;
bool exists;
// bool isSource;
}
internal class PackageInstaller
{
private List<Package> installPackages;
private List<Package> dependencies;
private string dataFolder;
private void copyDirectory(string sourcedir, string destdir)
{
if (!Directory.Exists(destdir))
{
Directory.CreateDirectory(destdir);
}
foreach (var filePath in Directory.GetFiles(sourcedir))
{
File.Copy(filePath, Path.Combine(destdir,
Path.GetFileName(filePath)));
}
foreach (var subDir in Directory.GetDirectories(sourcedir))
{
this.copyDirectory(subDir, Path.Combine(destdir,
Path.GetFileName(subDir)));
}
}
private void postInstall(Package package)
{
}
private ReturnStates check(Package package)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(package.tempWorkingDirectory
+ package.packageFileName))
{
byte[] hash = md5.ComputeHash(stream);
if (hash != package.checksum)
{
return ReturnStates.ERR_CHECKSUM;
}
}
}
return ReturnStates.OK;
}
private ReturnStates installPackage (Package package)
{
string[] allowedDirs = { "bin", "lib", "opt" };
// unpacking
if (!Directory.Exists(this.dataFolder))
return ReturnStates.ERR_INSTALL_NO_DEST_DIR;
if (Directory.GetFiles(package.tempWorkingDirectory).Any())
{
return ReturnStates.ERR_PACKAGE_ROOT_CONTATINS_FILES;
}
var subdirs = Directory.GetDirectories(package.tempWorkingDirectory);
if (!subdirs.All(dir => allowedDirs.Contains(dir)))
{
return ReturnStates.ERR_PACKAGE_DIRS_VERIFICATION_FAILED;
}
this.copyDirectory(package.tempWorkingDirectory, this.dataFolder);
return ReturnStates.OK;
}
private void build(Package package)
{
}
private void prepear(Package package)
{
}
private void get(Package package)
{
}
private void clean()
{
foreach (var package in this.installPackages)
{
Directory.Delete(package.tempWorkingDirectory, true);
}
}
public (string, ReturnStates) install()
{
foreach (Package package in dependencies)
{
this.prepear(package);
if (package.state != ReturnStates.PREPEAR)
{
return (package.errMessage, package.state);
}
this.installPackage(package);
if (package.state != ReturnStates.INSTALL)
{
return (package.errMessage, package.state);
}
this.postInstall(package);
if (package.state != ReturnStates.POST_INSTALL)
{
return (package.errMessage, package.state);
}
}
string s = this.clean();
if (s != "")
{
return (s, ReturnStates.ERR_CLEAN);
}
return ("", ReturnStates.OK);
}
public PackageInstaller(List<Package> packages, List<Package> deps)
{
this.installPackages = packages;
this.dependencies = deps;
}
}
}