Declare all local functions static
[pacman-ng.git] / lib / libalpm / be_package.c
blob90fd4124b249de66d2feb9702d6216583a1c5f25
1 /*
2 * be_package.c
4 * Copyright (c) 2006-2010 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <ctype.h>
28 #include <locale.h> /* setlocale */
29 #include <errno.h>
31 /* libarchive */
32 #include <archive.h>
33 #include <archive_entry.h>
35 /* libalpm */
36 #include "alpm_list.h"
37 #include "util.h"
38 #include "log.h"
39 #include "package.h"
40 #include "deps.h" /* _alpm_splitdep */
42 /**
43 * Open a package changelog for reading. Similar to fopen in functionality,
44 * except that the returned 'file stream' is from an archive.
45 * @param pkg the package (file) to read the changelog
46 * @return a 'file stream' to the package changelog
48 static void *_package_changelog_open(pmpkg_t *pkg)
50 ALPM_LOG_FUNC;
52 ASSERT(pkg != NULL, return(NULL));
54 struct archive *archive = NULL;
55 struct archive_entry *entry;
56 const char *pkgfile = pkg->origin_data.file;
58 if((archive = archive_read_new()) == NULL) {
59 RET_ERR(PM_ERR_LIBARCHIVE, NULL);
62 archive_read_support_compression_all(archive);
63 archive_read_support_format_all(archive);
65 if (archive_read_open_filename(archive, pkgfile,
66 ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
67 RET_ERR(PM_ERR_PKG_OPEN, NULL);
70 while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
71 const char *entry_name = archive_entry_pathname(entry);
73 if(strcmp(entry_name, ".CHANGELOG") == 0) {
74 return(archive);
77 /* we didn't find a changelog */
78 archive_read_finish(archive);
79 errno = ENOENT;
81 return(NULL);
84 /**
85 * Read data from an open changelog 'file stream'. Similar to fread in
86 * functionality, this function takes a buffer and amount of data to read.
87 * @param ptr a buffer to fill with raw changelog data
88 * @param size the size of the buffer
89 * @param pkg the package that the changelog is being read from
90 * @param fp a 'file stream' to the package changelog
91 * @return the number of characters read, or 0 if there is no more data
93 static size_t _package_changelog_read(void *ptr, size_t size,
94 const pmpkg_t *pkg, const void *fp)
96 ssize_t sret = archive_read_data((struct archive*)fp, ptr, size);
97 /* Report error (negative values) */
98 if(sret < 0) {
99 pm_errno = PM_ERR_LIBARCHIVE;
100 return(0);
101 } else {
102 return((size_t)sret);
107 static int _package_changelog_feof(const pmpkg_t *pkg, void *fp)
109 // note: this doesn't quite work, no feof in libarchive
110 return( archive_read_data((struct archive*)fp, NULL, 0) );
115 * Close a package changelog for reading. Similar to fclose in functionality,
116 * except that the 'file stream' is from an archive.
117 * @param pkg the package (file) that the changelog was read from
118 * @param fp a 'file stream' to the package changelog
119 * @return whether closing the package changelog stream was successful
121 static int _package_changelog_close(const pmpkg_t *pkg, void *fp)
123 return( archive_read_finish((struct archive *)fp) );
127 /** Package file operations struct accessor. We implement this as a method
128 * rather than a static struct as in be_files because we want to reuse the
129 * majority of the default_pkg_ops struct and add only a few operations of
130 * our own on top. The static file_pkg_ops variable inside this function
131 * lets us only initialize an operations struct once which can always be
132 * accessed by this method.
134 static struct pkg_operations *get_file_pkg_ops(void)
136 static struct pkg_operations *file_pkg_ops = NULL;
137 /* determine whether our static file_pkg_ops struct has been initialized */
138 if(!file_pkg_ops) {
139 MALLOC(file_pkg_ops, sizeof(struct pkg_operations),
140 RET_ERR(PM_ERR_MEMORY, NULL));
141 memcpy(file_pkg_ops, &default_pkg_ops, sizeof(struct pkg_operations));
142 file_pkg_ops->changelog_open = _package_changelog_open;
143 file_pkg_ops->changelog_read = _package_changelog_read;
144 file_pkg_ops->changelog_close = _package_changelog_close;
146 return(file_pkg_ops);
150 * Parses the package description file for a package into a pmpkg_t struct.
151 * @param archive the archive to read from, pointed at the .PKGINFO entry
152 * @param newpkg an empty pmpkg_t struct to fill with package info
154 * @return 0 on success, 1 on error
156 static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
158 char *ptr = NULL;
159 char *key = NULL;
160 int linenum = 0;
161 struct archive_read_buffer buf;
163 ALPM_LOG_FUNC;
165 memset(&buf, 0, sizeof(buf));
166 /* 512K for a line length seems reasonable */
167 buf.max_line_size = 512 * 1024;
169 /* loop until we reach EOF or other error */
170 while(_alpm_archive_fgets(a, &buf) == ARCHIVE_OK) {
171 char *line = _alpm_strtrim(buf.line);
173 linenum++;
174 if(strlen(line) == 0 || line[0] == '#') {
175 continue;
177 ptr = line;
178 key = strsep(&ptr, "=");
179 if(key == NULL || ptr == NULL) {
180 _alpm_log(PM_LOG_DEBUG, "%s: syntax error in description file line %d\n",
181 newpkg->name ? newpkg->name : "error", linenum);
182 } else {
183 key = _alpm_strtrim(key);
184 ptr = _alpm_strtrim(ptr);
185 if(strcmp(key, "pkgname") == 0) {
186 STRDUP(newpkg->name, ptr, RET_ERR(PM_ERR_MEMORY, -1));
187 newpkg->name_hash = _alpm_hash_sdbm(newpkg->name);
188 } else if(strcmp(key, "pkgver") == 0) {
189 STRDUP(newpkg->version, ptr, RET_ERR(PM_ERR_MEMORY, -1));
190 } else if(strcmp(key, "pkgdesc") == 0) {
191 STRDUP(newpkg->desc, ptr, RET_ERR(PM_ERR_MEMORY, -1));
192 } else if(strcmp(key, "force") == 0) {
193 /* For backward compatibility, like in sync_db_read */
194 if(!newpkg->epoch) {
195 newpkg->epoch = 1;
197 } else if(strcmp(key, "epoch") == 0) {
198 newpkg->epoch = atoi(ptr);
199 } else if(strcmp(key, "group") == 0) {
200 newpkg->groups = alpm_list_add(newpkg->groups, strdup(ptr));
201 } else if(strcmp(key, "url") == 0) {
202 STRDUP(newpkg->url, ptr, RET_ERR(PM_ERR_MEMORY, -1));
203 } else if(strcmp(key, "license") == 0) {
204 newpkg->licenses = alpm_list_add(newpkg->licenses, strdup(ptr));
205 } else if(strcmp(key, "builddate") == 0) {
206 char first = tolower((unsigned char)ptr[0]);
207 if(first > 'a' && first < 'z') {
208 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
209 setlocale(LC_TIME, "C");
210 strptime(ptr, "%a %b %e %H:%M:%S %Y", &tmp_tm);
211 newpkg->builddate = mktime(&tmp_tm);
212 setlocale(LC_TIME, "");
213 } else {
214 newpkg->builddate = atol(ptr);
216 } else if(strcmp(key, "packager") == 0) {
217 STRDUP(newpkg->packager, ptr, RET_ERR(PM_ERR_MEMORY, -1));
218 } else if(strcmp(key, "arch") == 0) {
219 STRDUP(newpkg->arch, ptr, RET_ERR(PM_ERR_MEMORY, -1));
220 } else if(strcmp(key, "size") == 0) {
221 /* size in the raw package is uncompressed (installed) size */
222 newpkg->isize = atol(ptr);
223 } else if(strcmp(key, "depend") == 0) {
224 pmdepend_t *dep = _alpm_splitdep(ptr);
225 newpkg->depends = alpm_list_add(newpkg->depends, dep);
226 } else if(strcmp(key, "optdepend") == 0) {
227 newpkg->optdepends = alpm_list_add(newpkg->optdepends, strdup(ptr));
228 } else if(strcmp(key, "conflict") == 0) {
229 newpkg->conflicts = alpm_list_add(newpkg->conflicts, strdup(ptr));
230 } else if(strcmp(key, "replaces") == 0) {
231 newpkg->replaces = alpm_list_add(newpkg->replaces, strdup(ptr));
232 } else if(strcmp(key, "provides") == 0) {
233 newpkg->provides = alpm_list_add(newpkg->provides, strdup(ptr));
234 } else if(strcmp(key, "backup") == 0) {
235 newpkg->backup = alpm_list_add(newpkg->backup, strdup(ptr));
236 } else if(strcmp(key, "makepkgopt") == 0) {
237 /* not used atm */
238 } else {
239 _alpm_log(PM_LOG_DEBUG, "%s: syntax error in description file line %d\n",
240 newpkg->name ? newpkg->name : "error", linenum);
243 line[0] = '\0';
246 return(0);
250 * Load a package and create the corresponding pmpkg_t struct.
251 * @param pkgfile path to the package file
252 * @param full whether to stop the load after metadata is read or continue
253 * through the full archive
254 * @return An information filled pmpkg_t struct
256 static pmpkg_t *pkg_load(const char *pkgfile, int full)
258 int ret = ARCHIVE_OK;
259 int config = 0;
260 struct archive *archive;
261 struct archive_entry *entry;
262 pmpkg_t *newpkg = NULL;
263 struct stat st;
265 ALPM_LOG_FUNC;
267 if(pkgfile == NULL || strlen(pkgfile) == 0) {
268 RET_ERR(PM_ERR_WRONG_ARGS, NULL);
271 if(stat(pkgfile, &st) != 0) {
272 RET_ERR(PM_ERR_PKG_OPEN, NULL);
275 if((archive = archive_read_new()) == NULL) {
276 RET_ERR(PM_ERR_LIBARCHIVE, NULL);
279 archive_read_support_compression_all(archive);
280 archive_read_support_format_all(archive);
282 if (archive_read_open_filename(archive, pkgfile,
283 ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
284 RET_ERR(PM_ERR_PKG_OPEN, NULL);
287 newpkg = _alpm_pkg_new();
288 if(newpkg == NULL) {
289 archive_read_finish(archive);
290 RET_ERR(PM_ERR_MEMORY, NULL);
293 newpkg->filename = strdup(pkgfile);
294 newpkg->size = st.st_size;
296 /* If full is false, only read through the archive until we find our needed
297 * metadata. If it is true, read through the entire archive, which serves
298 * as a verfication of integrity and allows us to create the filelist. */
299 while((ret = archive_read_next_header(archive, &entry)) == ARCHIVE_OK) {
300 const char *entry_name = archive_entry_pathname(entry);
302 if(strcmp(entry_name, ".PKGINFO") == 0) {
303 /* parse the info file */
304 if(parse_descfile(archive, newpkg) != 0) {
305 _alpm_log(PM_LOG_ERROR, _("could not parse package description file in %s\n"),
306 pkgfile);
307 goto pkg_invalid;
309 if(newpkg->name == NULL || strlen(newpkg->name) == 0) {
310 _alpm_log(PM_LOG_ERROR, _("missing package name in %s\n"), pkgfile);
311 goto pkg_invalid;
313 if(newpkg->version == NULL || strlen(newpkg->version) == 0) {
314 _alpm_log(PM_LOG_ERROR, _("missing package version in %s\n"), pkgfile);
315 goto pkg_invalid;
317 config = 1;
318 continue;
319 } else if(strcmp(entry_name, ".INSTALL") == 0) {
320 newpkg->scriptlet = 1;
321 } else if(*entry_name == '.') {
322 /* for now, ignore all files starting with '.' that haven't
323 * already been handled (for future possibilities) */
324 } else {
325 /* Keep track of all files for filelist generation */
326 newpkg->files = alpm_list_add(newpkg->files, strdup(entry_name));
329 if(archive_read_data_skip(archive)) {
330 _alpm_log(PM_LOG_ERROR, _("error while reading package %s: %s\n"),
331 pkgfile, archive_error_string(archive));
332 pm_errno = PM_ERR_LIBARCHIVE;
333 goto error;
336 /* if we are not doing a full read, see if we have all we need */
337 if(!full && config) {
338 break;
342 if(ret != ARCHIVE_EOF && ret != ARCHIVE_OK) { /* An error occured */
343 _alpm_log(PM_LOG_ERROR, _("error while reading package %s: %s\n"),
344 pkgfile, archive_error_string(archive));
345 pm_errno = PM_ERR_LIBARCHIVE;
346 goto error;
349 if(!config) {
350 _alpm_log(PM_LOG_ERROR, _("missing package metadata in %s\n"), pkgfile);
351 goto pkg_invalid;
354 archive_read_finish(archive);
356 /* internal fields for package struct */
357 newpkg->origin = PKG_FROM_FILE;
358 /* TODO eventually kill/move this? */
359 newpkg->origin_data.file = strdup(pkgfile);
360 newpkg->ops = get_file_pkg_ops();
362 if(full) {
363 /* "checking for conflicts" requires a sorted list, ensure that here */
364 _alpm_log(PM_LOG_DEBUG, "sorting package filelist for %s\n", pkgfile);
365 newpkg->files = alpm_list_msort(newpkg->files, alpm_list_count(newpkg->files),
366 _alpm_str_cmp);
367 newpkg->infolevel = INFRQ_ALL;
368 } else {
369 /* get rid of any partial filelist we may have collected, it is invalid */
370 FREELIST(newpkg->files);
371 newpkg->infolevel = INFRQ_BASE | INFRQ_DESC;
374 return(newpkg);
376 pkg_invalid:
377 pm_errno = PM_ERR_PKG_INVALID;
378 error:
379 _alpm_pkg_free(newpkg);
380 archive_read_finish(archive);
382 return(NULL);
385 /** Create a package from a file.
386 * If full is false, the archive is read only until all necessary
387 * metadata is found. If it is true, the entire archive is read, which
388 * serves as a verfication of integrity and the filelist can be created.
389 * @param filename location of the package tarball
390 * @param full whether to stop the load after metadata is read or continue
391 * through the full archive
392 * @param pkg address of the package pointer
393 * @return 0 on success, -1 on error (pm_errno is set accordingly)
395 int SYMEXPORT alpm_pkg_load(const char *filename, int full, pmpkg_t **pkg)
397 ALPM_LOG_FUNC;
399 /* Sanity checks */
400 ASSERT(filename != NULL && strlen(filename) != 0,
401 RET_ERR(PM_ERR_WRONG_ARGS, -1));
402 ASSERT(pkg != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
404 *pkg = pkg_load(filename, full);
405 if(*pkg == NULL) {
406 /* pm_errno is set by pkg_load */
407 return(-1);
410 return(0);
413 /* vim: set ts=2 sw=2 noet: */