1*41fbaed0Stron /*	$NetBSD: qmgr_move.c,v 1.1.1.1 2009/06/23 10:08:50 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	qmgr_move 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	move queue entries to another queue
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	#include "qmgr.h"
10*41fbaed0Stron /*
11*41fbaed0Stron /*	void	qmgr_move(from, to, time_stamp)
12*41fbaed0Stron /*	const char *from;
13*41fbaed0Stron /*	const char *to;
14*41fbaed0Stron /*	time_t	time_stamp;
15*41fbaed0Stron /* DESCRIPTION
16*41fbaed0Stron /*	The \fBqmgr_move\fR routine scans the \fIfrom\fR queue for entries
17*41fbaed0Stron /*	with valid queue names and moves them to the \fIto\fR queue.
18*41fbaed0Stron /*	If \fItime_stamp\fR is non-zero, the queue file time stamps are
19*41fbaed0Stron /*	set to the specified value.
20*41fbaed0Stron /*	Entries with invalid names are left alone. No attempt is made to
21*41fbaed0Stron /*	look for other badness such as multiple links or weird file types.
22*41fbaed0Stron /*	These issues are dealt with when a queue file is actually opened.
23*41fbaed0Stron /* LICENSE
24*41fbaed0Stron /* .ad
25*41fbaed0Stron /* .fi
26*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
27*41fbaed0Stron /* AUTHOR(S)
28*41fbaed0Stron /*	Wietse Venema
29*41fbaed0Stron /*	IBM T.J. Watson Research
30*41fbaed0Stron /*	P.O. Box 704
31*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
32*41fbaed0Stron /*--*/
33*41fbaed0Stron 
34*41fbaed0Stron /* System library. */
35*41fbaed0Stron 
36*41fbaed0Stron #include <sys_defs.h>
37*41fbaed0Stron #include <sys/stat.h>
38*41fbaed0Stron #include <string.h>
39*41fbaed0Stron #include <utime.h>
40*41fbaed0Stron #include <errno.h>
41*41fbaed0Stron 
42*41fbaed0Stron /* Utility library. */
43*41fbaed0Stron 
44*41fbaed0Stron #include <msg.h>
45*41fbaed0Stron #include <scan_dir.h>
46*41fbaed0Stron #include <recipient_list.h>
47*41fbaed0Stron 
48*41fbaed0Stron /* Global library. */
49*41fbaed0Stron 
50*41fbaed0Stron #include <mail_queue.h>
51*41fbaed0Stron #include <mail_scan_dir.h>
52*41fbaed0Stron 
53*41fbaed0Stron /* Application-specific. */
54*41fbaed0Stron 
55*41fbaed0Stron #include "qmgr.h"
56*41fbaed0Stron 
57*41fbaed0Stron /* qmgr_move - move queue entries to another queue, leave bad files alone */
58*41fbaed0Stron 
qmgr_move(const char * src_queue,const char * dst_queue,time_t time_stamp)59*41fbaed0Stron void    qmgr_move(const char *src_queue, const char *dst_queue,
60*41fbaed0Stron 		          time_t time_stamp)
61*41fbaed0Stron {
62*41fbaed0Stron     const char *myname = "qmgr_move";
63*41fbaed0Stron     SCAN_DIR *queue_dir;
64*41fbaed0Stron     char   *queue_id;
65*41fbaed0Stron     struct utimbuf tbuf;
66*41fbaed0Stron     const char *path;
67*41fbaed0Stron 
68*41fbaed0Stron     if (strcmp(src_queue, dst_queue) == 0)
69*41fbaed0Stron 	msg_panic("%s: source queue %s is destination", myname, src_queue);
70*41fbaed0Stron     if (msg_verbose)
71*41fbaed0Stron 	msg_info("start move queue %s -> %s", src_queue, dst_queue);
72*41fbaed0Stron 
73*41fbaed0Stron     queue_dir = scan_dir_open(src_queue);
74*41fbaed0Stron     while ((queue_id = mail_scan_dir_next(queue_dir)) != 0) {
75*41fbaed0Stron 	if (mail_queue_id_ok(queue_id)) {
76*41fbaed0Stron 	    if (time_stamp > 0) {
77*41fbaed0Stron 		tbuf.actime = tbuf.modtime = time_stamp;
78*41fbaed0Stron 		path = mail_queue_path((VSTRING *) 0, src_queue, queue_id);
79*41fbaed0Stron 		if (utime(path, &tbuf) < 0) {
80*41fbaed0Stron 		    if (errno != ENOENT)
81*41fbaed0Stron 			msg_fatal("%s: update %s time stamps: %m", myname, path);
82*41fbaed0Stron 		    msg_warn("%s: update %s time stamps: %m", myname, path);
83*41fbaed0Stron 		    continue;
84*41fbaed0Stron 		}
85*41fbaed0Stron 	    }
86*41fbaed0Stron 	    if (mail_queue_rename(queue_id, src_queue, dst_queue)) {
87*41fbaed0Stron 		if (errno != ENOENT)
88*41fbaed0Stron 		    msg_fatal("%s: rename %s from %s to %s: %m",
89*41fbaed0Stron 			      myname, queue_id, src_queue, dst_queue);
90*41fbaed0Stron 		msg_warn("%s: rename %s from %s to %s: %m",
91*41fbaed0Stron 			 myname, queue_id, src_queue, dst_queue);
92*41fbaed0Stron 		continue;
93*41fbaed0Stron 	    }
94*41fbaed0Stron 	    if (msg_verbose)
95*41fbaed0Stron 		msg_info("%s: moved %s from %s to %s",
96*41fbaed0Stron 			 myname, queue_id, src_queue, dst_queue);
97*41fbaed0Stron 	} else {
98*41fbaed0Stron 	    msg_warn("%s: ignored: queue %s id %s",
99*41fbaed0Stron 		     myname, src_queue, queue_id);
100*41fbaed0Stron 	}
101*41fbaed0Stron     }
102*41fbaed0Stron     scan_dir_close(queue_dir);
103*41fbaed0Stron 
104*41fbaed0Stron     if (msg_verbose)
105*41fbaed0Stron 	msg_info("end move queue %s -> %s", src_queue, dst_queue);
106*41fbaed0Stron }
107