709
717
if (in_place_extension)
711
int input_fd, output_fd;
712
720
char *tmpdir, *p;
722
security_context_t old_fscreatecon;
723
int reset_fscreatecon = 0;
724
memset (&old_fscreatecon, 0, sizeof (old_fscreatecon));
715
727
if (follow_symlinks)
716
728
input->in_file_name = follow_symlink (name);
728
740
panic(_("couldn't edit %s: is a terminal"), input->in_file_name);
730
742
input_fd = fileno (input->fp);
731
fstat (input_fd, &st);
732
if (!S_ISREG (st.st_mode))
743
fstat (input_fd, &input->st);
744
if (!S_ISREG (input->st.st_mode))
733
745
panic(_("couldn't edit %s: not a regular file"), input->in_file_name);
748
if (is_selinux_enabled ())
750
security_context_t con;
751
if (getfilecon (input->in_file_name, &con) != -1)
753
/* Save and restore the old context for the sake of w and W
755
reset_fscreatecon = getfscreatecon (&old_fscreatecon) >= 0;
756
if (setfscreatecon (con) < 0)
757
fprintf (stderr, _("%s: warning: failed to set default file creation context to %s: %s"),
758
myname, con, strerror (errno));
764
fprintf (stderr, _("%s: warning: failed to get security context of %s: %s"),
765
myname, input->in_file_name, strerror (errno));
735
770
output_file.fp = ck_mkstemp (&input->out_file_name, tmpdir, "sed");
736
771
output_file.missing_newline = false;
775
if (reset_fscreatecon)
777
setfscreatecon (old_fscreatecon);
778
freecon (old_fscreatecon);
739
782
if (!output_file.fp)
740
783
panic(_("couldn't open temporary file %s: %s"), input->out_file_name, strerror(errno));
742
output_fd = fileno (output_file.fp);
744
if (fchown (output_fd, st.st_uid, st.st_gid) == -1)
745
fchown (output_fd, -1, st.st_gid);
747
copy_acl (input->in_file_name, input_fd,
748
input->out_file_name, output_fd,
752
786
output_file.fp = stdout;
766
800
if (in_place_extension && output_file.fp != NULL)
768
802
const char *target_name;
803
int input_fd, output_fd;
805
target_name = input->in_file_name;
806
input_fd = fileno (input->fp);
807
output_fd = fileno (output_file.fp);
808
copy_acl (input->in_file_name, input_fd,
809
input->out_file_name, output_fd,
812
if (fchown (output_fd, input->st.st_uid, input->st.st_gid) == -1)
813
fchown (output_fd, -1, input->st.st_gid);
816
ck_fclose (input->fp);
769
817
ck_fclose (output_file.fp);
771
target_name = input->in_file_name;
772
818
if (strcmp(in_place_extension, "*") != 0)
774
820
char *backup_file_name = get_backup_file_name(target_name);