Adding parsePath and making parseConfigFilePath use it.
git-svn-id: https://svn.musicpd.org/mpd/trunk@6622 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
parent
b496239e76
commit
c734c13b5c
53
src/conf.c
53
src/conf.c
|
@ -397,55 +397,12 @@ ConfigParam *parseConfigFilePath(char *name, int force)
|
|||
if (!param)
|
||||
return NULL;
|
||||
|
||||
path = param->value;
|
||||
path = parsePath(param->value);
|
||||
if (!path)
|
||||
FATAL("error parsing \"%s\" at line %i\n", name, param->line);
|
||||
|
||||
if (path[0] != '/' && path[0] != '~') {
|
||||
FATAL("\"%s\" is not an absolute path at line %i\n",
|
||||
param->value, param->line);
|
||||
}
|
||||
/* Parse ~ in path */
|
||||
else if (path[0] == '~') {
|
||||
struct passwd *pwd = NULL;
|
||||
char *newPath;
|
||||
int pos = 1;
|
||||
if (path[1] == '/' || path[1] == '\0') {
|
||||
ConfigParam *userParam = getConfigParam(CONF_USER);
|
||||
|
||||
if (userParam) {
|
||||
pwd = getpwnam(userParam->value);
|
||||
if (!pwd) {
|
||||
FATAL("no such user %s at line %i\n",
|
||||
userParam->value,
|
||||
userParam->line);
|
||||
}
|
||||
} else {
|
||||
uid_t uid = geteuid();
|
||||
if ((pwd = getpwuid(uid)) == NULL) {
|
||||
FATAL("problems getting passwd entry "
|
||||
"for current user\n");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int foundSlash = 0;
|
||||
char *ch = path + 1;
|
||||
for (; *ch != '\0' && *ch != '/'; ch++) ;
|
||||
if (*ch == '/')
|
||||
foundSlash = 1;
|
||||
*ch = '\0';
|
||||
pos += ch - path - 1;
|
||||
if ((pwd = getpwnam(path + 1)) == NULL) {
|
||||
FATAL("user \"%s\" not found at line %i\n",
|
||||
path + 1, param->line);
|
||||
}
|
||||
if (foundSlash)
|
||||
*ch = '/';
|
||||
}
|
||||
newPath = xmalloc(strlen(pwd->pw_dir) + strlen(path + pos) + 1);
|
||||
strcpy(newPath, pwd->pw_dir);
|
||||
strcat(newPath, path + pos);
|
||||
free(param->value);
|
||||
param->value = newPath;
|
||||
}
|
||||
free(param->value);
|
||||
param->value = path;
|
||||
|
||||
return param;
|
||||
}
|
||||
|
|
58
src/utils.c
58
src/utils.c
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
#include "conf.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -28,6 +29,7 @@
|
|||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <pwd.h>
|
||||
|
||||
char *myFgets(char *buffer, int bufferSize, FILE * fp)
|
||||
{
|
||||
|
@ -158,4 +160,60 @@ mpd_malloc void *xcalloc(size_t nmemb, size_t size)
|
|||
return ret;
|
||||
}
|
||||
|
||||
char *parsePath(char *path)
|
||||
{
|
||||
ConfigParam *param;
|
||||
struct passwd *passwd;
|
||||
char *newPath;
|
||||
char *c;
|
||||
int foundSlash = 0;
|
||||
int pos = 1;
|
||||
|
||||
if (path[0] != '/' && path[0] != '~') {
|
||||
ERROR("\"%s\" is not an absolute path\n", path);
|
||||
return NULL;
|
||||
} else if (path[0] == '~') {
|
||||
if (path[1] == '/' || path[1] == '\0') {
|
||||
param = getConfigParam(CONF_USER);
|
||||
if (param && param->value) {
|
||||
passwd = getpwnam(param->value);
|
||||
if (!passwd) {
|
||||
ERROR("no such user %s\n",
|
||||
param->value);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
passwd = getpwuid(geteuid());
|
||||
if (!passwd) {
|
||||
ERROR("problems getting passwd entry "
|
||||
"for current user\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (c = path + 1; *c != '\0' && *c != '/'; c++);
|
||||
if (*c == '/') {
|
||||
foundSlash = 1;
|
||||
*c = '\0';
|
||||
}
|
||||
pos = c - path;
|
||||
|
||||
passwd = getpwnam(path + 1);
|
||||
if (!passwd) {
|
||||
ERROR("user \"%s\" not found\n", path + 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (foundSlash)
|
||||
*c = '/';
|
||||
}
|
||||
|
||||
newPath = xmalloc(strlen(passwd->pw_dir) + strlen(path + pos) + 1);
|
||||
strcpy(newPath, passwd->pw_dir);
|
||||
strcat(newPath, path + pos);
|
||||
} else {
|
||||
newPath = xstrdup(path);
|
||||
}
|
||||
|
||||
return newPath;
|
||||
}
|
||||
|
|
|
@ -82,4 +82,6 @@ mpd_malloc void *xrealloc(void *ptr, size_t size);
|
|||
|
||||
mpd_malloc void *xcalloc(size_t nmemb, size_t size);
|
||||
|
||||
char *parsePath(char *path);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue