Tighten up XDG base directory path handling
Don't search base directories if path starts with "/", "./" or "../", but still do if the path simply starts with ".". Bail early if HOME is needed but unset. Don't attempt to open the original path in configOpen and dataOpen.weechat-hashes
parent
1a2a1e59dd
commit
e89e6dda99
33
xdg.c
33
xdg.c
|
@ -71,7 +71,7 @@ basePath(struct Base base, const char **dirs, const char *path) {
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path[0] == '/' || path[0] == '.') {
|
if (path[strspn(path, ".")] == '/') {
|
||||||
*dirs = "";
|
*dirs = "";
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ basePath(struct Base base, const char **dirs, const char *path) {
|
||||||
home, base.defHome, path
|
home, base.defHome, path
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
errx(EX_CONFIG, "HOME unset");
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
@ -98,8 +98,7 @@ const char *configPath(const char **dirs, const char *path) {
|
||||||
return basePath(Config, dirs, path);
|
return basePath(Config, dirs, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *dataPath(const char **dirs, const char *path) {
|
||||||
dataPath(const char **dirs, const char *path) {
|
|
||||||
return basePath(Data, dirs, path);
|
return basePath(Data, dirs, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,17 +109,16 @@ FILE *configOpen(const char *path, const char *mode) {
|
||||||
if (file) return file;
|
if (file) return file;
|
||||||
if (errno != ENOENT) warn("%s", abs);
|
if (errno != ENOENT) warn("%s", abs);
|
||||||
}
|
}
|
||||||
FILE *file = fopen(path, mode);
|
dirs = NULL;
|
||||||
if (!file) warn("%s", path);
|
warn("%s", configPath(&dirs, path));
|
||||||
return file;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dataMkdir(const char *path) {
|
void dataMkdir(const char *path) {
|
||||||
const char *dirs = NULL;
|
const char *dirs = NULL;
|
||||||
const char *abs = dataPath(&dirs, path);
|
path = dataPath(&dirs, path);
|
||||||
if (!abs) return;
|
int error = mkdir(path, S_IRWXU);
|
||||||
int error = mkdir(abs, S_IRWXU);
|
if (error && errno != EEXIST) warn("%s", path);
|
||||||
if (error && errno != EEXIST) warn("%s", abs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *dataOpen(const char *path, const char *mode) {
|
FILE *dataOpen(const char *path, const char *mode) {
|
||||||
|
@ -130,20 +128,9 @@ FILE *dataOpen(const char *path, const char *mode) {
|
||||||
if (file) return file;
|
if (file) return file;
|
||||||
if (errno != ENOENT) warn("%s", abs);
|
if (errno != ENOENT) warn("%s", abs);
|
||||||
}
|
}
|
||||||
|
if (mode[0] != 'r') dataMkdir("");
|
||||||
if (mode[0] != 'r') {
|
|
||||||
dataMkdir("");
|
|
||||||
dirs = NULL;
|
dirs = NULL;
|
||||||
path = dataPath(&dirs, path);
|
path = dataPath(&dirs, path);
|
||||||
if (!path) {
|
|
||||||
warn("HOME unset");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
FILE *file = fopen(path, mode);
|
|
||||||
if (!file) warn("%s", path);
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE *file = fopen(path, mode);
|
FILE *file = fopen(path, mode);
|
||||||
if (!file) warn("%s", path);
|
if (!file) warn("%s", path);
|
||||||
return file;
|
return file;
|
||||||
|
|
Loading…
Reference in New Issue