1 /*-
2  * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 
32 #include <limits.h>
33 #include <pthread.h>
34 #include <string.h>
35 
36 #include "compat_stdbool.h"
37 #include "compat_stdint.h"
38 #include "compat_inttypes.h"
39 #include "compat_limits.h"
40 #include "basedef.h"
41 
42 #include "collection.h"
43 #include "cvsync.h"
44 #include "cvsync_attr.h"
45 #include "hash.h"
46 #include "mux.h"
47 
48 #include "filecmp.h"
49 #include "updater.h"
50 
51 bool
filecmp_list(struct filecmp_args * fca)52 filecmp_list(struct filecmp_args *fca)
53 {
54 	static const uint8_t _cmd[3] = { 0x00, 0x01, UPDATER_END };
55 	struct collection *cl;
56 	uint8_t *cmd = fca->fca_cmd;
57 	size_t namelen, relnamelen, commentlen, len;
58 	int type;
59 
60 	switch (cvsync_list_pton(fca->fca_name)) {
61 	case CVSYNC_LIST_ALL:
62 		type = CVSYNC_RELEASE_MAX;
63 		break;
64 	case CVSYNC_LIST_RCS:
65 		type = CVSYNC_RELEASE_RCS;
66 		break;
67 	default:
68 		return (false);
69 	}
70 
71 	for (cl = fca->fca_collections_list ; cl != NULL ; cl = cl->cl_next) {
72 		if ((cvsync_release_pton(cl->cl_release) != type) &&
73 		    (type != CVSYNC_RELEASE_MAX)) {
74 			continue;
75 		}
76 
77 		namelen = strlen(cl->cl_name);
78 		relnamelen = strlen(cl->cl_release);
79 		commentlen = strlen(cl->cl_comment);
80 		len = namelen + relnamelen + commentlen + 5;
81 		if (len > fca->fca_cmdmax)
82 			return (false);
83 
84 		SetWord(cmd, len - 2);
85 		cmd[2] = UPDATER_UPDATE; /* dummy */
86 		cmd[3] = namelen;
87 		cmd[4] = relnamelen;
88 		if (!mux_send(fca->fca_mux, MUX_UPDATER, cmd, 5))
89 			return (false);
90 		if (!mux_send(fca->fca_mux, MUX_UPDATER, cl->cl_name, namelen))
91 			return (false);
92 		if (!mux_send(fca->fca_mux, MUX_UPDATER, cl->cl_release,
93 			      relnamelen)) {
94 			return (false);
95 		}
96 		if (commentlen > 0) {
97 			if (!mux_send(fca->fca_mux, MUX_UPDATER,
98 				      cl->cl_comment, commentlen)) {
99 				return (false);
100 			}
101 		}
102 	}
103 
104 	if (!mux_recv(fca->fca_mux, MUX_FILECMP_IN, cmd, 3))
105 		return (false);
106 	if (GetWord(cmd) != 1)
107 		return (false);
108 	if (cmd[2] != FILECMP_END)
109 		return (false);
110 
111 	if (!mux_send(fca->fca_mux, MUX_UPDATER, _cmd, sizeof(_cmd)))
112 		return (false);
113 
114 	return (true);
115 }
116