Skip to content

Commit 2900717

Browse files
committed
lib: fix directory prefix matching in files check
1 parent 3ca652b commit 2900717

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/transaction_files.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,25 @@ match_preserved_file(struct xbps_handle *xhp, const char *file)
142142
}
143143

144144
static bool
145-
can_delete_directory(const char *file, size_t len, size_t max)
145+
has_dir_prefix(const char *dir, size_t dirlen, const char *file)
146+
{
147+
return strncmp(file, dir, dirlen) == 0 && file[dirlen] == '/';
148+
}
149+
150+
static bool
151+
can_delete_directory(const char *dir, size_t dirlen, size_t max)
146152
{
147153
struct item *item;
148154
size_t rmcount = 0, fcount = 0;
149155
DIR *dp;
150156

151-
dp = opendir(file);
157+
dp = opendir(dir);
152158
if (dp == NULL) {
153159
if (errno == ENOENT) {
154160
return true;
155161
} else {
156162
xbps_dbg_printf("[files] %s: %s: %s\n",
157-
__func__, file, strerror(errno));
163+
__func__, dir, strerror(errno));
158164
return false;
159165
}
160166
}
@@ -166,7 +172,7 @@ can_delete_directory(const char *file, size_t len, size_t max)
166172
*/
167173
for (size_t i = 0; i < max; i++) {
168174
item = items[i];
169-
if (strncmp(item->file, file, len) == 0) {
175+
if (has_dir_prefix(dir, dirlen, item->file)) {
170176
if (!item->deleted) {
171177
closedir(dp);
172178
return false;
@@ -187,7 +193,7 @@ can_delete_directory(const char *file, size_t len, size_t max)
187193

188194
if (fcount <= rmcount) {
189195
xbps_dbg_printf("[files] only removed %zu out of %zu files: %s\n",
190-
rmcount, fcount, file);
196+
rmcount, fcount, dir);
191197
}
192198
closedir(dp);
193199

tests/xbps/libxbps/shell/obsoletefiles_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ directory_to_symlink_head() {
344344

345345
directory_to_symlink_body() {
346346
mkdir -p some_repo pkg_A/foo
347-
touch pkg_A/foo/bar
347+
touch pkg_A/foo/bar pkg_A/foobar
348348
# create package and install it
349349
cd some_repo
350350
xbps-create -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A

0 commit comments

Comments
 (0)