Merge branch 'morganamilo/fix-download' into 'master'

Fix non root downloads and improve error handling/logging

See merge request pacman/pacman!278
This commit is contained in:
Morgan Adamiec 2025-06-21 19:20:22 +00:00
commit 7ada823ea9
7 changed files with 38 additions and 12 deletions

View file

@ -308,6 +308,8 @@ typedef enum _alpm_errno_t {
/** Files conflict */ /** Files conflict */
ALPM_ERR_FILE_CONFLICTS, ALPM_ERR_FILE_CONFLICTS,
/* Misc */ /* Misc */
/** Download setup failed */
ALPM_ERR_RETRIEVE_PREPARE,
/** Download failed */ /** Download failed */
ALPM_ERR_RETRIEVE, ALPM_ERR_RETRIEVE,
/** Invalid Regex */ /** Invalid Regex */

View file

@ -153,7 +153,7 @@ int SYMEXPORT alpm_db_update(alpm_handle_t *handle, alpm_list_t *dbs, int force)
syncpath = get_sync_dir(handle); syncpath = get_sync_dir(handle);
ASSERT(syncpath != NULL, return -1); ASSERT(syncpath != NULL, return -1);
temporary_syncpath = _alpm_temporary_download_dir_setup(syncpath, handle->sandboxuser); temporary_syncpath = _alpm_temporary_download_dir_setup(handle, syncpath);
ASSERT(temporary_syncpath != NULL, FREE(syncpath); return -1); ASSERT(temporary_syncpath != NULL, FREE(syncpath); return -1);
/* make sure we have a sane umask */ /* make sure we have a sane umask */

View file

@ -1329,7 +1329,7 @@ int SYMEXPORT alpm_fetch_pkgurl(alpm_handle_t *handle, const alpm_list_t *urls,
/* find a valid cache dir to download to */ /* find a valid cache dir to download to */
cachedir = _alpm_filecache_setup(handle); cachedir = _alpm_filecache_setup(handle);
temporary_cachedir = _alpm_temporary_download_dir_setup(cachedir, handle->sandboxuser); temporary_cachedir = _alpm_temporary_download_dir_setup(handle, cachedir);
ASSERT(temporary_cachedir != NULL, return -1); ASSERT(temporary_cachedir != NULL, return -1);
for(i = urls; i; i = i->next) { for(i = urls; i; i = i->next) {

View file

@ -138,6 +138,8 @@ const char SYMEXPORT *alpm_strerror(alpm_errno_t err)
case ALPM_ERR_FILE_CONFLICTS: case ALPM_ERR_FILE_CONFLICTS:
return _("conflicting files"); return _("conflicting files");
/* Miscellaneous */ /* Miscellaneous */
case ALPM_ERR_RETRIEVE_PREPARE:
return _("failed to initialize download");
case ALPM_ERR_RETRIEVE: case ALPM_ERR_RETRIEVE:
return _("failed to retrieve some files"); return _("failed to retrieve some files");
case ALPM_ERR_INVALID_REGEX: case ALPM_ERR_INVALID_REGEX:

View file

@ -780,7 +780,7 @@ static int download_files(alpm_handle_t *handle)
alpm_list_t *payloads = NULL; alpm_list_t *payloads = NULL;
cachedir = _alpm_filecache_setup(handle); cachedir = _alpm_filecache_setup(handle);
temporary_cachedir = _alpm_temporary_download_dir_setup(cachedir, handle->sandboxuser); temporary_cachedir = _alpm_temporary_download_dir_setup(handle, cachedir);
if(temporary_cachedir == NULL) { if(temporary_cachedir == NULL) {
ret = -1; ret = -1;
goto finish; goto finish;

View file

@ -23,6 +23,7 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
#include <dirent.h> #include <dirent.h>
@ -949,32 +950,53 @@ const char *_alpm_filecache_setup(alpm_handle_t *handle)
/** Create a temporary directory under the supplied directory. /** Create a temporary directory under the supplied directory.
* The new directory is writable by the download user, and will be * The new directory is writable by the download user, and will be
* removed after the download operation has completed. * removed after the download operation has completed.
* @param handle an alpm handle
* @param dir existing sync or cache directory * @param dir existing sync or cache directory
* @param user download user name
* @return pointer to a sub-directory writable by the download user inside the existing directory. * @return pointer to a sub-directory writable by the download user inside the existing directory.
*/ */
char *_alpm_temporary_download_dir_setup(const char *dir, const char *user) char *_alpm_temporary_download_dir_setup(alpm_handle_t *handle, const char *dir)
{ {
const char *user = handle->sandboxuser;
uid_t myuid = getuid();
struct passwd const *pw = NULL; struct passwd const *pw = NULL;
ASSERT(dir != NULL, return NULL); ASSERT(dir != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
if(user != NULL) { if(myuid == 0 && user != NULL) {
ASSERT((pw = getpwnam(user)) != NULL, return NULL); errno = 0;
pw = getpwnam(user);
if(pw == NULL) {
if(errno == 0) {
_alpm_log(handle, ALPM_LOG_ERROR,
_("download user '%s' does not exist\n"), user);
} else {
_alpm_log(handle, ALPM_LOG_ERROR,
_("failed to get download user '%s': %s\n"),
user, strerror(errno));
}
RET_ERR(handle, ALPM_ERR_RETRIEVE_PREPARE, NULL);
}
} }
const char template[] = "download-XXXXXX"; const char template[] = "download-XXXXXX";
size_t newdirlen = strlen(dir) + sizeof(template) + 1; size_t newdirlen = strlen(dir) + sizeof(template) + 1;
char *newdir = NULL; char *newdir = NULL;
MALLOC(newdir, newdirlen, return NULL); MALLOC(newdir, newdirlen, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
snprintf(newdir, newdirlen - 1, "%s%s", dir, template); snprintf(newdir, newdirlen - 1, "%s%s", dir, template);
if(mkdtemp(newdir) == NULL) { if(mkdtemp(newdir) == NULL) {
_alpm_log(handle, ALPM_LOG_ERROR,
_("failed to create temporary download directory %s: %s\n"),
newdir, strerror(errno));
free(newdir); free(newdir);
return NULL; RET_ERR(handle, ALPM_ERR_RETRIEVE_PREPARE, NULL);
} }
if(pw != NULL) { if(pw != NULL) {
if(chown(newdir, pw->pw_uid, pw->pw_gid) == -1) { if(chown(newdir, pw->pw_uid, pw->pw_gid) == -1) {
_alpm_log(handle, ALPM_LOG_ERROR,
_("failed to chown temporary download directory %s: %s\n"),
newdir, strerror(errno));
free(newdir); free(newdir);
return NULL; RET_ERR(handle, ALPM_ERR_RETRIEVE_PREPARE, NULL);
} }
} }
newdir[newdirlen-2] = '/'; newdir[newdirlen-2] = '/';

View file

@ -139,7 +139,7 @@ char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename);
/* Checks whether a file exists in cache */ /* Checks whether a file exists in cache */
int _alpm_filecache_exists(alpm_handle_t *handle, const char *filename); int _alpm_filecache_exists(alpm_handle_t *handle, const char *filename);
const char *_alpm_filecache_setup(alpm_handle_t *handle); const char *_alpm_filecache_setup(alpm_handle_t *handle);
char *_alpm_temporary_download_dir_setup(const char *dir, const char *user); char *_alpm_temporary_download_dir_setup(alpm_handle_t *handle, const char *dir);
void _alpm_remove_temporary_download_dir(const char *dir); void _alpm_remove_temporary_download_dir(const char *dir);
/* Unlike many uses of alpm_pkgvalidation_t, _alpm_test_checksum expects /* Unlike many uses of alpm_pkgvalidation_t, _alpm_test_checksum expects