Browse code

preserve original access and modification times; optimise

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@1119 77e5149b-7576-45b1-b177-96237e5ba77b

Tomasz Kojm authored on 2004/11/24 07:27:24
Showing 2 changed files
... ...
@@ -1,7 +1,12 @@
1
+Tue Nov 23 23:23:45 CET 2004 (tk)
2
+---------------------------------
3
+  * clamscan: --move: preserve original access and modification times
4
+	      (requested by Tomasz Papszun); optimise
5
+
1 6
 Tue Nov 23 09:06:45 GMT 2004 (njh)
2 7
 ----------------------------------
3 8
   * libclamav/binhex.c:	Fix crash in base64 encoded binhex files reported by
4
-				Stefan Kaltenbrunner <stefan@kaltenbrunner.cc>
9
+				Stefan Kaltenbrunner <stefan*kaltenbrunner.cc>
5 10
 
6 11
 Mon Nov 22 15:20:07 GMT 2004 (njh)
7 12
 ----------------------------------
... ...
@@ -30,6 +30,7 @@
30 30
 #include <sys/stat.h>
31 31
 #include <sys/types.h>
32 32
 #include <sys/wait.h>
33
+#include <utime.h>
33 34
 #include <grp.h>
34 35
 #include <fcntl.h>
35 36
 #include <unistd.h>
