2012-07-12 16:29:59 -04:00
|
|
|
/*
|
|
|
|
* filelist.c
|
|
|
|
*
|
2015-01-21 01:31:20 -05:00
|
|
|
* Copyright (c) 2012-2015 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"
|
|
|
|
|
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) {
|
2013-04-12 20:37:56 -04:00
|
|
|
char *strA = filesA->files[ctrA].name;
|
|
|
|
char *strB = filesB->files[ctrB].name;
|
2012-12-06 07:35:22 -05:00
|
|
|
|
|
|
|
int cmp = strcmp(strA, strB);
|
|
|
|
if(cmp < 0) {
|
|
|
|
/* item only in filesA, qualifies as a difference */
|
2013-02-15 19:22:15 -05:00
|
|
|
ret = alpm_list_add(ret, strA);
|
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) {
|
2013-04-12 20:37:56 -04:00
|
|
|
ret = alpm_list_add(ret, filesA->files[ctrA].name);
|
2012-07-12 16:29:59 -04:00
|
|
|
ctrA++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-09 22:47:50 -04:00
|
|
|
static int _alpm_filelist_pathcmp(const char *p1, const char *p2)
|
|
|
|
{
|
|
|
|
while(*p1 && *p1 == *p2) {
|
|
|
|
p1++;
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip trailing '/' */
|
|
|
|
if(*p1 == '\0' && *p2 == '/') {
|
|
|
|
p2++;
|
|
|
|
} else if(*p2 == '\0' && *p1 == '/') {
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *p1 - *p2;
|
|
|
|
}
|
|
|
|
|
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;
|
2014-07-09 22:47:50 -04:00
|
|
|
alpm_file_t *arrA = filesA->files, *arrB = filesB->files;
|
2012-07-17 22:09:24 -04:00
|
|
|
|
|
|
|
while(ctrA < filesA->count && ctrB < filesB->count) {
|
2014-07-09 22:47:50 -04:00
|
|
|
const char *strA = arrA[ctrA].name, *strB = arrB[ctrB].name;
|
|
|
|
int cmp = _alpm_filelist_pathcmp(strA, strB);
|
2012-07-17 22:25:37 -04:00
|
|
|
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
|
|
|
/* when not directories, item in both qualifies as an intersect */
|
2014-07-09 22:47:50 -04:00
|
|
|
if(strA[strlen(strA) - 1] != '/' || strB[strlen(strB) - 1] != '/') {
|
|
|
|
ret = alpm_list_add(ret, arrA[ctrA].name);
|
2012-07-17 22:09:24 -04:00
|
|
|
}
|
2012-07-17 22:25:37 -04:00
|
|
|
ctrA++;
|
|
|
|
ctrB++;
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:37:56 -04:00
|
|
|
alpm_file_t SYMEXPORT *alpm_filelist_contains(alpm_filelist_t *filelist,
|
2012-07-12 16:29:59 -04:00
|
|
|
const char *path)
|
|
|
|
{
|
2013-04-12 20:37:56 -04:00
|
|
|
alpm_file_t key;
|
2012-07-12 16:29:59 -04:00
|
|
|
|
|
|
|
if(!filelist) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
key.name = (char *)path;
|
|
|
|
|
2013-04-12 20:37:56 -04:00
|
|
|
return bsearch(&key, filelist->files, filelist->count,
|
2012-07-12 16:29:59 -04:00
|
|
|
sizeof(alpm_file_t), _alpm_files_cmp);
|
|
|
|
}
|
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
/* vim: set noet: */
|