move the actual doing stuff code for FS lib into a Guts.pm
[scpubgit/DKit.git] / lib / DX / Lib / FS / Action / RewriteFile.pm
index 64bd66b..c636e35 100644 (file)
@@ -1,9 +1,7 @@
 package DX::Lib::FS::Action::RewriteFile;
 
 use DX::Lib::FS::Fact::FileContent;
-use File::stat;
-use File::Copy;
-use Fcntl qw(O_CREAT O_WRONLY);
+use DX::Lib::FS::Guts;
 use Moo;
 
 with 'DX::Role::Action';
@@ -42,17 +40,8 @@ sub expected_effect {
 
 sub _do_run {
   my ($self) = @_;
-  my $stat = stat($self->path) or die "Couldn't stat ${\$self->path}: $!";
-  my $perms = $stat->mode & 07777;
-  my $new = $self->path.'.new';
-  unlink($new);
-  sysopen my $fh, $new, O_CREAT | O_WRONLY, $perms
-    or die "Couldn't open ${new}: $!";
-  print $fh $self->final_content
-    or die "Couldn't write data to ${new}: $!";
-  close $fh;
-  move($new, $self->path) or die "Couldn't install ${new}: $!";
-  return +(file_content => $self->from);
+  DX::Lib::FS::Guts->rewrite_file($self->path, $self->final_content);
+  return +(file_content => $self->path);
 }
 
 1;