... ...
@@ -962,59 +963,46 @@ int clamav_unpack(const char *prog, char **args, const char *tmpdir, const struc
962 962
 
963 963
 void move_infected(const char *filename, const struct optstruct *opt)
964 964
 {
965
-    char *movedir, *movefilename, *tmp, numext[4 + 1];
966
-    struct stat fstat, mfstat;
967
-    int n, len, movefilename_size;
965
+	char *movedir, *movefilename, *tmp, numext[4 + 1];
966
+	struct stat fstat, mfstat;
967
+	int n, len, movefilename_size;
968
+	struct utimbuf ubuf;
968 969
 
969 970
 
970 971
     if(!(movedir = getargl(opt, "move"))) {
971 972
         /* Should never reach here */
972
-        mprintf("@error moving file '%s'.\n", filename);
973
-        mprintf("clamscan: getargc() returned NULL.\n");
974
-        logg("clamscan: getargc() returned NULL.\n");
973
+        mprintf("@getargc() returned NULL\n", filename);
975 974
         claminfo.notmoved++;
976 975
         return;
977 976
     }
978 977
 
979 978
     if(access(movedir, W_OK|X_OK) == -1) {
980
-        mprintf("@error moving file '%s'.\n", filename);
981
-        mprintf("clamscan: cannot write to '%s': %s.\n", movedir, strerror(errno));
982
-        logg("clamscan: cannot write to '%s': %s.\n", movedir, strerror(errno));
979
+        mprintf("@error moving file '%s': cannot write to '%s': %s\n", filename, movedir, strerror(errno));
983 980
         claminfo.notmoved++;
984 981
         return;
985 982
     }
986 983
 
987
-    if(!(tmp = strrchr(filename, '/'))) {
988
-        mprintf("@error moving file '%s'.\n", filename);
989
-        mprintf("clamscan: '%s' does not appear to be a valid filename.\n", filename);
990
-        logg("clamscan: '%s' does not appear to be a valid filename.\n", filename);
991
-        claminfo.notmoved++;
992
-        return;
993
-    }
984
+    if(!(tmp = strrchr(filename, '/')))
985
+	tmp = (char *) filename;
994 986
 
995
-    movefilename_size = sizeof(char) * (strlen(movedir) + strlen(tmp) + sizeof(numext) + 1);
987
+    movefilename_size = sizeof(char) * (strlen(movedir) + strlen(tmp) + sizeof(numext) + 2);
996 988
 
997
-    if(!(movefilename = malloc(movefilename_size))) {
998
-        mprintf("@error moving file '%s'.\n", filename);
999
-        mprintf("clamscan: malloc() returned NULL.\n");
1000
-        logg("clamscan: malloc() returned NULL.\n");
1001
-        claminfo.notmoved++;
1002
-        return;
989
+    if(!(movefilename = mmalloc(movefilename_size))) {
990
+        mprintf("@Memory allocation error\n");
991
+	exit(71);
1003 992
     }
1004 993
 
1005 994
     if(!(strrcpy(movefilename, movedir))) {
1006
-        mprintf("@error moving file '%s'.\n", filename);
1007
-        mprintf("clamscan: strrcpy() returned NULL.\n");
1008
-        logg("clamscan: strrcpy() returned NULL.\n");
995
+        mprintf("@strrcpy() returned NULL\n");
1009 996
         claminfo.notmoved++;
1010 997
         free(movefilename);
1011 998
         return;
1012 999
     }
1013 1000
 
1001
+    strcat(movefilename, "/");
1002
+
1014 1003
     if(!(strcat(movefilename, tmp))) {
1015
-        mprintf("@error moving file '%s'.\n", filename);
1016
-        mprintf("clamscan: strcat() returned NULL.\n");
1017
-        logg("clamscan: strcat() returned NULL.\n");
1004
+        mprintf("@strcat() returned NULL\n");
1018 1005
         claminfo.notmoved++;
1019 1006
         free(movefilename);
1020 1007
         return;
... ...
@@ -1024,8 +1012,8 @@ void move_infected(const char *filename, const struct optstruct *opt)
1024 1024
 
1025 1025
     if(!stat(movefilename, &mfstat)) {
1026 1026
         if(fstat.st_ino == mfstat.st_ino) { /* It's the same file*/
1027
-            mprintf("clamscan: file excluded '%s'.\n", filename);
1028
-            logg("clamscan: file excluded '%s'.\n", filename);
1027
+            mprintf("File excluded '%s'\n", filename);
1028
+            logg("File excluded '%s'\n", filename);
1029 1029
             claminfo.notmoved++;
1030 1030
             free(movefilename);
1031 1031
             return;
... ...
@@ -1048,27 +1036,31 @@ void move_infected(const char *filename, const struct optstruct *opt)
1048 1048
        }
1049 1049
     }
1050 1050
 
1051
-    if(filecopy(filename, movefilename) == -1) {
1052
-        mprintf("@error moving file '%s'.\n", filename);
1053
-        mprintf("clamscan: cannot move '%s' to '%s': %s.\n", filename, movefilename, strerror(errno));
1054
-        logg("clamscan: cannot move '%s' to '%s': %s.\n", filename, movefilename, strerror(errno));
1055
-        claminfo.notmoved++;
1056
-        free(movefilename);
1057
-        return;
1058
-    }
1051
+    if(rename(filename, movefilename) == -1) {
1052
+	if(filecopy(filename, movefilename) == -1) {
1053
+	    mprintf("@cannot move '%s' to '%s': %s\n", filename, movefilename, strerror(errno));
1054
+	    claminfo.notmoved++;
1055
+	    free(movefilename);
1056
+	    return;
1057
+	}
1059 1058
 
1060
-    chmod(movefilename, fstat.st_mode);
1061
-    chown(movefilename, fstat.st_uid, fstat.st_gid);
1059
+	chmod(movefilename, fstat.st_mode);
1060
+	chown(movefilename, fstat.st_uid, fstat.st_gid);
1062 1061
 
1063
-    if(unlink(filename)) {
1064
-        mprintf("@error moving file '%s'.\n", filename);
1065
-        mprintf("clamscan: cannot unlink '%s': %s.\n", filename, strerror(errno));
1066
-        logg("clamscan: cannot unlink '%s': %s.\n", filename, strerror(errno));
1067
-        claminfo.notremoved++;            
1068
-    } else {
1069
-        mprintf("%s: moved to '%s'.\n", filename, movefilename);
1070
-        logg("%s: moved to '%s'.\n", filename, movefilename);
1062
+	ubuf.actime = fstat.st_atime;
1063
+	ubuf.modtime = fstat.st_mtime;
1064
+	utime(movefilename, &ubuf);
1065
+
1066
+	if(unlink(filename)) {
1067
+	    mprintf("@cannot unlink '%s': %s\n", filename, strerror(errno));
1068
+	    claminfo.notremoved++;            
1069
+	    free(movefilename);
1070
+	    return;
1071
+	}
1071 1072
     }
1072 1073
 
1074
+    mprintf("%s: moved to '%s'\n", filename, movefilename);
1075
+    logg("%s: moved to '%s'\n", filename, movefilename);
1076
+
1073 1077
     free(movefilename);
1074 1078
 }