Avoid double slash when explicitly passing --root or --rootdir

Passing a path with a trailing slash to --root or --rootdir can lead to a
double slash at the start of paths.  e.g.

$ pacman --root / -v 2>1  | grep " //"
Log File  : //var/log/pacman.log

In MSYS2, paths starting with // will hit the network and fail.

Avoid this be explicitly stripping the trailing / from paths passed to these
flags.

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Finlay Maroney 2023-03-10 19:33:20 -05:00 committed by Allan McRae
parent 76b140c72a
commit 34611a6643

View file

@ -1136,15 +1136,22 @@ int setdefaults(config_t *c)
#define SETDEFAULT(opt, val) if(!opt) { opt = val; if(!opt) { return -1; } }
if(c->rootdir) {
char* rootdir = strdup(c->rootdir);
int rootdir_len = strlen(rootdir);
/* This removes trailing slashes from the root directory */
if(rootdir[rootdir_len-1] == '/'){
rootdir[rootdir_len-1] = '\0';
}
char path[PATH_MAX];
if(!c->dbpath) {
snprintf(path, PATH_MAX, "%s/%s", c->rootdir, &DBPATH[1]);
snprintf(path, PATH_MAX, "%s/%s", rootdir, &DBPATH[1]);
SETDEFAULT(c->dbpath, strdup(path));
}
if(!c->logfile) {
snprintf(path, PATH_MAX, "%s/%s", c->rootdir, &LOGFILE[1]);
snprintf(path, PATH_MAX, "%s/%s", rootdir, &LOGFILE[1]);
SETDEFAULT(c->logfile, strdup(path));
}
free(rootdir);
} else {
SETDEFAULT(c->rootdir, strdup(ROOTDIR));
SETDEFAULT(c->dbpath, strdup(DBPATH));