-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.c
More file actions
149 lines (130 loc) · 3.75 KB
/
Copy pathdb.c
File metadata and controls
149 lines (130 loc) · 3.75 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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
#include "upac.h"
static InstalledPackage *db_packages = NULL;
static int db_count = 0;
static int db_capacity = 0;
static int db_initialized = 0;
int upac_db_init(void) {
if (db_initialized) return 0;
FILE *fp = fopen(UPAC_DB_PATH, "r");
if (!fp) {
if (errno == ENOENT) {
char dir[1024];
snprintf(dir, sizeof(dir), "%s", UPAC_DB_PATH);
char *slash = strrchr(dir, '/');
if (slash) {
*slash = '\0';
mkdir_p(dir);
}
fp = fopen(UPAC_DB_PATH, "w");
if (fp) fclose(fp);
db_initialized = 1;
return 0;
}
perror("upac: cannot open database");
return -1;
}
char line[1024];
while (fgets(line, sizeof(line), fp)) {
if (db_count >= db_capacity) {
db_capacity = db_capacity == 0 ? 64 : db_capacity * 2;
db_packages = realloc(db_packages, db_capacity * sizeof(InstalledPackage));
}
InstalledPackage *pkg = &db_packages[db_count];
if (sscanf(line, "%255s %63s %d %511s",
pkg->name, pkg->version, &pkg->revision, pkg->install_path) == 4) {
db_count++;
}
}
fclose(fp);
db_initialized = 1;
return 0;
}
int upac_lock_db(void) {
char lock_path[1024];
snprintf(lock_path, sizeof(lock_path), "%s.lock", UPAC_DB_PATH);
char dir[1024];
snprintf(dir, sizeof(dir), "%s", lock_path);
char *slash = strrchr(dir, '/');
if (slash) {
*slash = '\0';
mkdir_p(dir);
}
int fd = open(lock_path, O_CREAT | O_RDWR, 0644);
if (fd < 0) return -1;
if (flock(fd, LOCK_EX) != 0) {
close(fd);
return -1;
}
return fd;
}
void upac_unlock_db(int fd) {
if (fd >= 0) {
flock(fd, LOCK_UN);
close(fd);
}
}
int upac_db_add(const InstalledPackage *pkg) {
for (int i = 0; i < db_count; i++) {
if (strcmp(db_packages[i].name, pkg->name) == 0) {
memcpy(&db_packages[i], pkg, sizeof(InstalledPackage));
goto save;
}
}
if (db_count >= db_capacity) {
db_capacity = db_capacity == 0 ? 64 : db_capacity * 2;
db_packages = realloc(db_packages, db_capacity * sizeof(InstalledPackage));
}
memcpy(&db_packages[db_count++], pkg, sizeof(InstalledPackage));
save:
return upac_db_save();
}
int upac_db_save(void) {
FILE *fp = fopen(UPAC_DB_PATH, "w");
if (!fp) {
perror("upac: cannot write database");
return -1;
}
for (int i = 0; i < db_count; i++) {
fprintf(fp, "%s %s %d %s\n",
db_packages[i].name,
db_packages[i].version,
db_packages[i].revision,
db_packages[i].install_path);
}
fclose(fp);
return 0;
}
int upac_db_remove(const char *name) {
for (int i = 0; i < db_count; i++) {
if (strcmp(db_packages[i].name, name) == 0) {
memmove(&db_packages[i], &db_packages[i+1],
(db_count - i - 1) * sizeof(InstalledPackage));
db_count--;
return upac_db_save();
}
}
return -1;
}
int upac_db_find(const char *name, InstalledPackage *result) {
for (int i = 0; i < db_count; i++) {
if (strcmp(db_packages[i].name, name) == 0) {
if (result) memcpy(result, &db_packages[i], sizeof(InstalledPackage));
return 0;
}
}
return -1;
}
int upac_db_list_all(InstalledPackage **list, int *count) {
if (list) *list = db_packages;
if (count) *count = db_count;
return 0;
}