2012-07-12 16:29:59 -04:00
|
|
|
/*
|
|
|
|
* filelist.c
|
|
|
|
*
|
2013-01-01 23:19:59 -05:00
|
|
|
* Copyright (c) 2012-2013 Pacman Development Team <pacman-dev@archlinux.org>
|
2012-07-12 16:29:59 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-08-06 22:08:04 -04:00
|
|
|
#include <limits.h>
|
2012-07-12 16:29:59 -04:00
|
|
|
#include <string.h>
|
2012-08-06 22:08:04 -04:00
|
|
|
#include <sys/stat.h>
|
2012-07-12 16:29:59 -04:00
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "filelist.h"
|
2012-08-06 22:08:04 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
/** Helper function for comparing strings when sorting */
|
|
|
|
static int _alpm_filelist_strcmp(const void *s1, const void *s2)
|
|
|
|
{
|
|
|
|
return strcmp(*(char **)s1, *(char **)s2);
|
|
|
|
}
|
|
|
|
|
2012-08-06 22:18:15 -04:00
|
|
|
/* TODO make sure callers check the return value so we can bail on errors.
|
|
|
|
* For now we soldier on as best we can, skipping paths that are too long to
|
|
|
|
* resolve and using the original filenames on memory errors. */
|
2012-08-06 22:08:04 -04:00
|
|
|
/**
|
|
|
|
* @brief Resolves a symlink and its children.
|
|
|
|
*
|
|
|
|
* @attention Pre-condition: files must be sorted!
|
|
|
|
*
|
|
|
|
* @param files filelist to resolve
|
2012-08-06 22:18:15 -04:00
|
|
|
* @param i pointer to the index in files to start processing, will point to
|
|
|
|
* the last file processed on return
|
2012-08-06 22:08:04 -04:00
|
|
|
* @param path absolute path for the symlink being resolved
|
|
|
|
* @param root_len length of the root portion of path
|
|
|
|
* @param resolving is file \i in \files a symlink that needs to be resolved
|
|
|
|
*
|
2012-08-06 22:18:15 -04:00
|
|
|
* @return 0 on success, -1 on error
|
2012-08-06 22:08:04 -04:00
|
|
|
*/
|
2012-08-06 22:18:15 -04:00
|
|
|
int _alpm_filelist_resolve_link(alpm_filelist_t *files, size_t *i,
|
|
|
|
char *path, size_t root_len, int resolving)
|
2012-08-06 22:08:04 -04:00
|
|
|
{
|
|
|
|
char *causal_dir = NULL; /* symlink being resolved */
|
|
|
|
char *filename_r = NULL; /* resolved filename */
|
|
|
|
size_t causal_dir_len = 0, causal_dir_r_len = 0;
|
|
|
|
|
|
|
|
if(resolving) {
|
|
|
|
/* deal with the symlink being resolved */
|
|
|
|
MALLOC(filename_r, PATH_MAX, goto error);
|
2012-08-06 22:18:15 -04:00
|
|
|
causal_dir = files->files[*i].name;
|
2012-08-06 22:08:04 -04:00
|
|
|
causal_dir_len = strlen(causal_dir);
|
|
|
|
if(realpath(path, filename_r) == NULL) {
|
2012-08-06 22:18:15 -04:00
|
|
|
files->resolved_path[*i] = causal_dir;
|
2012-08-06 22:08:04 -04:00
|
|
|
FREE(filename_r);
|
2012-08-06 22:18:15 -04:00
|
|
|
return -1;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
causal_dir_r_len = strlen(filename_r + root_len) + 1;
|
|
|
|
if(causal_dir_r_len >= PATH_MAX) {
|
2012-08-06 22:18:15 -04:00
|
|
|
files->resolved_path[*i] = causal_dir;
|
2012-08-06 22:08:04 -04:00
|
|
|
FREE(filename_r);
|
2012-08-06 22:18:15 -04:00
|
|
|
return -1;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
/* remove root_r from filename_r */
|
|
|
|
memmove(filename_r, filename_r + root_len, causal_dir_r_len);
|
|
|
|
filename_r[causal_dir_r_len - 1] = '/';
|
|
|
|
filename_r[causal_dir_r_len] = '\0';
|
2012-08-06 22:18:15 -04:00
|
|
|
STRDUP(files->resolved_path[*i], filename_r, goto error);
|
|
|
|
(*i)++;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
|
2012-08-06 22:18:15 -04:00
|
|
|
for(; *i < files->count; (*i)++) {
|
|
|
|
char *filename = files->files[*i].name;
|
2012-08-06 22:08:04 -04:00
|
|
|
size_t filename_len = strlen(filename);
|
|
|
|
size_t filename_r_len = filename_len;
|
|
|
|
struct stat sbuf;
|
|
|
|
int exists;
|
|
|
|
|
|
|
|
if(resolving) {
|
|
|
|
if(filename_len < causal_dir_len || strncmp(filename, causal_dir, causal_dir_len) != 0) {
|
|
|
|
/* not inside causal_dir anymore */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename_r_len = filename_len + causal_dir_r_len - causal_dir_len;
|
|
|
|
if(filename_r_len >= PATH_MAX) {
|
|
|
|
/* resolved path is too long */
|
2012-08-06 22:18:15 -04:00
|
|
|
files->resolved_path[*i] = filename;
|
2012-08-06 22:08:04 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(filename_r + causal_dir_r_len, filename + causal_dir_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deal with files and paths too long to resolve*/
|
|
|
|
if(filename[filename_len - 1] != '/' || root_len + filename_r_len >= PATH_MAX) {
|
2012-08-06 22:18:14 -04:00
|
|
|
if(resolving) {
|
2012-08-06 22:18:15 -04:00
|
|
|
STRDUP(files->resolved_path[*i], filename_r, goto error);
|
2012-08-06 22:18:14 -04:00
|
|
|
} else {
|
2012-08-06 22:18:15 -04:00
|
|
|
files->resolved_path[*i] = filename;
|
2012-08-06 22:18:14 -04:00
|
|
|
}
|
2012-08-06 22:08:04 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* construct absolute path and stat() */
|
2012-08-06 22:18:14 -04:00
|
|
|
strcpy(path + root_len, resolving ? filename_r : filename);
|
2012-08-06 22:08:04 -04:00
|
|
|
exists = !_alpm_lstat(path, &sbuf);
|
|
|
|
|
|
|
|
/* deal with symlinks */
|
|
|
|
if(exists && S_ISLNK(sbuf.st_mode)) {
|
2012-08-06 22:18:15 -04:00
|
|
|
_alpm_filelist_resolve_link(files, i, path, root_len, 1);
|
2012-08-06 22:08:04 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deal with normal directories */
|
2012-08-06 22:18:14 -04:00
|
|
|
if(resolving) {
|
2012-08-06 22:18:15 -04:00
|
|
|
STRDUP(files->resolved_path[*i], filename_r, goto error);
|
2012-08-06 22:18:14 -04:00
|
|
|
} else {
|
2012-08-06 22:18:15 -04:00
|
|
|
files->resolved_path[*i] = filename;
|
2012-08-06 22:18:14 -04:00
|
|
|
}
|
2012-08-06 22:08:04 -04:00
|
|
|
|
|
|
|
/* deal with children of non-existent directories to reduce lstat() calls */
|
2013-01-03 16:48:53 -05:00
|
|
|
if(!exists) {
|
2012-08-06 22:18:15 -04:00
|
|
|
for((*i)++; *i < files->count; (*i)++) {
|
|
|
|
char *f = files->files[*i].name;
|
2012-08-06 22:08:04 -04:00
|
|
|
size_t f_len = strlen(f);
|
|
|
|
size_t f_r_len;
|
|
|
|
|
|
|
|
if(f_len < filename_len || strncmp(f, filename, filename_len) != 0) {
|
|
|
|
/* not inside the non-existent dir anymore */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
f_r_len = f_len + causal_dir_r_len - causal_dir_len;
|
|
|
|
if(resolving && f_r_len <= PATH_MAX) {
|
|
|
|
strcpy(filename_r + causal_dir_r_len, f + causal_dir_len);
|
2012-08-06 22:18:15 -04:00
|
|
|
STRDUP(files->resolved_path[*i], filename_r, goto error);
|
2012-08-06 22:08:04 -04:00
|
|
|
} else {
|
2012-08-06 22:18:15 -04:00
|
|
|
files->resolved_path[*i] = f;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 22:18:15 -04:00
|
|
|
(*i)--;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 22:18:15 -04:00
|
|
|
(*i)--;
|
2012-08-06 22:08:04 -04:00
|
|
|
|
2012-08-06 22:18:14 -04:00
|
|
|
FREE(filename_r);
|
2012-08-06 22:08:04 -04:00
|
|
|
|
2012-08-06 22:18:15 -04:00
|
|
|
return 0;
|
2012-08-06 22:08:04 -04:00
|
|
|
|
|
|
|
error:
|
2012-08-06 22:18:14 -04:00
|
|
|
FREE(filename_r);
|
|
|
|
/* out of memory, set remaining files to their original names */
|
2012-08-06 22:18:15 -04:00
|
|
|
for(; *i < files->count; (*i)++) {
|
|
|
|
files->resolved_path[*i] = files->files[*i].name;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
2012-08-06 22:18:15 -04:00
|
|
|
(*i)--;
|
|
|
|
return -1;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Takes a file list and resolves all directory paths according to the
|
|
|
|
* filesystem
|
|
|
|
*
|
|
|
|
* @attention Pre-condition: files must be sorted!
|
|
|
|
*
|
|
|
|
* @note A symlink and directory at the same path in two difference packages
|
|
|
|
* causes a conflict so the filepath can not change as packages get installed
|
|
|
|
*
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param files list of files to resolve
|
2012-08-06 22:18:15 -04:00
|
|
|
*
|
|
|
|
* @return 0 on success, -1 on error
|
2012-08-06 22:08:04 -04:00
|
|
|
*/
|
2012-08-06 22:18:15 -04:00
|
|
|
int _alpm_filelist_resolve(alpm_handle_t *handle, alpm_filelist_t *files)
|
2012-08-06 22:08:04 -04:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
2013-01-03 16:48:51 -05:00
|
|
|
size_t root_len, i = 0;
|
2012-08-06 22:18:15 -04:00
|
|
|
int ret = 0;
|
2012-08-06 22:08:04 -04:00
|
|
|
|
|
|
|
if(!files || files->resolved_path) {
|
2012-08-06 22:18:15 -04:00
|
|
|
return 0;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
|
2012-08-06 22:18:15 -04:00
|
|
|
CALLOC(files->resolved_path, files->count, sizeof(char *), return -1);
|
2012-08-06 22:08:04 -04:00
|
|
|
|
|
|
|
/* not much point in going on if we can't even resolve root */
|
|
|
|
if(realpath(handle->root, path) == NULL){
|
2012-08-06 22:18:15 -04:00
|
|
|
return -1;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
root_len = strlen(path) + 1;
|
|
|
|
if(root_len >= PATH_MAX) {
|
2012-08-06 22:18:15 -04:00
|
|
|
return -1;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
path[root_len - 1] = '/';
|
|
|
|
path[root_len] = '\0';
|
|
|
|
|
2012-08-06 22:18:15 -04:00
|
|
|
ret = _alpm_filelist_resolve_link(files, &i, path, root_len, 0);
|
2012-08-06 22:08:04 -04:00
|
|
|
|
|
|
|
qsort(files->resolved_path, files->count, sizeof(char *),
|
|
|
|
_alpm_filelist_strcmp);
|
|
|
|
|
2012-08-06 22:18:15 -04:00
|
|
|
return ret;
|
2012-08-06 22:08:04 -04:00
|
|
|
}
|
|
|
|
|
2012-07-12 16:29:59 -04:00
|
|
|
|
2012-07-17 22:09:24 -04:00
|
|
|
/* Returns the difference of the provided two lists of files.
|
2012-07-12 16:29:59 -04:00
|
|
|
* Pre-condition: both lists are sorted!
|
|
|
|
* When done, free the list but NOT the contained data.
|
|
|
|
*/
|
2012-07-17 22:09:24 -04:00
|
|
|
alpm_list_t *_alpm_filelist_difference(alpm_filelist_t *filesA,
|
|
|
|
alpm_filelist_t *filesB)
|
2012-07-12 16:29:59 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *ret = NULL;
|
|
|
|
size_t ctrA = 0, ctrB = 0;
|
|
|
|
|
|
|
|
while(ctrA < filesA->count && ctrB < filesB->count) {
|
|
|
|
alpm_file_t *fileA = filesA->files + ctrA;
|
2012-07-20 10:17:18 -04:00
|
|
|
const char *strA = filesA->resolved_path[ctrA];
|
|
|
|
const char *strB = filesB->resolved_path[ctrB];
|
2012-12-06 07:35:22 -05:00
|
|
|
|
|
|
|
int cmp = strcmp(strA, strB);
|
|
|
|
if(cmp < 0) {
|
|
|
|
/* item only in filesA, qualifies as a difference */
|
|
|
|
ret = alpm_list_add(ret, fileA);
|
2012-07-12 16:29:59 -04:00
|
|
|
ctrA++;
|
2012-12-06 07:35:22 -05:00
|
|
|
} else if(cmp > 0) {
|
2012-07-12 16:29:59 -04:00
|
|
|
ctrB++;
|
|
|
|
} else {
|
2012-12-06 07:35:22 -05:00
|
|
|
ctrA++;
|
|
|
|
ctrB++;
|
2012-07-17 22:09:24 -04:00
|
|
|
}
|
2012-07-12 16:29:59 -04:00
|
|
|
}
|
|
|
|
|
2012-07-17 22:09:24 -04:00
|
|
|
/* ensure we have completely emptied pA */
|
|
|
|
while(ctrA < filesA->count) {
|
2012-07-12 16:29:59 -04:00
|
|
|
alpm_file_t *fileA = filesA->files + ctrA;
|
2012-12-06 07:35:22 -05:00
|
|
|
ret = alpm_list_add(ret, fileA);
|
2012-07-12 16:29:59 -04:00
|
|
|
ctrA++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-17 22:09:24 -04:00
|
|
|
/* Returns the intersection of the provided two lists of files.
|
|
|
|
* Pre-condition: both lists are sorted!
|
|
|
|
* When done, free the list but NOT the contained data.
|
|
|
|
*/
|
|
|
|
alpm_list_t *_alpm_filelist_intersection(alpm_filelist_t *filesA,
|
|
|
|
alpm_filelist_t *filesB)
|
|
|
|
{
|
|
|
|
alpm_list_t *ret = NULL;
|
|
|
|
size_t ctrA = 0, ctrB = 0;
|
|
|
|
|
|
|
|
while(ctrA < filesA->count && ctrB < filesB->count) {
|
2012-07-17 22:25:37 -04:00
|
|
|
int cmp, isdirA, isdirB;
|
|
|
|
char *strA, *strB;
|
|
|
|
|
|
|
|
isdirA = 0;
|
2012-07-20 08:25:18 -04:00
|
|
|
strA = filesA->resolved_path[ctrA];
|
2012-07-17 22:09:24 -04:00
|
|
|
if(strA[strlen(strA)-1] == '/') {
|
2012-07-17 22:25:37 -04:00
|
|
|
isdirA = 1;
|
2012-07-20 08:25:18 -04:00
|
|
|
strA = strndup(filesA->resolved_path[ctrA], strlen(strA)-1);
|
2012-07-17 22:25:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isdirB = 0;
|
2012-07-20 08:25:18 -04:00
|
|
|
strB = filesB->resolved_path[ctrB];
|
2012-07-17 22:25:37 -04:00
|
|
|
if(strB[strlen(strB)-1] == '/') {
|
|
|
|
isdirB = 1;
|
2012-07-20 08:25:18 -04:00
|
|
|
strB = strndup(filesB->resolved_path[ctrB], strlen(strB)-1);
|
2012-07-17 22:25:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmp = strcmp(strA, strB);
|
|
|
|
if(cmp < 0) {
|
2012-07-17 22:09:24 -04:00
|
|
|
ctrA++;
|
2012-07-17 22:25:37 -04:00
|
|
|
} else if(cmp > 0) {
|
2012-07-17 22:09:24 -04:00
|
|
|
ctrB++;
|
|
|
|
} else {
|
2012-07-17 22:25:37 -04:00
|
|
|
/* TODO: this creates conflicts between a symlink to a directory in
|
|
|
|
* one package and a real directory in the other. For example,
|
|
|
|
* lib -> /usr/lib in pkg1 and /lib in pkg2. This would be allowed
|
|
|
|
* when installing one package at a time _provided_ pkg1 is installed
|
|
|
|
* first. This will need adjusted if the order of package install can
|
|
|
|
* be guaranteed to install the symlink first */
|
|
|
|
|
|
|
|
/* when not directories, item in both qualifies as an intersect */
|
|
|
|
if(! (isdirA && isdirB)) {
|
2012-07-20 08:25:18 -04:00
|
|
|
ret = alpm_list_add(ret, filesA->files + ctrA);
|
2012-07-17 22:09:24 -04:00
|
|
|
}
|
2012-07-17 22:25:37 -04:00
|
|
|
ctrA++;
|
|
|
|
ctrB++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isdirA) {
|
|
|
|
free(strA);
|
|
|
|
}
|
|
|
|
if(isdirB) {
|
|
|
|
free(strB);
|
2012-07-17 22:09:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-12 16:29:59 -04:00
|
|
|
/* Helper function for comparing files list entries
|
|
|
|
*/
|
|
|
|
int _alpm_files_cmp(const void *f1, const void *f2)
|
|
|
|
{
|
|
|
|
const alpm_file_t *file1 = f1;
|
|
|
|
const alpm_file_t *file2 = f2;
|
|
|
|
return strcmp(file1->name, file2->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
alpm_file_t *alpm_filelist_contains(alpm_filelist_t *filelist,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
alpm_file_t key;
|
|
|
|
|
|
|
|
if(!filelist) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
key.name = (char *)path;
|
|
|
|
|
|
|
|
return bsearch(&key, filelist->files, filelist->count,
|
|
|
|
sizeof(alpm_file_t), _alpm_files_cmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|