-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
221 lines (191 loc) · 6.88 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <complex.h>
#define BUFFER_SIZE 10000
char communicate[1000];
void install(char *package) {
char buffer[BUFFER_SIZE];
if (getcwd(buffer, sizeof(buffer)) == NULL) {
perror("获取运行目录失败");
exit(EXIT_FAILURE);
}
//printf("当前运行目录:%s\n", buffer);
printf("解压包体文件,这可能会花费一部分时间(以root权限)\n");
char sfs_job[BUFFER_SIZE];
snprintf(sfs_job, BUFFER_SIZE, "sudo unsquashfs %s %s/temp", package, buffer);
system(sfs_job);
char file_cd_job[BUFFER_SIZE];
snprintf(file_cd_job, BUFFER_SIZE, "cd %s/temp/squashfs-root", buffer);
system(file_cd_job);
FILE *package_head_file_read;
char package_head_read_job[100];
package_head_file_read = fopen("package_head", "r");
if (package_head_file_read == NULL) {
printf("包头未找到,结束\n");
return;
}
fgets(package_head_read_job, 100, package_head_file_read);
fclose(package_head_file_read);
char file_cd_2rd_part_job[BUFFER_SIZE];
snprintf(file_cd_2rd_part_job, BUFFER_SIZE, "cd %s", buffer);
system(file_cd_2rd_part_job);
printf("即将开始执行包安装步骤(以root权限)\n");
char install_job[BUFFER_SIZE];
snprintf(install_job, BUFFER_SIZE, "sudo bash %s/temp/squashfs-root/install.sh", buffer);
system(install_job);
char package_tail_file_path[BUFFER_SIZE];
snprintf(package_tail_file_path, BUFFER_SIZE, "%s/temp/squashfs-root/package-tail", buffer);
FILE *package_tail_file_read;
char package_tail_read_job[100];
package_tail_file_read = fopen(package_tail_file_path, "r");
if (package_tail_file_read == NULL) {
printf("包尾未找到,结束");
return;
}
fgets(package_tail_read_job, 100, package_tail_file_read);
fclose(package_tail_file_read);
//char cp_uninstall_script_job[BUFFER_SIZE];
//snprintf(cp_uninstall_script_job, BUFFER_SIZE, "bash %s/temp/squashfs-root/uninstall.sh", buffer);
//system(cp_uninstall_script_job);
char list_file_dir[BUFFER_SIZE];
snprintf(list_file_dir, BUFFER_SIZE, "%s/installed-list", buffer);
FILE *LIST_ADD = fopen(list_file_dir, "a+");
if (LIST_ADD == NULL) {
printf("找不到包文件集,结束\n");
return;
}
fprintf(LIST_ADD, "\n%s", package_head_read_job);
fclose(LIST_ADD);
char add_pkg[BUFFER_SIZE];
snprintf(add_pkg, BUFFER_SIZE, "%s/pkg_dir/%s", buffer, package_head_read_job);
FILE *FILE_ADD = fopen(add_pkg, "a+");
fprintf(FILE_ADD, "%s", package_tail_read_job);
fclose(FILE_ADD);
printf("安装完成,进行清理操作(以root权限运行)\n");
char rm_file_job[BUFFER_SIZE];
snprintf(rm_file_job, BUFFER_SIZE, "sudo rm -rf %s/temp/*", buffer);
system(rm_file_job);
}
void uninstall(char *uninstall_package){
char buffer[BUFFER_SIZE];
if (getcwd(buffer, sizeof(buffer)) == NULL) {
perror("获取运行目录失败");
exit(EXIT_FAILURE);
}
//printf("当前运行目录:%s\n", buffer);
char list_file_dir[BUFFER_SIZE];
snprintf(list_file_dir, BUFFER_SIZE, "%s/pkg_dir/%s", buffer, uninstall_package);
FILE *list_path = fopen(list_file_dir, "r");
if (list_path == NULL){
printf("软件未安装,结束");
return;
}
fclose(list_path);
char *pkg_path = malloc(sizeof(char) * BUFFER_SIZE);
if (pkg_path == NULL) {
printf("内存分配失败\n");
exit(EXIT_FAILURE);
}
FILE *list_file = fopen(list_file_dir, "r");
if (list_file == NULL) {
printf("找不到软件包路径,结束\n");
free(pkg_path);
return;
}
fgets(pkg_path, BUFFER_SIZE, list_file);
fclose(list_file);
printf("删除软件操作不可逆,您确定要删除该软件包吗?(y/N)");
char yN[10];
scanf("%s", yN);
if (strcmp(yN, "y") == 0){
char uninstall_command[BUFFER_SIZE];
snprintf(uninstall_command, BUFFER_SIZE, "rm -rf %s", pkg_path);
system(uninstall_command);
} else if (strcmp(yN, "n") == 0) {
printf("操作已取消,结束");
} else {
printf("输入无效,操作已取消,结束");
}
free(pkg_path);
}
void update(char *update_package){
char buffer[BUFFER_SIZE];
if (getcwd(buffer, sizeof(buffer)) == NULL) {
perror("获取运行目录失败");
exit(EXIT_FAILURE);
}
//printf("当前运行目录:%s\n", buffer);
printf("解压包体文件,这可能会花费一部分时间(以root权限)\n");
char update_sfs_job[BUFFER_SIZE];
snprintf(update_sfs_job, BUFFER_SIZE, "sudo unsquashfs %s %s/temp", update_package, buffer);
system(update_sfs_job);
char update_script_dir[BUFFER_SIZE];
snprintf(update_script_dir, BUFFER_SIZE, "%s/temp/squashfs-root/update.sh", buffer);
FILE *update = fopen(update_script_dir, "r");
if (update == NULL) {
printf("该软件没有升级脚本,请联系开发者或重新安装\n");
return;
}
fclose(update);
char update_command[BUFFER_SIZE];
snprintf(update_command, BUFFER_SIZE, "bash %s/temp/squashfs-root/update.sh", buffer);
system(update_command);
}
void list(){
char buffer[BUFFER_SIZE];
if (getcwd(buffer, sizeof(buffer)) == NULL) {
perror("获取运行目录失败");
exit(EXIT_FAILURE);
}
printf("当前运行目录:%s\n", buffer);
char list_file_dir[BUFFER_SIZE];
snprintf(list_file_dir, BUFFER_SIZE, "%s/installed-list", buffer);
char list_command[BUFFER_SIZE];
snprintf(list_command, BUFFER_SIZE, "cat %s", list_file_dir);
system(list_command);
}
void help(){
printf("Trix Package Manager\n");
printf("by zxtsd\n");
printf("Usage tpm [-i/-u/--list/--help] [包路径/包名/无需参数/无需参数] [升级包路径]\n");
printf("-i 安装软件包 [tpm -i []]\n");
printf("-u 卸载软件包\n");
printf("--list 已安装软件列表\n");
printf("--help 展示该帮助\n");
}
int main(int argc, char *argv[]) {
if (argc > 2) {
if (strcmp(argv[1], "-i") == 0) {
install(argv[2]);
return 0;
}else {
printf("参数错误,结束\n");
return 127;
}
if(strcmp(argv[1], "-u") == 0){
uninstall(argv[2]);
return 0;
}else {
printf("参数错误,结束\n");
return 127;
}
if (strcmp(argv[1], "--list") == 0) {
list();
return 0;
}else {
printf("参数错误,结束\n");
return 127;
}
if(strcmp(argv[1], "--help") == 0){
help();
return 0;
}else {
printf("参数错误,结束\n");
return 127;
}
}
help();
return 0;
}