Preserve existing file permissions when rewriting a history file

Fixes #2335
This commit is contained in:
ridiculousfish 2015-09-26 02:19:51 -07:00
parent abeaac6632
commit 871a2088db

View file

@ -1443,6 +1443,20 @@ bool history_t::save_internal_via_rewrite()
else else
{ {
wcstring new_name = history_filename(name, wcstring()); 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)) if (0 > wrename(tmp_name, new_name))
{ {
debug(2, L"Error when renaming history file"); debug(2, L"Error when renaming history file");