Fix the way system files are handled at surf startup.

1. Do not chmod existing directories.
2. Fix the handling of tilde expansion in paths, don't expand ~foo to
   $HOME/foo but to foo's home directory.
3. Separate the creation of files and directories. We don't have to
   worry anymore about pathnames having to end with a '/' to be correctly
   handled.

Signed-off-by: Christoph Lohmann <20h@r-36.net>
This commit is contained in:
Quentin Rameau 2015-10-13 21:39:01 +02:00 committed by Christoph Lohmann
parent 1554354f16
commit 8a898ec4df
1 changed files with 56 additions and 27 deletions

87
surf.c
View File

@ -24,6 +24,8 @@
#include <libgen.h> #include <libgen.h>
#include <stdarg.h> #include <stdarg.h>
#include <regex.h> #include <regex.h>
#include <pwd.h>
#include <string.h>
#include "arg.h" #include "arg.h"
@ -113,6 +115,7 @@ static void addaccelgroup(Client *c);
static void beforerequest(WebKitWebView *w, WebKitWebFrame *f, static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
WebKitWebResource *r, WebKitNetworkRequest *req, WebKitWebResource *r, WebKitNetworkRequest *req,
WebKitNetworkResponse *resp, Client *c); WebKitNetworkResponse *resp, Client *c);
static char *buildfile(const char *path);
static char *buildpath(const char *path); static char *buildpath(const char *path);
static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c); static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
static void cleanup(void); static void cleanup(void);
@ -257,37 +260,63 @@ beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
} }
static char * static char *
buildpath(const char *path) { buildfile(const char *path) {
char *apath, *p; char *dname, *bname, *bpath, *fpath;
FILE *f; FILE *f;
/* creating directory */ dname = g_path_get_dirname(path);
if(path[0] == '/') { bname = g_path_get_basename(path);
apath = g_strdup(path);
} else if(path[0] == '~') {
if(path[1] == '/') {
apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
} else {
apath = g_strconcat(g_get_home_dir(), "/",
&path[1], NULL);
}
} else {
apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
}
if((p = strrchr(apath, '/'))) { bpath = buildpath(dname);
*p = '\0'; g_free(dname);
g_mkdir_with_parents(apath, 0700);
g_chmod(apath, 0700); /* in case it existed */ fpath = g_build_filename(bpath, bname, NULL);
*p = '/'; g_free(bname);
}
/* creating file (gives error when apath ends with "/") */
if((f = fopen(apath, "a"))) { if(!(f = fopen(fpath, "a")))
g_chmod(apath, 0600); /* always */ die("Could not open file: %s\n", fpath);
g_chmod(fpath, 0600); /* always */
fclose(f); fclose(f);
return fpath;
} }
return apath; static char *
buildpath(const char *path) {
struct passwd *pw;
char *apath, *name, *p, *fpath;
if(path[0] == '~') {
if(path[1] == '/' || path[1] == '\0') {
p = (char *)&path[1];
pw = getpwuid(getuid());
} else {
if((p = strchr(path, '/')))
name = g_strndup(&path[1], --p - path);
else
name = g_strdup(&path[1]);
if(!(pw = getpwnam(name))) {
die("Can't get user %s home directory: %s.\n",
name, path);
}
g_free(name);
}
apath = g_build_filename(pw->pw_dir, p, NULL);
} else {
apath = g_strdup(path);
}
/* creating directory */
if (g_mkdir_with_parents(apath, 0700) < 0)
die("Could not access directory: %s\n", apath);
fpath = realpath(apath, NULL);
g_free(apath);
return fpath;
} }
static gboolean static gboolean
@ -1221,8 +1250,8 @@ setup(void) {
atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False); atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
/* dirs and files */ /* dirs and files */
cookiefile = buildpath(cookiefile); cookiefile = buildfile(cookiefile);
scriptfile = buildpath(scriptfile); scriptfile = buildfile(scriptfile);
cachefolder = buildpath(cachefolder); cachefolder = buildpath(cachefolder);
styledir = buildpath(styledir); styledir = buildpath(styledir);
if(stylefile == NULL) { if(stylefile == NULL) {
@ -1234,12 +1263,12 @@ setup(void) {
styles[i].regex); styles[i].regex);
styles[i].regex = NULL; styles[i].regex = NULL;
} }
styles[i].style = buildpath( styles[i].style = buildfile(
g_strconcat(styledir, g_strconcat(styledir,
styles[i].style, NULL)); styles[i].style, NULL));
} }
} else { } else {
stylefile = buildpath(stylefile); stylefile = buildfile(stylefile);
} }
/* request handler */ /* request handler */