From: Leonardo Taccari Date: Thu, 11 Oct 2018 20:37:39 +0000 (+0200) Subject: Use stat(2) to retrieve the file permissions mode X-Git-Url: https://git.owens.tech/assets/static/git.owens.tech/assets/static/git.owens.tech/git?a=commitdiff_plain;h=66858a8ec85641bd42f47298304e2bb8b5ee5051;p=vimb.git Use stat(2) to retrieve the file permissions mode Remove the mode arguments from util_file_prepend_line(), util_file_pop_line() and util_file_set_content(). Both util_file_prepend_line() and util_file_pop_line() just calls util_file_set_content() so stat(2) can be used there and if it fails the 0600 is used as a fallback. Thanks to @fanglingsu for reviews and suggestions! --- diff --git a/src/bookmark.c b/src/bookmark.c index 4cc6bd7..72c76a8 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -90,7 +90,7 @@ gboolean bookmark_remove(const char *uri) g_string_append_printf(new, "%s\n", line); } g_strfreev(lines); - util_file_set_content(vb.files[FILES_BOOKMARK], new->str, 0600); + util_file_set_content(vb.files[FILES_BOOKMARK], new->str); g_string_free(new, TRUE); } @@ -217,7 +217,7 @@ gboolean bookmark_queue_unshift(const char *uri) */ char *bookmark_queue_pop(int *item_count) { - return util_file_pop_line(vb.files[FILES_QUEUE], item_count, 0600); + return util_file_pop_line(vb.files[FILES_QUEUE], item_count); } /** diff --git a/src/main.c b/src/main.c index b6c3b5c..58b5f24 100644 --- a/src/main.c +++ b/src/main.c @@ -646,7 +646,7 @@ static void client_destroy(Client *c) * exists. */ if (c->state.uri && vb.config.closed_max && vb.files[FILES_CLOSED]) { util_file_prepend_line(vb.files[FILES_CLOSED], c->state.uri, - vb.config.closed_max, 0600); + vb.config.closed_max); } gtk_widget_destroy(c->window); diff --git a/src/normal.c b/src/normal.c index ca402ae..a2028a7 100644 --- a/src/normal.c +++ b/src/normal.c @@ -603,7 +603,7 @@ static VbResult normal_open(Client *c, const NormalCmdInfo *info) } a.i = info->key == 'U' ? TARGET_NEW : TARGET_CURRENT; - a.s = util_file_pop_line(vb.files[FILES_CLOSED], NULL, 0600); + a.s = util_file_pop_line(vb.files[FILES_CLOSED], NULL); if (!a.s) { return RESULT_ERROR; } diff --git a/src/util.c b/src/util.c index a3f4775..1d07b0b 100644 --- a/src/util.c +++ b/src/util.c @@ -254,11 +254,9 @@ gboolean util_file_prepend(const char *file, const char *format, ...) * @line: Line to be written as new first line into the file. * The line ending is inserted automatic. * @max_lines Maximum number of lines in file after the operation. - * @mode Mode (file permission as chmod(2)) used for the file when - * creating it. */ void util_file_prepend_line(const char *file, const char *line, - unsigned int max_lines, int mode) + unsigned int max_lines) { char **lines; GString *new_content; @@ -279,7 +277,7 @@ void util_file_prepend_line(const char *file, const char *line, } g_strfreev(lines); } - util_file_set_content(file, new_content->str, mode); + util_file_set_content(file, new_content->str); g_string_free(new_content, TRUE); } @@ -289,12 +287,10 @@ void util_file_prepend_line(const char *file, const char *line, * @file: file to read from * @item_count: will be filled with the number of remaining lines in file if it * is not NULL. - * @mode Mode (file permission as chmod(2)) used for the file when - * creating it. * * Returned string must be freed with g_free. */ -char *util_file_pop_line(const char *file, int *item_count, int mode) +char *util_file_pop_line(const char *file, int *item_count) { char **lines; char *new, @@ -314,7 +310,7 @@ char *util_file_pop_line(const char *file, int *item_count, int mode) /* minus one for last empty item and one for popped item */ count = len - 2; new = g_strjoinv("\n", lines + 1); - util_file_set_content(file, new, mode); + util_file_set_content(file, new); g_free(new); } g_strfreev(lines); @@ -360,12 +356,17 @@ char *util_get_file_contents(const char *filename, gsize *length) * Atomicly writes contents to given file. * Returns TRUE on success, FALSE otherwise. */ -char *util_file_set_content(const char *file, const char *contents, int mode) +char *util_file_set_content(const char *file, const char *contents) { gboolean retval = FALSE; char *tmp_name; - int fd; + int fd, mode; gsize length; + struct stat st; + + mode = 0600; + if (stat(file, &st) == 0) + mode = st.st_mode; /* Create a temporary file. */ tmp_name = g_strconcat(file, ".XXXXXX", NULL); diff --git a/src/util.h b/src/util.h index 816ed5f..bccb2a3 100644 --- a/src/util.h +++ b/src/util.h @@ -38,11 +38,11 @@ char *util_expand(State state, const char *src, int expflags); gboolean util_file_append(const char *file, const char *format, ...); gboolean util_file_prepend(const char *file, const char *format, ...); void util_file_prepend_line(const char *file, const char *line, - unsigned int max_lines, int mode); -char *util_file_pop_line(const char *file, int *item_count, int mode); + unsigned int max_lines); +char *util_file_pop_line(const char *file, int *item_count); char *util_get_config_dir(void); char *util_get_file_contents(const char *filename, gsize *length); -char *util_file_set_content(const char *file, const char *contents, int mode); +char *util_file_set_content(const char *file, const char *contents); char *util_get_filepath(const char *dir, const char *filename, gboolean create, int mode); char **util_get_lines(const char *filename); GList *util_file_to_unique_list(const char *filename, Util_Content_Func func,