Skip to content

Commit

Permalink
Fix pkg backup with the override of file descriptor for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Jan 24, 2020
1 parent 3ca2543 commit ad0476f
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions libpkg/backup.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2011-2014 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2020 Baptiste Daroussin <[email protected]>
* Copyright (c) 2012 Matthew Seaman <[email protected]>
* All rights reserved.
*
Expand All @@ -26,6 +26,7 @@
*/

#include <assert.h>
#include <fcntl.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
Expand Down Expand Up @@ -120,19 +121,17 @@ pkgdb_dump(struct pkgdb *db, const char *dest)
{
sqlite3 *backup;
int ret;
int destdbfd;
int savedfd;

if (eaccess(dest, W_OK)) {
if (errno != ENOENT) {
pkg_fatal_errno("Unable to access '%s'", dest);
}

/* Could we create the Sqlite DB file? */
if (eaccess(bsd_dirname(dest), W_OK)) {
pkg_fatal_errno("Unable to access '%s'",
bsd_dirname(dest));
}
destdbfd = open(bsd_dirname(dest), O_DIRECTORY|O_CLOEXEC);
if (destdbfd == -1) {
pkg_fatal_errno("Unable to access '%s'",
bsd_dirname(dest));
}

savedfd = pkg_get_dbdirfd();
ctx.pkg_dbdirfd = destdbfd;
ret = sqlite3_open(dest, &backup);

if (ret != SQLITE_OK) {
Expand All @@ -145,6 +144,8 @@ pkgdb_dump(struct pkgdb *db, const char *dest)
ret = copy_database(db->sqlite, backup);

sqlite3_close(backup);
ctx.pkg_dbdirfd = savedfd;
close(savedfd);

return (ret == SQLITE_OK? EPKG_OK : EPKG_FATAL);
}
Expand Down

0 comments on commit ad0476f

Please sign in to comment.