Browse code

unified opts

git-svn-id: file:///var/lib/svn/clamav-devel/branches/clamd-proto@4620 77e5149b-7576-45b1-b177-96237e5ba77b

aCaB authored on 2009/01/17 03:29:00
Showing 3 changed files
... ...
@@ -116,6 +116,8 @@ int main(int argc, char **argv)
116 116
 	exit(ret);
117 117
     }
118 118
 
119
+    actsetup(opts);
120
+
119 121
     time(&starttime);
120 122
     /* ctime() does \n, but I need it once more */
121 123
 
... ...
@@ -50,11 +50,40 @@
50 50
 
51 51
 #define SOCKET_INET	AF_INET
52 52
 
53
-void move_infected(const char *filename, const struct optstruct *opts);
54 53
 int notremoved = 0, notmoved = 0;
55 54
 int printinfected = 0;
56 55
 
57
-static int dsresult(int sockd, const char *scantype, const char *filename, const struct optstruct *opts)
56
+static void (*action)(const char *) = NULL;
57
+static char *actarget;
58
+static void move_infected(const char *filename, int move);
59
+static void action_move(const char *filename) {
60
+    move_infected(filename, 1);
61
+}
62
+static void action_copy(const char *filename) {
63
+    move_infected(filename, 0);
64
+}
65
+static void action_remove(const char *filename) {
66
+    if(unlink(filename)) {
67
+	logg("!%s: Can't remove.\n", filename);
68
+	notremoved++;
69
+    } else {
70
+	logg("~%s: Removed.\n", filename);
71
+    }
72
+}
73
+
74
+void actsetup(const struct optstruct *opts) {
75
+    if(optget(opts, "move")->enabled) {
76
+	actarget = optget(opts, "move")->strarg;
77
+	action = action_move;
78
+    } else if (optget(opts, "copy")->enabled) {
79
+	actarget = optget(opts, "copy")->strarg;
80
+	action = action_copy;
81
+    } else if(optget(opts, "remove")->enabled) {
82
+	action = action_remove;
83
+    }
84
+}
85
+
86
+static int dsresult(int sockd, const char *scantype, const char *filename)
58 87
 {
59 88
 	int infected = 0, waserror = 0;
60 89
 	unsigned int len;
... ...
@@ -94,22 +123,11 @@ static int dsresult(int sockd, const char *scantype, const char *filename, const
94 94
 	    if(eol-bol > 7) {
95 95
 		if(!memcmp(eol - 6, " FOUND", 6)) {
96 96
 		    infected++;
97
-		    if(optget(opts, "move")->enabled || optget(opts, "copy")->enabled) {
98
-			char *com = strrchr(bol, ':');
99
-			if(com) {
100
-			    *com = '\0';
101
-			    move_infected(bol, opts);
102
-			}
103
-		    } else if(optget(opts, "remove")->enabled) {
104
-			char *com = strrchr(bol, ':');
105
-			if(com) {
106
-			    *com = '\0';
107
-			    if(unlink(bol)) {
108
-				logg("!%s: Can't remove.\n", bol);
109
-				notremoved++;
110
-			    } else {
111
-				logg("~%s: Removed.\n", bol);
112
-			    }
97
+		    if(action) {
98
+			char *comma = strrchr(bol, ':');
99
+			if(comma) {
100
+			    *comma = '\0';
101
+			    action(bol);
113 102
 			}
114 103
 		    }
115 104
 		} else if(!memcmp(eol-6, " ERROR", 6)) {
... ...
@@ -416,7 +434,7 @@ int client(const struct optstruct *opts, int *infected)
416 416
 	if((sockd = dconnect(opts, NULL)) < 0)
417 417
 	    return 2;
418 418
 
419
-	if((ret = dsresult(sockd, scantype, cwd, opts)) >= 0)
419
+	if((ret = dsresult(sockd, scantype, cwd)) >= 0)
420 420
 	    *infected += ret;
421 421
 	else
422 422
 	    errors++;
... ...
@@ -483,7 +501,7 @@ int client(const struct optstruct *opts, int *infected)
483 483
 			if((sockd = dconnect(opts, NULL)) < 0)
484 484
 			    return 2;
485 485
 
486
-			if((ret = dsresult(sockd, scantype, fullpath, opts)) >= 0)
486
+			if((ret = dsresult(sockd, scantype, fullpath)) >= 0)
487 487
 			    *infected += ret;
488 488
 			else
489 489
 			    errors++;
... ...
@@ -504,26 +522,16 @@ int client(const struct optstruct *opts, int *infected)
504 504
     return *infected ? 1 : (errors ? 2 : 0);
505 505
 }
506 506
 
507
-void move_infected(const char *filename, const struct optstruct *opts)
507
+void move_infected(const char *filename, int move)
508 508
 {
509
-	char *movedir, *movefilename, numext[4 + 1];
509
+	char *movefilename, numext[4 + 1];
510 510
 	const char *tmp;
511 511
 	struct stat ofstat, mfstat;
512 512
 	int n, len, movefilename_size;
513
-	int moveflag = optget(opts, "move")->enabled;
514 513
 	struct utimbuf ubuf;
515 514
 
516
-
517
-    if((moveflag && !(movedir = optget(opts, "move")->strarg)) ||
518
-        (!moveflag && !(movedir = optget(opts, "copy")->strarg))) {
519
-        /* Should never reach here */
520
-        logg("!optget() returned NULL\n");
521
-        notmoved++;
522
-        return;
523
-    }
524
-
525
-    if(access(movedir, W_OK|X_OK) == -1) {
526
-        logg("!problem %s file '%s': cannot write to '%s': %s\n", (moveflag) ? "moving" : "copying", filename, movedir, strerror(errno));
515
+    if(access(actarget, W_OK|X_OK) == -1) {
516
+        logg("!problem %s file '%s': cannot write to '%s': %s\n", (move) ? "moving" : "copying", filename, actarget, strerror(errno));
527 517
         notmoved++;
528 518
         return;
529 519
     }
... ...
@@ -538,14 +546,14 @@ void move_infected(const char *filename, const struct optstruct *opts)
538 538
     if(!(tmp = strrchr(filename, '/')))
539 539
 	tmp = filename;
540 540
 
541
-    movefilename_size = sizeof(char) * (strlen(movedir) + strlen(tmp) + sizeof(numext) + 2);
541
+    movefilename_size = sizeof(char) * (strlen(actarget) + strlen(tmp) + sizeof(numext) + 2);
542 542
 
543 543
     if(!(movefilename = malloc(movefilename_size))) {
544 544
         logg("!Memory allocation error\n");
545 545
 	exit(2);
546 546
     }
547 547
 
548
-    if(!(cli_strrcpy(movefilename, movedir))) {
548
+    if(!(cli_strrcpy(movefilename, actarget))) {
549 549
         logg("!cli_strrcpy() returned NULL\n");
550 550
         notmoved++;
551 551
         free(movefilename);
... ...
@@ -562,7 +570,7 @@ void move_infected(const char *filename, const struct optstruct *opts)
562 562
     }
563 563
 
564 564
     if(!stat(movefilename, &mfstat)) {
565
-        if((ofstat.st_dev == mfstat.st_dev) && (ofstat.st_ino == mfstat.st_ino)) { /* It's the same file*/
565
+        if((ofstat.st_dev == mfstat.st_dev) && (ofstat.st_ino == mfstat.st_ino)) { /* It's the same file */
566 566
             logg("File excluded '%s'\n", filename);
567 567
             notmoved++;
568 568
             free(movefilename);
... ...
@@ -586,9 +594,9 @@ void move_infected(const char *filename, const struct optstruct *opts)
586 586
        }
587 587
     }
588 588
 
589
-    if(!moveflag || rename(filename, movefilename) == -1) {
589
+    if(!move || rename(filename, movefilename) == -1) {
590 590
 	if(filecopy(filename, movefilename) == -1) {
591
-	    logg("^cannot %s '%s' to '%s': %s\n", (moveflag) ? "move" : "copy", filename, movefilename, strerror(errno));
591
+	    logg("^cannot %s '%s' to '%s': %s\n", (move) ? "move" : "copy", filename, movefilename, strerror(errno));
592 592
 	    notmoved++;
593 593
 	    free(movefilename);
594 594
 	    return;
... ...
@@ -602,15 +610,15 @@ void move_infected(const char *filename, const struct optstruct *opts)
602 602
 	ubuf.modtime = ofstat.st_mtime;
603 603
 	utime(movefilename, &ubuf);
604 604
 
605
-	if(moveflag && unlink(filename)) {
605
+	if(move && unlink(filename)) {
606 606
 	    logg("^cannot unlink '%s': %s\n", filename, strerror(errno));
607
-	    notremoved++;            
607
+	    notremoved++;
608 608
 	    free(movefilename);
609 609
 	    return;
610 610
 	}
611 611
     }
612 612
 
613
-    logg("%s: %s to '%s'\n", (moveflag)?"moved":"copied", filename, movefilename);
613
+    logg("%s: %s to '%s'\n", (move)?"moved":"copied", filename, movefilename);
614 614
 
615 615
     free(movefilename);
616 616
 }
... ...
@@ -21,6 +21,7 @@
21 21
 
22 22
 #include "shared/optparser.h"
23 23
 
24
+void actsetup(const struct optstruct *opts);
24 25
 int client(const struct optstruct *opts, int *infected);
25 26
 int get_clamd_version(const struct optstruct *opts);
26 27
 int reload_clamd_database(const struct optstruct *opts);