aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/history.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-09-26 02:19:51 -0700
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-09-26 02:19:51 -0700
commit871a2088dbfe89f8d2dabfd07b6cb42f88a042c6 (patch)
tree831a11ab979f5ca5bcf2d0e3fba38cfc467de5e7 /src/history.cpp
parentabeaac66328f83d8962d6a5a4b9ce8712edbbaa5 (diff)
Preserve existing file permissions when rewriting a history file
Fixes #2335
Diffstat (limited to 'src/history.cpp')
-rw-r--r--src/history.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/history.cpp b/src/history.cpp
index c526eb39..5d9528b2 100644
--- a/src/history.cpp
+++ b/src/history.cpp
@@ -1443,6 +1443,20 @@ bool history_t::save_internal_via_rewrite()
else
{
wcstring new_name = history_filename(name, wcstring());
+
+ /* Ensure we maintain the ownership and permissions of the original (#2355).
+ * If the stat fails, we assume (hope) our default permissions are correct. This
+ * corresponds to e.g. someone running sudo -E as the very first command. If they
+ * did, it would be tricky to set the permissions correctly. (bash doesn't get this
+ * case right either). */
+ struct stat sbuf;
+ if (wstat(new_name, &sbuf) >= 0)
+ {
+ /* Success */
+ fchown(out_fd, sbuf.st_uid, sbuf.st_gid);
+ fchmod(out_fd, sbuf.st_mode);
+ }
+
if (0 > wrename(tmp_name, new_name))
{
debug(2, L"Error when renaming history file");