]>
Commit | Line | Data |
---|---|---|
221cc3ce MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2014 Pakfire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <assert.h> | |
5e9463ec | 22 | #include <errno.h> |
c98132d1 | 23 | #include <linux/limits.h> |
221cc3ce MT |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <sys/stat.h> | |
28 | #include <sys/types.h> | |
29 | #include <tar.h> | |
30 | #include <time.h> | |
31 | ||
9a6e3e2d MT |
32 | #include <archive_entry.h> |
33 | ||
221cc3ce MT |
34 | #include <pakfire/constants.h> |
35 | #include <pakfire/file.h> | |
883b3be9 | 36 | #include <pakfire/pakfire.h> |
9f953e68 | 37 | #include <pakfire/private.h> |
221cc3ce MT |
38 | #include <pakfire/util.h> |
39 | ||
5e9463ec | 40 | struct _PakfireFile { |
883b3be9 | 41 | Pakfire pakfire; |
5e9463ec | 42 | int nrefs; |
221cc3ce | 43 | |
0027da6f MT |
44 | const char* path; |
45 | char abspath[PATH_MAX]; | |
5e9463ec MT |
46 | char type; |
47 | ssize_t size; | |
221cc3ce | 48 | |
c98132d1 MT |
49 | char user[256]; |
50 | char group[256]; | |
221cc3ce | 51 | |
5e9463ec | 52 | mode_t mode; |
ef4e8460 MT |
53 | |
54 | time_t ctime; | |
55 | time_t mtime; | |
d03fa9a3 | 56 | |
5e9463ec MT |
57 | char* chksum; |
58 | ||
59 | #warning TODO capabilities, config, data | |
60 | // capabilities | |
61 | //int is_configfile; | |
62 | //int is_datafile; | |
63 | }; | |
64 | ||
883b3be9 | 65 | PAKFIRE_EXPORT int pakfire_file_create(PakfireFile* file, Pakfire pakfire) { |
90312f5c | 66 | PakfireFile f = calloc(1, sizeof(*f)); |
5e9463ec MT |
67 | if (!f) |
68 | return -ENOMEM; | |
69 | ||
883b3be9 | 70 | f->pakfire = pakfire_ref(pakfire); |
5e9463ec MT |
71 | f->nrefs = 1; |
72 | ||
73 | *file = f; | |
74 | return 0; | |
d03fa9a3 MT |
75 | } |
76 | ||
548b6515 MT |
77 | PAKFIRE_EXPORT int pakfire_file_copy_stat(PakfireFile file, struct stat* stat) { |
78 | if (!stat) | |
79 | return EINVAL; | |
80 | ||
548b6515 MT |
81 | // Set mode |
82 | pakfire_file_set_mode(file, stat->st_mode); | |
83 | ||
84 | // Set size | |
85 | pakfire_file_set_size(file, stat->st_size); | |
86 | ||
87 | // Set user | |
88 | // XXX | |
89 | pakfire_file_set_user(file, "root"); | |
90 | pakfire_file_set_group(file, "root"); | |
91 | ||
ef4e8460 MT |
92 | // Set times |
93 | pakfire_file_set_ctime(file, stat->st_ctime); | |
94 | pakfire_file_set_mtime(file, stat->st_mtime); | |
548b6515 MT |
95 | |
96 | return 0; | |
97 | } | |
98 | ||
3a9677db MT |
99 | struct archive_entry* pakfire_file_archive_entry(PakfireFile file) { |
100 | struct archive_entry* entry = archive_entry_new(); | |
101 | if (!entry) | |
102 | return NULL; | |
103 | ||
104 | // Set path | |
105 | archive_entry_copy_pathname(entry, pakfire_file_get_path(file)); | |
106 | ||
107 | // Set source path | |
108 | archive_entry_copy_sourcepath(entry, file->abspath); | |
109 | ||
110 | // Set size | |
111 | archive_entry_set_size(entry, pakfire_file_get_size(file)); | |
112 | ||
113 | // Set mode | |
114 | archive_entry_set_mode(entry, pakfire_file_get_mode(file)); | |
115 | ||
116 | // Set user | |
117 | archive_entry_set_uname(entry, pakfire_file_get_user(file)); | |
118 | ||
119 | // Set group | |
120 | archive_entry_set_gname(entry, pakfire_file_get_group(file)); | |
121 | ||
122 | // Set times | |
123 | archive_entry_set_ctime(entry, pakfire_file_get_ctime(file), 0); | |
124 | archive_entry_set_mtime(entry, pakfire_file_get_mtime(file), 0); | |
125 | ||
126 | return entry; | |
127 | } | |
128 | ||
9a6e3e2d | 129 | int pakfire_file_copy_archive_entry(PakfireFile file, struct archive_entry* entry) { |
32485f6c MT |
130 | // Set path |
131 | pakfire_file_set_path(file, archive_entry_pathname(entry)); | |
9a6e3e2d MT |
132 | |
133 | // Set size | |
134 | pakfire_file_set_size(file, archive_entry_size(entry)); | |
135 | ||
136 | // Set mode | |
137 | pakfire_file_set_mode(file, archive_entry_mode(entry)); | |
138 | ||
139 | // Set user | |
140 | pakfire_file_set_user(file, archive_entry_uname(entry)); | |
141 | ||
142 | // Set group | |
143 | pakfire_file_set_group(file, archive_entry_gname(entry)); | |
144 | ||
ef4e8460 MT |
145 | // Set times |
146 | pakfire_file_set_ctime(file, archive_entry_ctime(entry)); | |
147 | pakfire_file_set_mtime(file, archive_entry_mtime(entry)); | |
9a6e3e2d MT |
148 | |
149 | return 0; | |
150 | } | |
151 | ||
5e9463ec | 152 | static void pakfire_file_free(PakfireFile file) { |
883b3be9 MT |
153 | pakfire_unref(file->pakfire); |
154 | ||
5e9463ec | 155 | if (file->chksum) |
f0d6233d | 156 | free(file->chksum); |
221cc3ce | 157 | |
f0d6233d | 158 | free(file); |
221cc3ce MT |
159 | } |
160 | ||
5e9463ec MT |
161 | PAKFIRE_EXPORT PakfireFile pakfire_file_ref(PakfireFile file) { |
162 | file->nrefs++; | |
221cc3ce | 163 | |
5e9463ec MT |
164 | return file; |
165 | } | |
221cc3ce | 166 | |
5e9463ec MT |
167 | PAKFIRE_EXPORT PakfireFile pakfire_file_unref(PakfireFile file) { |
168 | if (--file->nrefs > 0) | |
169 | return file; | |
170 | ||
171 | pakfire_file_free(file); | |
172 | return NULL; | |
221cc3ce MT |
173 | } |
174 | ||
9f953e68 | 175 | PAKFIRE_EXPORT int pakfire_file_cmp(PakfireFile file1, PakfireFile file2) { |
32485f6c MT |
176 | const char* path1 = pakfire_file_get_path(file1); |
177 | const char* path2 = pakfire_file_get_path(file2); | |
221cc3ce | 178 | |
32485f6c | 179 | return strcmp(path1, path2); |
221cc3ce MT |
180 | } |
181 | ||
32485f6c MT |
182 | PAKFIRE_EXPORT const char* pakfire_file_get_path(PakfireFile file) { |
183 | return file->path; | |
221cc3ce MT |
184 | } |
185 | ||
0027da6f MT |
186 | PAKFIRE_EXPORT int pakfire_file_set_path(PakfireFile file, const char* path) { |
187 | char* p = pakfire_make_path(file->pakfire, path); | |
188 | if (!p) | |
189 | return 1; | |
190 | ||
191 | // Copy to abspath | |
192 | snprintf(file->abspath, sizeof(file->abspath) - 1, "%s", p); | |
193 | free(p); | |
194 | ||
195 | // Store relative path | |
196 | file->path = file->abspath + strlen(file->abspath) - strlen(path); | |
197 | ||
198 | return 0; | |
221cc3ce MT |
199 | } |
200 | ||
f9770f19 MT |
201 | PAKFIRE_EXPORT int pakfire_file_get_type(PakfireFile file) { |
202 | return file->mode & S_IFMT; | |
221cc3ce MT |
203 | } |
204 | ||
9f953e68 | 205 | PAKFIRE_EXPORT ssize_t pakfire_file_get_size(PakfireFile file) { |
221cc3ce MT |
206 | return file->size; |
207 | } | |
208 | ||
9f953e68 | 209 | PAKFIRE_EXPORT void pakfire_file_set_size(PakfireFile file, ssize_t size) { |
221cc3ce MT |
210 | file->size = size; |
211 | } | |
212 | ||
9f953e68 | 213 | PAKFIRE_EXPORT const char* pakfire_file_get_user(PakfireFile file) { |
221cc3ce MT |
214 | return file->user; |
215 | } | |
216 | ||
9f953e68 | 217 | PAKFIRE_EXPORT void pakfire_file_set_user(PakfireFile file, const char* user) { |
c98132d1 | 218 | snprintf(file->user, sizeof(file->user) - 1, "%s", user); |
221cc3ce MT |
219 | } |
220 | ||
9f953e68 | 221 | PAKFIRE_EXPORT const char* pakfire_file_get_group(PakfireFile file) { |
221cc3ce MT |
222 | return file->group; |
223 | } | |
224 | ||
9f953e68 | 225 | PAKFIRE_EXPORT void pakfire_file_set_group(PakfireFile file, const char* group) { |
c98132d1 | 226 | snprintf(file->group, sizeof(file->group) - 1, "%s", group); |
221cc3ce MT |
227 | } |
228 | ||
9f953e68 | 229 | PAKFIRE_EXPORT mode_t pakfire_file_get_mode(PakfireFile file) { |
221cc3ce MT |
230 | return file->mode; |
231 | } | |
232 | ||
9f953e68 | 233 | PAKFIRE_EXPORT void pakfire_file_set_mode(PakfireFile file, mode_t mode) { |
221cc3ce MT |
234 | file->mode = mode; |
235 | } | |
236 | ||
ef4e8460 MT |
237 | PAKFIRE_EXPORT time_t pakfire_file_get_ctime(PakfireFile file) { |
238 | return file->ctime; | |
239 | } | |
240 | ||
241 | PAKFIRE_EXPORT void pakfire_file_set_ctime(PakfireFile file, time_t time) { | |
242 | file->ctime = time; | |
243 | } | |
244 | ||
245 | PAKFIRE_EXPORT time_t pakfire_file_get_mtime(PakfireFile file) { | |
246 | return file->mtime; | |
221cc3ce MT |
247 | } |
248 | ||
ef4e8460 MT |
249 | PAKFIRE_EXPORT void pakfire_file_set_mtime(PakfireFile file, time_t time) { |
250 | file->mtime = time; | |
221cc3ce MT |
251 | } |
252 | ||
9f953e68 | 253 | PAKFIRE_EXPORT const char* pakfire_file_get_chksum(PakfireFile file) { |
221cc3ce MT |
254 | return file->chksum; |
255 | } | |
256 | ||
9f953e68 | 257 | PAKFIRE_EXPORT void pakfire_file_set_chksum(PakfireFile file, const char* chksum) { |
ffbef232 MT |
258 | if (file->chksum) { |
259 | free(file->chksum); | |
260 | file->chksum = NULL; | |
261 | } | |
262 | ||
263 | if (chksum) | |
264 | file->chksum = strdup(chksum); | |
221cc3ce | 265 | } |
3a9677db MT |
266 | |
267 | FILE* pakfire_file_fopen(PakfireFile file, const char* mode) { | |
268 | if (!*file->abspath) | |
269 | return NULL; | |
270 | ||
271 | return fopen(file->abspath, mode); | |
272 | } |