You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by he...@apache.org on 2018/09/27 08:04:22 UTC

svn commit: r1842074 - in /spamassassin: branches/3.4/lib/Mail/SpamAssassin/Util.pm trunk/lib/Mail/SpamAssassin/Util.pm

Author: hege
Date: Thu Sep 27 08:04:21 2018
New Revision: 1842074

URL: http://svn.apache.org/viewvc?rev=1842074&view=rev
Log:
Add touch_file() to utils

Modified:
    spamassassin/branches/3.4/lib/Mail/SpamAssassin/Util.pm
    spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm

Modified: spamassassin/branches/3.4/lib/Mail/SpamAssassin/Util.pm
URL: http://svn.apache.org/viewvc/spamassassin/branches/3.4/lib/Mail/SpamAssassin/Util.pm?rev=1842074&r1=1842073&r2=1842074&view=diff
==============================================================================
--- spamassassin/branches/3.4/lib/Mail/SpamAssassin/Util.pm (original)
+++ spamassassin/branches/3.4/lib/Mail/SpamAssassin/Util.pm Thu Sep 27 08:04:21 2018
@@ -55,7 +55,7 @@ our @ISA = qw(Exporter);
 our @EXPORT = ();
 our @EXPORT_OK = qw(&local_tz &base64_decode &untaint_var &untaint_file_path
                   &exit_status_str &proc_status_ok &am_running_on_windows
-                  &reverse_ip_address &decode_dns_question_entry
+                  &reverse_ip_address &decode_dns_question_entry &touch_file
                   &get_my_locales &parse_rfc822_date &get_user_groups
                   &secure_tmpfile &secure_tmpdir &uri_list_canonicalize);
 
@@ -1105,6 +1105,45 @@ sub first_available_module {
 }
 
 ###########################################################################
+
+=item touch_file(file, { args });
+
+Touch or create a file.
+
+Possible args:
+
+create_exclusive => 1
+  Create a new empty file safely, only if not existing before
+
+=cut
+
+sub touch_file {
+  my ($file, $args) = @_;
+
+  $file = untaint_file_path($file);
+  $args ||= {};
+
+  return unless defined $file && $file ne '';
+
+  if ($args->{create_exclusive}) {
+    if (sysopen(my $fh, $file, O_CREAT|O_EXCL)) {
+      close $fh;
+      return 1;
+    }
+    return 1 if $! == EEXIST; # fine if it exists already
+    dbg("util: exclusive touch_file failed: $file: $!");
+    return 0;
+  }
+
+  if (!utime(undef,undef,$file)) {
+    dbg("util: touch_file failed: $file: $!");
+    return 0;
+  }
+
+  return 1;
+}
+
+###########################################################################
 
 =item my ($filepath, $filehandle) = secure_tmpfile();
 

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm?rev=1842074&r1=1842073&r2=1842074&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm Thu Sep 27 08:04:21 2018
@@ -56,7 +56,7 @@ our @EXPORT = ();
 our @EXPORT_OK = qw(&local_tz &base64_decode &base64_encode
                   &untaint_var &untaint_file_path
                   &exit_status_str &proc_status_ok &am_running_on_windows
-                  &reverse_ip_address &decode_dns_question_entry
+                  &reverse_ip_address &decode_dns_question_entry &touch_file
                   &secure_tmpfile &secure_tmpdir &uri_list_canonicalize
                   &get_my_locales &parse_rfc822_date &idn_to_ascii
                   &is_valid_utf_8 &get_user_groups);
@@ -1227,6 +1227,45 @@ sub first_available_module {
 }
 
 ###########################################################################
+
+=item touch_file(file, { args });
+
+Touch or create a file.
+
+Possible args:
+
+create_exclusive => 1
+  Create a new empty file safely, only if not existing before
+
+=cut
+
+sub touch_file {
+  my ($file, $args) = @_;
+
+  $file = untaint_file_path($file);
+  $args ||= {};
+
+  return unless defined $file && $file ne '';
+
+  if ($args->{create_exclusive}) {
+    if (sysopen(my $fh, $file, O_CREAT|O_EXCL)) {
+      close $fh;
+      return 1;
+    }
+    return 1 if $! == EEXIST; # fine if it exists already
+    dbg("util: exclusive touch_file failed: $file: $!");
+    return 0;
+  }
+
+  if (!utime(undef,undef,$file)) {
+    dbg("util: touch_file failed: $file: $!");
+    return 0;
+  }
+
+  return 1;
+}
+
+###########################################################################
 
 =item my ($filepath, $filehandle) = secure_tmpfile();