1 /* CVS client-related stuff.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details. */
19 #endif /* HAVE_CONFIG_H */
32 # if defined(AUTH_CLIENT_SUPPORT) || defined(HAVE_KERBEROS) || defined(HAVE_GSSAPI) || defined(SOCK_ERRNO) || defined(SOCK_STRERROR)
33 # ifdef HAVE_WINSOCK_H
35 # else /* No winsock.h */
36 # include <sys/socket.h>
37 # include <netinet/in.h>
38 # include <arpa/inet.h>
40 # endif /* No winsock.h */
43 /* If SOCK_ERRNO is defined, then send()/recv() and other socket calls
44 do not set errno, but that this macro should be used to obtain an
45 error code. This probably doesn't make sense unless
46 NO_SOCKET_TO_FD is also defined. */
48 # define SOCK_ERRNO errno
51 /* If SOCK_STRERROR is defined, then the error codes returned by
52 socket operations are not known to strerror, and this macro must be
53 used instead to convert those error codes to strings. */
54 # ifndef SOCK_STRERROR
55 # define SOCK_STRERROR strerror
62 extern char *strerror ();
64 # endif /* ! SOCK_STRERROR */
70 extern char *krb_realmofhost ();
71 # ifndef HAVE_KRB_GET_ERR_TEXT
72 # define krb_get_err_text(status) krb_err_txt[status]
73 # endif /* HAVE_KRB_GET_ERR_TEXT */
75 /* Information we need if we are going to use Kerberos encryption. */
76 static C_Block kblock;
77 static Key_schedule sched;
79 # endif /* HAVE_KERBEROS */
85 /* This is needed for GSSAPI encryption. */
86 static gss_ctx_id_t gcontext;
88 static int connect_to_gserver PROTO((cvsroot_t *, int, struct hostent *));
90 # endif /* HAVE_GSSAPI */
94 /* Keep track of any paths we are sending for Max-dotdot so that we can verify
95 * that uplevel paths coming back form the server are valid.
97 * FIXME: The correct way to do this is probably provide some sort of virtual
98 * path map on the client side. This would be generic enough to be applied to
99 * absolute paths supplied by the user too.
101 static List *uppaths = NULL;
105 static void add_prune_candidate PROTO((const char *));
107 /* All the commands. */
108 int add PROTO((int argc, char **argv));
109 int admin PROTO((int argc, char **argv));
110 int checkout PROTO((int argc, char **argv));
111 int commit PROTO((int argc, char **argv));
112 int diff PROTO((int argc, char **argv));
113 int history PROTO((int argc, char **argv));
114 int import PROTO((int argc, char **argv));
115 int cvslog PROTO((int argc, char **argv));
116 int patch PROTO((int argc, char **argv));
117 int release PROTO((int argc, char **argv));
118 int cvsremove PROTO((int argc, char **argv));
119 int rtag PROTO((int argc, char **argv));
120 int status PROTO((int argc, char **argv));
121 int tag PROTO((int argc, char **argv));
122 int update PROTO((int argc, char **argv));
124 /* All the response handling functions. */
125 static void handle_ok PROTO((char *, int));
126 static void handle_error PROTO((char *, int));
127 static void handle_valid_requests PROTO((char *, int));
128 static void handle_checked_in PROTO((char *, int));
129 static void handle_new_entry PROTO((char *, int));
130 static void handle_checksum PROTO((char *, int));
131 static void handle_copy_file PROTO((char *, int));
132 static void handle_updated PROTO((char *, int));
133 static void handle_merged PROTO((char *, int));
134 static void handle_patched PROTO((char *, int));
135 static void handle_rcs_diff PROTO((char *, int));
136 static void handle_removed PROTO((char *, int));
137 static void handle_remove_entry PROTO((char *, int));
138 static void handle_set_static_directory PROTO((char *, int));
139 static void handle_clear_static_directory PROTO((char *, int));
140 static void handle_set_sticky PROTO((char *, int));
141 static void handle_clear_sticky PROTO((char *, int));
142 static void handle_module_expansion PROTO((char *, int));
143 static void handle_wrapper_rcs_option PROTO((char *, int));
144 static void handle_m PROTO((char *, int));
145 static void handle_e PROTO((char *, int));
146 static void handle_f PROTO((char *, int));
147 static void handle_notified PROTO((char *, int));
149 static size_t try_read_from_server PROTO ((char *, size_t));
151 static void auth_server PROTO ((cvsroot_t *, struct buffer *, struct buffer *,
152 int, int, struct hostent *));
154 /* We need to keep track of the list of directories we've sent to the
155 server. This list, along with the current CVSROOT, will help us
156 decide which command-line arguments to send. */
157 List *dirs_sent_to_server = NULL;
159 static int is_arg_a_parent_or_listed_dir PROTO((Node *, void *));
162 is_arg_a_parent_or_listed_dir (n, d)
166 char *directory = n->key; /* name of the dir sent to server */
167 char *this_argv_elem = (char *) d; /* this argv element */
169 /* Say we should send this argument if the argument matches the
170 beginning of a directory name sent to the server. This way,
171 the server will know to start at the top of that directory
172 hierarchy and descend. */
174 if (strncmp (directory, this_argv_elem, strlen (this_argv_elem)) == 0)
180 static int arg_should_not_be_sent_to_server PROTO((char *));
182 /* Return nonzero if this argument should not be sent to the
186 arg_should_not_be_sent_to_server (arg)
189 /* Decide if we should send this directory name to the server. We
190 should always send argv[i] if:
192 1) the list of directories sent to the server is empty (as it
193 will be for checkout, etc.).
195 2) the argument is "."
197 3) the argument is a file in the cwd and the cwd is checked out
198 from the current root
200 4) the argument lies within one of the paths in
205 if (list_isempty (dirs_sent_to_server))
206 return 0; /* always send it */
208 if (strcmp (arg, ".") == 0)
209 return 0; /* always send it */
211 /* We should send arg if it is one of the directories sent to the
212 server or the parent of one; this tells the server to descend
213 the hierarchy starting at this level. */
216 if (walklist (dirs_sent_to_server, is_arg_a_parent_or_listed_dir, arg))
219 /* If arg wasn't a parent, we don't know anything about it (we
220 would have seen something related to it during the
221 send_files phase). Don't send it. */
225 /* Try to decide whether we should send arg to the server by
226 checking the contents of the corresponding CVSADM directory. */
230 /* Calculate "dirname arg" */
231 for (t = arg + strlen (arg) - 1; t >= arg; t--)
237 /* Now we're either poiting to the beginning of the
238 string, or we found a path separator. */
241 /* Found a path separator. */
245 /* First, check to see if we sent this directory to the
246 server, because it takes less time than actually
247 opening the stuff in the CVSADM directory. */
248 if (walklist (dirs_sent_to_server, is_arg_a_parent_or_listed_dir,
251 *t = c; /* make sure to un-truncate the arg */
255 /* Since we didn't find it in the list, check the CVSADM
257 this_root = Name_Root (arg, (char *) NULL);
262 /* We're at the beginning of the string. Look at the
263 CVSADM files in cwd. */
264 this_root = (CVSroot_cmdline ? xstrdup(CVSroot_cmdline)
265 : Name_Root ((char *) NULL, (char *) NULL));
268 /* Now check the value for root. */
269 if (CVSroot_cmdline == NULL && this_root && current_parsed_root
270 && (strcmp (this_root, current_parsed_root->original) != 0))
272 /* Don't send this, since the CVSROOTs don't match. */
279 /* OK, let's send it. */
285 #endif /* CLIENT_SUPPORT */
289 #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
291 /* Shared with server. */
294 * Return a malloc'd, '\0'-terminated string
295 * corresponding to the mode in SB.
299 mode_to_string (mode_t mode)
300 #else /* ! __STDC__ */
301 mode_to_string (mode)
303 #endif /* __STDC__ */
305 char buf[18], u[4], g[4], o[4];
309 if (mode & S_IRUSR) u[i++] = 'r';
310 if (mode & S_IWUSR) u[i++] = 'w';
311 if (mode & S_IXUSR) u[i++] = 'x';
315 if (mode & S_IRGRP) g[i++] = 'r';
316 if (mode & S_IWGRP) g[i++] = 'w';
317 if (mode & S_IXGRP) g[i++] = 'x';
321 if (mode & S_IROTH) o[i++] = 'r';
322 if (mode & S_IWOTH) o[i++] = 'w';
323 if (mode & S_IXOTH) o[i++] = 'x';
326 sprintf(buf, "u=%s,g=%s,o=%s", u, g, o);
331 * Change mode of FILENAME to MODE_STRING.
332 * Returns 0 for success or errno code.
333 * If RESPECT_UMASK is set, then honor the umask.
336 change_mode (filename, mode_string, respect_umask)
345 /* We can only distinguish between
348 3) Picasso's "Blue Period"
349 We handle the first two. */
353 if ((p[0] == 'u' || p[0] == 'g' || p[0] == 'o') && p[1] == '=')
356 while (*q != ',' && *q != '\0')
363 /* Skip to the next field. */
364 while (*p != ',' && *p != '\0')
370 /* xchmod honors the umask for us. In the !respect_umask case, we
371 don't try to cope with it (probably to handle that well, the server
372 needs to deal with modes in data structures, rather than via the
373 modes in temporary files). */
374 xchmod (filename, writeable);
377 #else /* ! CHMOD_BROKEN */
386 if ((p[0] == 'u' || p[0] == 'g' || p[0] == 'o') && p[1] == '=')
388 int can_read = 0, can_write = 0, can_execute = 0;
390 while (*q != ',' && *q != '\0')
409 else if (p[0] == 'g')
418 else if (p[0] == 'o')
428 /* Skip to the next field. */
429 while (*p != ',' && *p != '\0')
438 (void) umask (oumask);
442 if (chmod (filename, mode) < 0)
445 #endif /* ! CHMOD_BROKEN */
448 #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
450 #ifdef CLIENT_SUPPORT
452 int client_prune_dirs;
454 static List *ignlist = (List *) NULL;
456 /* Buffer to write to the server. */
457 static struct buffer *to_server;
459 /* Buffer used to read from the server. */
460 static struct buffer *from_server;
463 /* We want to be able to log data sent between us and the server. We
464 do it using log buffers. Each log buffer has another buffer which
465 handles the actual I/O, and a file to log information to.
467 This structure is the closure field of a log buffer. */
471 /* The underlying buffer. */
473 /* The file to log information to. */
477 static struct buffer *log_buffer_initialize
478 PROTO((struct buffer *, FILE *, int, void (*) (struct buffer *)));
479 static int log_buffer_input PROTO((void *, char *, int, int, int *));
480 static int log_buffer_output PROTO((void *, const char *, int, int *));
481 static int log_buffer_flush PROTO((void *));
482 static int log_buffer_block PROTO((void *, int));
483 static int log_buffer_shutdown PROTO((struct buffer *));
485 /* Create a log buffer. */
487 static struct buffer *
488 log_buffer_initialize (buf, fp, input, memory)
492 void (*memory) PROTO((struct buffer *));
494 struct log_buffer *n;
496 n = (struct log_buffer *) xmalloc (sizeof *n);
499 return buf_initialize (input ? log_buffer_input : NULL,
500 input ? NULL : log_buffer_output,
501 input ? NULL : log_buffer_flush,
508 /* The input function for a log buffer. */
511 log_buffer_input (closure, data, need, size, got)
518 struct log_buffer *lb = (struct log_buffer *) closure;
522 if (lb->buf->input == NULL)
525 status = (*lb->buf->input) (lb->buf->closure, data, need, size, got);
532 if (fwrite (data, 1, n_to_write, lb->log) != n_to_write)
533 error (0, errno, "writing to log file");
539 /* The output function for a log buffer. */
542 log_buffer_output (closure, data, have, wrote)
548 struct log_buffer *lb = (struct log_buffer *) closure;
552 if (lb->buf->output == NULL)
555 status = (*lb->buf->output) (lb->buf->closure, data, have, wrote);
562 if (fwrite (data, 1, n_to_write, lb->log) != n_to_write)
563 error (0, errno, "writing to log file");
569 /* The flush function for a log buffer. */
572 log_buffer_flush (closure)
575 struct log_buffer *lb = (struct log_buffer *) closure;
577 if (lb->buf->flush == NULL)
580 /* We don't really have to flush the log file here, but doing it
581 will let tail -f on the log file show what is sent to the
582 network as it is sent. */
583 if (fflush (lb->log) != 0)
584 error (0, errno, "flushing log file");
586 return (*lb->buf->flush) (lb->buf->closure);
589 /* The block function for a log buffer. */
592 log_buffer_block (closure, block)
596 struct log_buffer *lb = (struct log_buffer *) closure;
599 return set_block (lb->buf);
601 return set_nonblock (lb->buf);
604 /* The shutdown function for a log buffer. */
607 log_buffer_shutdown (buf)
610 struct log_buffer *lb = (struct log_buffer *) buf->closure;
613 retval = buf_shutdown (lb->buf);
614 if (fclose (lb->log) < 0)
615 error (0, errno, "closing log file");
619 #ifdef NO_SOCKET_TO_FD
621 /* Under certain circumstances, we must communicate with the server
622 via a socket using send() and recv(). This is because under some
623 operating systems (OS/2 and Windows 95 come to mind), a socket
624 cannot be converted to a file descriptor -- it must be treated as a
625 socket and nothing else.
627 We may also need to deal with socket routine error codes differently
628 in these cases. This is handled through the SOCK_ERRNO and
629 SOCK_STRERROR macros. */
631 /* These routines implement a buffer structure which uses send and
632 recv. The buffer is always in blocking mode so we don't implement
633 the block routine. */
635 /* Note that it is important that these routines always handle errors
636 internally and never return a positive errno code, since it would in
637 general be impossible for the caller to know in general whether any
638 error code came from a socket routine (to decide whether to use
639 SOCK_STRERROR or simply strerror to print an error message). */
641 /* We use an instance of this structure as the closure field. */
645 /* The socket number. */
649 static struct buffer *socket_buffer_initialize
650 PROTO ((int, int, void (*) (struct buffer *)));
651 static int socket_buffer_input PROTO((void *, char *, int, int, int *));
652 static int socket_buffer_output PROTO((void *, const char *, int, int *));
653 static int socket_buffer_flush PROTO((void *));
654 static int socket_buffer_shutdown PROTO((struct buffer *));
658 /* Create a buffer based on a socket. */
660 static struct buffer *
661 socket_buffer_initialize (socket, input, memory)
664 void (*memory) PROTO((struct buffer *));
666 struct socket_buffer *n;
668 n = (struct socket_buffer *) xmalloc (sizeof *n);
670 return buf_initialize (input ? socket_buffer_input : NULL,
671 input ? NULL : socket_buffer_output,
672 input ? NULL : socket_buffer_flush,
673 (int (*) PROTO((void *, int))) NULL,
674 socket_buffer_shutdown,
681 /* The buffer input function for a buffer built on a socket. */
684 socket_buffer_input (closure, data, need, size, got)
691 struct socket_buffer *sb = (struct socket_buffer *) closure;
694 /* I believe that the recv function gives us exactly the semantics
695 we want. If there is a message, it returns immediately with
696 whatever it could get. If there is no message, it waits until
697 one comes in. In other words, it is not like read, which in
698 blocking mode normally waits until all the requested data is
706 /* Note that for certain (broken?) networking stacks, like
707 VMS's UCX (not sure what version, problem reported with
708 recv() in 1997), and (according to windows-NT/config.h)
709 Windows NT 3.51, we must call recv or send with a
710 moderately sized buffer (say, less than 200K or something),
711 or else there may be network errors (somewhat hard to
712 produce, e.g. WAN not LAN or some such). buf_read_data
713 makes sure that we only recv() BUFFER_DATA_SIZE bytes at
716 nbytes = recv (sb->socket, data, size, 0);
718 error (1, 0, "reading from server: %s", SOCK_STRERROR (SOCK_ERRNO));
721 /* End of file (for example, the server has closed
722 the connection). If we've already read something, we
723 just tell the caller about the data, not about the end of
724 file. If we've read nothing, we return end of file. */
742 /* The buffer output function for a buffer built on a socket. */
745 socket_buffer_output (closure, data, have, wrote)
751 struct socket_buffer *sb = (struct socket_buffer *) closure;
755 /* See comment in socket_buffer_input regarding buffer size we pass
758 #ifdef SEND_NEVER_PARTIAL
759 /* If send() never will produce a partial write, then just do it. This
760 is needed for systems where its return value is something other than
761 the number of bytes written. */
762 if (send (sb->socket, data, have, 0) < 0)
763 error (1, 0, "writing to server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
769 nbytes = send (sb->socket, data, have, 0);
771 error (1, 0, "writing to server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
783 /* The buffer flush function for a buffer built on a socket. */
787 socket_buffer_flush (closure)
790 /* Nothing to do. Sockets are always flushed. */
797 socket_buffer_shutdown (buf)
800 struct socket_buffer *n = (struct socket_buffer *) buf->closure;
803 /* no need to flush children of an endpoint buffer here */
808 if (! buf_empty_p (buf)
809 || (err = recv (n->socket, &tmp, 1, 0)) > 0)
810 error (0, 0, "dying gasps from %s unexpected", current_parsed_root->hostname);
812 error (0, 0, "reading from %s: %s", current_parsed_root->hostname, SOCK_STRERROR (SOCK_ERRNO));
814 /* shutdown() socket */
815 # ifdef SHUTDOWN_SERVER
816 if (current_parsed_root->method != server_method)
818 if (shutdown (n->socket, 0) < 0)
820 error (1, 0, "shutting down server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
825 else if (buf->output)
827 /* shutdown() socket */
828 # ifdef SHUTDOWN_SERVER
829 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
830 * SHUTDOWN_SERVER_OUTPUT
832 if (current_parsed_root->method == server_method)
833 SHUTDOWN_SERVER (n->socket);
836 if (shutdown (n->socket, 1) < 0)
838 error (1, 0, "shutting down server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
847 #endif /* NO_SOCKET_TO_FD */
850 * Read a line from the server. Result does not include the terminating \n.
852 * Space for the result is malloc'd and should be freed by the caller.
854 * Returns number of bytes read.
864 status = buf_flush (to_server, 1);
866 error (1, status, "writing to server");
868 status = buf_read_line (from_server, &result, &len);
872 error (1, 0, "end of file from server (consult above messages if any)");
873 else if (status == -2)
874 error (1, 0, "out of memory");
876 error (1, status, "reading from server");
887 #endif /* CLIENT_SUPPORT */
890 #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
893 * Zero if compression isn't supported or requested; non-zero to indicate
894 * a compression level to request from gzip.
899 * Level of compression to use when running gzip on a single file.
903 #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
905 #ifdef CLIENT_SUPPORT
908 * The Repository for the top level of this command (not necessarily
909 * the CVSROOT, just the current directory at the time we do it).
911 static char *toplevel_repos = NULL;
913 /* Working directory when we first started. Note: we could speed things
914 up on some systems by using savecwd.h here instead of just always
919 handle_ok (args, len)
927 handle_error (args, len)
931 int something_printed;
934 * First there is a symbolic error code followed by a space, which
937 char *p = strchr (args, ' ');
940 error (0, 0, "invalid data from cvs server");
945 /* Next we print the text of the message from the server. We
946 probably should be prefixing it with "server error" or some
947 such, because if it is something like "Out of memory", the
948 current behavior doesn't say which machine is out of
952 something_printed = 0;
953 for (; len > 0; --len)
955 something_printed = 1;
958 if (something_printed)
963 handle_valid_requests (args, len)
975 for (rq = requests; rq->name != NULL; ++rq)
977 if (strcmp (rq->name, p) == 0)
980 if (rq->name == NULL)
982 * It is a request we have never heard of (and thus never
983 * will want to use). So don't worry about it.
988 if (rq->flags & RQ_ENABLEME)
991 * Server wants to know if we have this, to enable the
994 send_to_server (rq->name, 0);
995 send_to_server ("\012", 0);
998 rq->flags |= RQ_SUPPORTED;
1001 } while (q != NULL);
1002 for (rq = requests; rq->name != NULL; ++rq)
1004 if ((rq->flags & RQ_SUPPORTED)
1005 || (rq->flags & RQ_ENABLEME))
1007 if (rq->flags & RQ_ESSENTIAL)
1008 error (1, 0, "request `%s' not supported by server", rq->name);
1015 * This is a proc for walklist(). It inverts the error return premise of
1019 * True If this path is prefixed by one of the paths in walklist and
1020 * does not step above the prefix path.
1024 int path_list_prefixed (p, closure)
1028 const char *questionable = closure;
1029 const char *prefix = p->key;
1030 if (strncmp (prefix, questionable, strlen (prefix))) return 0;
1031 questionable += strlen (prefix);
1032 while (ISDIRSEP (*questionable)) questionable++;
1033 if (*questionable == '\0') return 1;
1034 return pathname_levels (questionable);
1040 * Need to validate the client pathname. Disallowed paths include:
1042 * 1. Absolute paths.
1043 * 2. Pathnames that do not reference a specifically requested update
1046 * In case 2, we actually only check that the directory is under the uppermost
1047 * directories mentioned on the command line.
1050 * True If the path is valid.
1054 int is_valid_client_path (pathname)
1055 const char *pathname;
1057 /* 1. Absolute paths. */
1058 if (isabsolute (pathname)) return 0;
1059 /* 2. No up-references in path. */
1060 if (pathname_levels (pathname) == 0) return 1;
1061 /* 2. No Max-dotdot paths registered. */
1062 if (uppaths == NULL) return 0;
1064 return walklist (uppaths, path_list_prefixed, (void *)pathname);
1070 * Do all the processing for PATHNAME, where pathname consists of the
1071 * repository and the filename. The parameters we pass to FUNC are:
1072 * DATA is just the DATA parameter which was passed to
1073 * call_in_directory; ENT_LIST is a pointer to an entries list (which
1074 * we manage the storage for); SHORT_PATHNAME is the pathname of the
1075 * file relative to the (overall) directory in which the command is
1076 * taking place; and FILENAME is the filename portion only of
1077 * SHORT_PATHNAME. When we call FUNC, the curent directory points to
1078 * the directory portion of SHORT_PATHNAME. */
1081 call_in_directory (pathname, func, data)
1083 void (*func) PROTO((char *data, List *ent_list, char *short_pathname,
1087 /* This variable holds the result of Entries_Open. */
1088 List *last_entries = NULL;
1091 /* This is what we get when we hook up the directory (working directory
1092 name) from PATHNAME with the filename from REPOSNAME. For example:
1094 reposname: /u/src/master/ccvs/foo/ChangeLog
1095 short_pathname: ccvs/src/ChangeLog
1097 char *short_pathname;
1101 * Do the whole descent in parallel for the repositories, so we
1102 * know what to put in CVS/Repository files. I'm not sure the
1103 * full hair is necessary since the server does a similar
1104 * computation; I suspect that we only end up creating one
1105 * directory at a time anyway.
1107 * Also note that we must *only* worry about this stuff when we
1108 * are creating directories; `cvs co foo/bar; cd foo/bar; cvs co
1109 * CVSROOT; cvs update' is legitimate, but in this case
1110 * foo/bar/CVSROOT/CVS/Repository is not a subdirectory of
1111 * foo/bar/CVS/Repository.
1117 int reposdirname_absolute;
1121 read_line (&reposname);
1122 assert (reposname != NULL);
1124 reposdirname_absolute = 0;
1125 if (strncmp (reposname, toplevel_repos, strlen (toplevel_repos)) != 0)
1127 reposdirname_absolute = 1;
1128 short_repos = reposname;
1132 short_repos = reposname + strlen (toplevel_repos) + 1;
1133 if (short_repos[-1] != '/')
1135 reposdirname_absolute = 1;
1136 short_repos = reposname;
1140 /* Now that we have SHORT_REPOS, we can calculate the path to the file we
1141 * are being requested to operate on.
1143 filename = strrchr (short_repos, '/');
1144 if (filename == NULL)
1145 filename = short_repos;
1149 short_pathname = xmalloc (strlen (pathname) + strlen (filename) + 5);
1150 strcpy (short_pathname, pathname);
1151 strcat (short_pathname, filename);
1153 /* Now that we know the path to the file we were requested to operate on,
1154 * we can verify that it is valid.
1156 * For security reasons, if SHORT_PATHNAME is absolute or attempts to
1157 * ascend outside of the current sanbbox, we abort. The server should not
1158 * send us anything but relative paths which remain inside the sandbox
1159 * here. Anything less means a trojan CVS server could create and edit
1160 * arbitrary files on the client.
1162 if (!is_valid_client_path (short_pathname))
1165 "Server attempted to update a file via an invalid pathname:");
1166 error (1, 0, "`%s'.", short_pathname);
1169 reposdirname = xstrdup (short_repos);
1170 p = strrchr (reposdirname, '/');
1173 reposdirname = xrealloc (reposdirname, 2);
1174 reposdirname[0] = '.'; reposdirname[1] = '\0';
1179 dir_name = xstrdup (pathname);
1180 p = strrchr (dir_name, '/');
1183 dir_name = xrealloc (dir_name, 2);
1184 dir_name[0] = '.'; dir_name[1] = '\0';
1188 if (client_prune_dirs)
1189 add_prune_candidate (dir_name);
1191 if (toplevel_wd == NULL)
1193 toplevel_wd = xgetwd ();
1194 if (toplevel_wd == NULL)
1195 error (1, errno, "could not get working directory");
1198 if (CVS_CHDIR (toplevel_wd) < 0)
1199 error (1, errno, "could not chdir to %s", toplevel_wd);
1201 /* Create the CVS directory at the top level if needed. The
1202 isdir seems like an unneeded system call, but it *does*
1203 need to be called both if the CVS_CHDIR below succeeds
1204 (e.g. "cvs co .") or if it fails (e.g. basicb-1a in
1205 testsuite). We only need to do this for the "." case,
1206 since the server takes care of forcing this directory to be
1207 created in all other cases. If we don't create CVSADM
1208 here, the call to Entries_Open below will fail. FIXME:
1209 perhaps this means that we should change our algorithm
1210 below that calls Create_Admin instead of having this code
1212 if (/* I think the reposdirname_absolute case has to do with
1213 things like "cvs update /foo/bar". In any event, the
1214 code below which tries to put toplevel_repos into
1215 CVS/Repository is almost surely unsuited to
1216 the reposdirname_absolute case. */
1217 !reposdirname_absolute
1218 && (strcmp (dir_name, ".") == 0)
1219 && ! isdir (CVSADM))
1226 repo = xmalloc (strlen (toplevel_repos)
1228 strcpy (repo, toplevel_repos);
1229 r = repo + strlen (repo);
1230 if (r[-1] != '.' || r[-2] != '/')
1233 Create_Admin (".", ".", repo, (char *) NULL,
1234 (char *) NULL, 0, 1, 1);
1239 if (CVS_CHDIR (dir_name) < 0)
1244 if (! existence_error (errno))
1245 error (1, errno, "could not chdir to %s", dir_name);
1247 /* Directory does not exist, we need to create it. */
1250 /* Provided we are willing to assume that directories get
1251 created one at a time, we could simplify this a lot.
1252 Do note that one aspect still would need to walk the
1253 dir_name path: the checking for "fncmp (dir, CVSADM)". */
1255 dir = xmalloc (strlen (dir_name) + 1);
1257 rdirp = reposdirname;
1259 /* This algorithm makes nested directories one at a time
1260 and create CVS administration files in them. For
1261 example, we're checking out foo/bar/baz from the
1264 1) create foo, point CVS/Repository to <root>/foo
1265 2) .. foo/bar .. <root>/foo/bar
1266 3) .. foo/bar/baz .. <root>/foo/bar/baz
1268 As you can see, we're just stepping along DIR_NAME (with
1269 DIRP) and REPOSDIRNAME (with RDIRP) respectively.
1271 We need to be careful when we are checking out a
1272 module, however, since DIR_NAME and REPOSDIRNAME are not
1273 going to be the same. Since modules will not have any
1274 slashes in their names, we should watch the output of
1275 STRCHR to decide whether or not we should use STRCHR on
1276 the RDIRP. That is, if we're down to a module name,
1277 don't keep picking apart the repository directory name. */
1281 dirp = strchr (dirp, '/');
1284 strncpy (dir, dir_name, dirp - dir_name);
1285 dir[dirp - dir_name] = '\0';
1286 /* Skip the slash. */
1289 /* This just means that the repository string has
1290 fewer components than the dir_name string. But
1291 that is OK (e.g. see modules3-8 in testsuite). */
1294 rdirp = strchr (rdirp, '/');
1298 /* If there are no more slashes in the dir name,
1299 we're down to the most nested directory -OR- to
1300 the name of a module. In the first case, we
1301 should be down to a DIRP that has no slashes,
1302 so it won't help/hurt to do another STRCHR call
1303 on DIRP. It will definitely hurt, however, if
1304 we're down to a module name, since a module
1305 name can point to a nested directory (that is,
1306 DIRP will still have slashes in it. Therefore,
1307 we should set it to NULL so the routine below
1308 copies the contents of REMOTEDIRNAME onto the
1309 root repository directory (does this if rdirp
1310 is set to NULL, because we used to do an extra
1311 STRCHR call here). */
1314 strcpy (dir, dir_name);
1317 if (fncmp (dir, CVSADM) == 0)
1319 error (0, 0, "cannot create a directory named %s", dir);
1320 error (0, 0, "because CVS uses \"%s\" for its own uses",
1322 error (1, 0, "rename the directory and try again");
1325 if (mkdir_if_needed (dir))
1327 /* It already existed, fine. Just keep going. */
1329 else if (strcmp (cvs_cmd_name, "export") == 0)
1330 /* Don't create CVSADM directories if this is export. */
1335 * Put repository in CVS/Repository. For historical
1336 * (pre-CVS/Root) reasons, this is an absolute pathname,
1337 * but what really matters is the part of it which is
1338 * relative to cvsroot.
1343 repo = xmalloc (strlen (reposdirname)
1344 + strlen (toplevel_repos)
1346 if (reposdirname_absolute)
1350 strcpy (repo, toplevel_repos);
1352 r = repo + strlen (repo);
1357 /* See comment near start of function; the only
1358 way that the server can put the right thing
1359 in each CVS/Repository file is to create the
1360 directories one at a time. I think that the
1361 CVS server has been doing this all along. */
1363 warning: server is not creating directories one at a time");
1364 strncpy (r, reposdirname, rdirp - reposdirname);
1365 r[rdirp - reposdirname] = '\0';
1368 strcpy (r, reposdirname);
1370 Create_Admin (dir, dir, repo,
1371 (char *)NULL, (char *)NULL, 0, 0, 1);
1374 b = strrchr (dir, '/');
1376 Subdir_Register ((List *) NULL, (char *) NULL, dir);
1380 Subdir_Register ((List *) NULL, dir, b + 1);
1387 /* Skip the slash. */
1391 } while (dirp != NULL);
1393 /* Now it better work. */
1394 if ( CVS_CHDIR (dir_name) < 0)
1395 error (1, errno, "could not chdir to %s", dir_name);
1397 else if (strcmp (cvs_cmd_name, "export") == 0)
1398 /* Don't create CVSADM directories if this is export. */
1400 else if (!isdir (CVSADM))
1403 * Put repository in CVS/Repository. For historical
1404 * (pre-CVS/Root) reasons, this is an absolute pathname,
1405 * but what really matters is the part of it which is
1406 * relative to cvsroot.
1410 if (reposdirname_absolute)
1411 repo = reposdirname;
1414 repo = xmalloc (strlen (reposdirname)
1415 + strlen (toplevel_repos)
1417 strcpy (repo, toplevel_repos);
1419 strcat (repo, reposdirname);
1422 Create_Admin (".", ".", repo, (char *)NULL, (char *)NULL, 0, 1, 1);
1423 if (repo != reposdirname)
1427 if (strcmp (cvs_cmd_name, "export") != 0)
1429 last_entries = Entries_Open (0, dir_name);
1431 /* If this is a newly created directory, we will record
1432 all subdirectory information, so call Subdirs_Known in
1433 case there are no subdirectories. If this is not a
1434 newly created directory, it may be an old working
1435 directory from before we recorded subdirectory
1436 information in the Entries file. We force a search for
1437 all subdirectories now, to make sure our subdirectory
1438 information is up to date. If the Entries file does
1439 record subdirectory information, then this call only
1440 does list manipulation. */
1442 Subdirs_Known (last_entries);
1447 dirlist = Find_Directories ((char *) NULL, W_LOCAL,
1452 free (reposdirname);
1453 (*func) (data, last_entries, short_pathname, filename);
1454 if (last_entries != NULL)
1455 Entries_Close (last_entries);
1457 free (short_pathname);
1462 copy_a_file (data, ent_list, short_pathname, filename)
1465 char *short_pathname;
1469 #ifdef USE_VMS_FILENAMES
1473 read_line (&newname);
1475 #ifdef USE_VMS_FILENAMES
1476 /* Mogrify the filename so VMS is happy with it. */
1477 for(p = newname; *p; p++)
1478 if(*p == '.' || *p == '#') *p = '_';
1480 /* cvsclient.texi has said for a long time that newname must be in the
1481 same directory. Wouldn't want a malicious or buggy server overwriting
1482 ~/.profile, /etc/passwd, or anything like that. */
1483 if (last_component (newname) != newname)
1484 error (1, 0, "protocol error: Copy-file tried to specify directory");
1486 if (unlink_file (newname) && !existence_error (errno))
1487 error (0, errno, "unable to remove %s", newname);
1488 copy_file (filename, newname);
1493 handle_copy_file (args, len)
1497 call_in_directory (args, copy_a_file, (char *)NULL);
1501 static void read_counted_file PROTO ((char *, char *));
1503 /* Read from the server the count for the length of a file, then read
1504 the contents of that file and write them to FILENAME. FULLNAME is
1505 the name of the file for use in error messages. FIXME-someday:
1506 extend this to deal with compressed files and make update_entries
1507 use it. On error, gives a fatal error. */
1509 read_counted_file (filename, fullname)
1517 /* Pointers in buf to the place to put data which will be read,
1518 and the data which needs to be written, respectively. */
1521 /* Number of bytes left to read and number of bytes in buf waiting to
1522 be written, respectively. */
1528 read_line (&size_string);
1529 if (size_string[0] == 'z')
1531 protocol error: compressed files not supported for that operation");
1532 /* FIXME: should be doing more error checking, probably. Like using
1533 strtoul and making sure we used up the whole line. */
1534 size = atoi (size_string);
1537 /* A more sophisticated implementation would use only a limited amount
1538 of buffer space (8K perhaps), and read that much at a time. We allocate
1539 a buffer for the whole file only to make it easy to keep track what
1540 needs to be read and written. */
1541 buf = xmalloc (size);
1543 /* FIXME-someday: caller should pass in a flag saying whether it
1544 is binary or not. I haven't carefully looked into whether
1545 CVS/Template files should use local text file conventions or
1547 fp = CVS_FOPEN (filename, "wb");
1549 error (1, errno, "cannot write %s", fullname);
1554 while (nread > 0 || nwrite > 0)
1560 n = try_read_from_server (pread, nread);
1568 n = fwrite (pwrite, 1, nwrite, fp);
1570 error (1, errno, "cannot write %s", fullname);
1576 if (fclose (fp) < 0)
1577 error (1, errno, "cannot close %s", fullname);
1580 /* OK, we want to swallow the "U foo.c" response and then output it only
1581 if we can update the file. In the future we probably want some more
1582 systematic approach to parsing tagged text, but for now we keep it
1583 ad hoc. "Why," I hear you cry, "do we not just look at the
1584 Update-existing and Created responses?" That is an excellent question,
1585 and the answer is roughly conservatism/laziness--I haven't read through
1586 update.c enough to figure out the exact correspondence or lack thereof
1587 between those responses and a "U foo.c" line (note that Merged, from
1588 join_file, can be either "C foo" or "U foo" depending on the context). */
1589 /* Nonzero if we have seen +updated and not -updated. */
1590 static int updated_seen;
1591 /* Filename from an "fname" tagged response within +updated/-updated. */
1592 static char *updated_fname;
1594 /* This struct is used to hold data when reading the +importmergecmd
1595 and -importmergecmd tags. We put the variables in a struct only
1596 for namespace issues. FIXME: As noted above, we need to develop a
1597 more systematic approach. */
1600 /* Nonzero if we have seen +importmergecmd and not -importmergecmd. */
1602 /* Number of conflicts, from a "conflicts" tagged response. */
1604 /* First merge tag, from a "mergetag1" tagged response. */
1606 /* Second merge tag, from a "mergetag2" tagged response. */
1608 /* Repository, from a "repository" tagged response. */
1612 /* Nonzero if we should arrange to return with a failure exit status. */
1613 static int failure_exit;
1617 * The time stamp of the last file we registered.
1619 static time_t last_register_time;
1622 * The Checksum response gives the checksum for the file transferred
1623 * over by the next Updated, Merged or Patch response. We just store
1624 * it here, and then check it in update_entries.
1627 static int stored_checksum_valid;
1628 static unsigned char stored_checksum[16];
1631 handle_checksum (args, len)
1639 if (stored_checksum_valid)
1640 error (1, 0, "Checksum received before last one was used");
1644 for (i = 0; i < 16; i++)
1650 stored_checksum[i] = (char) strtol (buf, &bufend, 16);
1651 if (bufend != buf + 2)
1655 if (i < 16 || *s != '\0')
1656 error (1, 0, "Invalid Checksum response: `%s'", args);
1658 stored_checksum_valid = 1;
1661 /* Mode that we got in a "Mode" response (malloc'd), or NULL if none. */
1662 static char *stored_mode;
1664 static void handle_mode PROTO ((char *, int));
1667 handle_mode (args, len)
1671 if (stored_mode != NULL)
1672 error (1, 0, "protocol error: duplicate Mode");
1673 stored_mode = xstrdup (args);
1676 /* Nonzero if time was specified in Mod-time. */
1677 static int stored_modtime_valid;
1678 /* Time specified in Mod-time. */
1679 static time_t stored_modtime;
1681 static void handle_mod_time PROTO ((char *, int));
1684 handle_mod_time (args, len)
1688 if (stored_modtime_valid)
1689 error (0, 0, "protocol error: duplicate Mod-time");
1690 stored_modtime = get_date (args, NULL);
1691 if (stored_modtime == (time_t) -1)
1692 error (0, 0, "protocol error: cannot parse date %s", args);
1694 stored_modtime_valid = 1;
1698 * If we receive a patch, but the patch program fails to apply it, we
1699 * want to request the original file. We keep a list of files whose
1700 * patches have failed.
1703 char **failed_patches;
1704 int failed_patches_count;
1706 struct update_entries_data
1710 * We are just getting an Entries line; the local file is
1713 UPDATE_ENTRIES_CHECKIN,
1714 /* We are getting the file contents as well. */
1715 UPDATE_ENTRIES_UPDATE,
1717 * We are getting a patch against the existing local file, not
1718 * an entire new file.
1720 UPDATE_ENTRIES_PATCH,
1722 * We are getting an RCS change text (diff -n output) against
1723 * the existing local file, not an entire new file.
1725 UPDATE_ENTRIES_RCS_DIFF
1729 /* We are replacing an existing file. */
1730 UPDATE_ENTRIES_EXISTING,
1731 /* We are creating a new file. */
1733 /* We don't know whether it is existing or new. */
1734 UPDATE_ENTRIES_EXISTING_OR_NEW
1738 * String to put in the timestamp field or NULL to use the timestamp
1744 /* Update the Entries line for this file. */
1746 update_entries (data_arg, ent_list, short_pathname, filename)
1749 char *short_pathname;
1753 struct update_entries_data *data = (struct update_entries_data *)data_arg;
1758 /* Timestamp field. Always empty according to the protocol. */
1760 char *options = NULL;
1764 char *scratch_entries = NULL;
1767 #ifdef UTIME_EXPECTS_WRITABLE
1768 int change_it_back = 0;
1771 read_line (&entries_line);
1774 * Parse the entries line.
1776 scratch_entries = xstrdup (entries_line);
1778 if (scratch_entries[0] != '/')
1779 error (1, 0, "bad entries line `%s' from server", entries_line);
1780 user = scratch_entries + 1;
1781 if ((cp = strchr (user, '/')) == NULL)
1782 error (1, 0, "bad entries line `%s' from server", entries_line);
1785 if ((cp = strchr (vn, '/')) == NULL)
1786 error (1, 0, "bad entries line `%s' from server", entries_line);
1790 if ((cp = strchr (ts, '/')) == NULL)
1791 error (1, 0, "bad entries line `%s' from server", entries_line);
1794 if ((cp = strchr (options, '/')) == NULL)
1795 error (1, 0, "bad entries line `%s' from server", entries_line);
1799 /* If a slash ends the tag_or_date, ignore everything after it. */
1800 cp = strchr (tag_or_date, '/');
1803 if (*tag_or_date == 'T')
1804 tag = tag_or_date + 1;
1805 else if (*tag_or_date == 'D')
1806 date = tag_or_date + 1;
1808 /* Done parsing the entries line. */
1810 if (data->contents == UPDATE_ENTRIES_UPDATE
1811 || data->contents == UPDATE_ENTRIES_PATCH
1812 || data->contents == UPDATE_ENTRIES_RCS_DIFF)
1818 char *temp_filename;
1822 read_line (&mode_string);
1824 read_line (&size_string);
1825 if (size_string[0] == 'z')
1828 size = atoi (size_string+1);
1833 size = atoi (size_string);
1837 /* Note that checking this separately from writing the file is
1838 a race condition: if the existence or lack thereof of the
1839 file changes between now and the actual calls which
1840 operate on it, we lose. However (a) there are so many
1841 cases, I'm reluctant to try to fix them all, (b) in some
1842 cases the system might not even have a system call which
1843 does the right thing, and (c) it isn't clear this needs to
1845 if (data->existp == UPDATE_ENTRIES_EXISTING
1846 && !isfile (filename))
1847 /* Emit a warning and update the file anyway. */
1848 error (0, 0, "warning: %s unexpectedly disappeared",
1851 if (data->existp == UPDATE_ENTRIES_NEW
1852 && isfile (filename))
1854 /* Emit a warning and refuse to update the file; we don't want
1855 to clobber a user's file. */
1859 /* size should be unsigned, but until we get around to fixing
1860 that, work around it. */
1865 /* This error might be confusing; it isn't really clear to
1866 the user what to do about it. Keep in mind that it has
1867 several causes: (1) something/someone creates the file
1868 during the time that CVS is running, (2) the repository
1869 has two files whose names clash for the client because
1870 of case-insensitivity or similar causes, See 3 for
1871 additional notes. (3) a special case of this is that a
1872 file gets renamed for example from a.c to A.C. A
1873 "cvs update" on a case-insensitive client will get this
1874 error. In this case and in case 2, the filename
1875 (short_pathname) printed in the error message will likely _not_
1876 have the same case as seen by the user in a directory listing.
1877 (4) the client has a file which the server doesn't know
1878 about (e.g. "? foo" file), and that name clashes with a file
1879 the server does know about, (5) classify.c will print the same
1880 message for other reasons.
1882 I hope the above paragraph makes it clear that making this
1883 clearer is not a one-line fix. */
1884 error (0, 0, "move away %s; it is in the way", short_pathname);
1885 if (updated_fname != NULL)
1887 cvs_output ("C ", 0);
1888 cvs_output (updated_fname, 0);
1889 cvs_output ("\n", 1);
1893 discard_file_and_return:
1894 /* Now read and discard the file contents. */
1897 while (nread < usize)
1899 toread = usize - nread;
1900 if (toread > sizeof buf)
1901 toread = sizeof buf;
1903 nread += try_read_from_server (buf, toread);
1909 free (scratch_entries);
1910 free (entries_line);
1912 /* The Mode, Mod-time, and Checksum responses should not carry
1913 over to a subsequent Created (or whatever) response, even
1914 in the error case. */
1915 if (stored_mode != NULL)
1920 stored_modtime_valid = 0;
1921 stored_checksum_valid = 0;
1923 if (updated_fname != NULL)
1925 free (updated_fname);
1926 updated_fname = NULL;
1931 temp_filename = xmalloc (strlen (filename) + 80);
1932 #ifdef USE_VMS_FILENAMES
1933 /* A VMS rename of "blah.dat" to "foo" to implies a
1934 destination of "foo.dat" which is unfortinate for CVS */
1935 sprintf (temp_filename, "%s_new_", filename);
1937 #ifdef _POSIX_NO_TRUNC
1938 sprintf (temp_filename, ".new.%.9s", filename);
1939 #else /* _POSIX_NO_TRUNC */
1940 sprintf (temp_filename, ".new.%s", filename);
1941 #endif /* _POSIX_NO_TRUNC */
1942 #endif /* USE_VMS_FILENAMES */
1944 buf = xmalloc (size);
1946 /* Some systems, like OS/2 and Windows NT, end lines with CRLF
1947 instead of just LF. Format translation is done in the C
1948 library I/O funtions. Here we tell them whether or not to
1949 convert -- if this file is marked "binary" with the RCS -kb
1950 flag, then we don't want to convert, else we do (because
1951 CVS assumes text files by default). */
1954 bin = !(strcmp (options, "-kb"));
1958 if (data->contents == UPDATE_ENTRIES_RCS_DIFF)
1960 /* This is an RCS change text. We just hold the change
1965 "server error: gzip invalid with RCS change text");
1967 read_from_server (buf, size);
1973 fd = CVS_OPEN (temp_filename,
1974 (O_WRONLY | O_CREAT | O_TRUNC
1975 | (bin ? OPEN_BINARY : 0)),
1980 /* I can see a case for making this a fatal error; for
1981 a condition like disk full or network unreachable
1982 (for a file server), carrying on and giving an
1983 error on each file seems unnecessary. But if it is
1984 a permission problem, or some such, then it is
1985 entirely possible that future files will not have
1986 the same problem. */
1987 error (0, errno, "cannot write %s", short_pathname);
1988 goto discard_file_and_return;
1993 read_from_server (buf, size);
1997 if (gunzip_and_write (fd, short_pathname,
1998 (unsigned char *) buf, size))
1999 error (1, 0, "aborting due to compression error");
2001 else if (write (fd, buf, size) != size)
2002 error (1, errno, "writing %s", short_pathname);
2006 error (1, errno, "writing %s", short_pathname);
2009 /* This is after we have read the file from the net (a change
2010 from previous versions, where the server would send us
2011 "M U foo.c" before Update-existing or whatever), but before
2012 we finish writing the file (arguably a bug). The timing
2013 affects a user who wants status info about how far we have
2014 gotten, and also affects whether "U foo.c" appears in addition
2015 to various error messages. */
2016 if (updated_fname != NULL)
2018 cvs_output ("U ", 0);
2019 cvs_output (updated_fname, 0);
2020 cvs_output ("\n", 1);
2021 free (updated_fname);
2027 if (data->contents == UPDATE_ENTRIES_UPDATE)
2029 rename_file (temp_filename, filename);
2031 else if (data->contents == UPDATE_ENTRIES_PATCH)
2033 /* You might think we could just leave Patched out of
2034 Valid-responses and not get this response. However, if
2035 memory serves, the CVS 1.9 server bases this on -u
2036 (update-patches), and there is no way for us to send -u
2037 or not based on whether the server supports "Rcs-diff".
2039 Fall back to transmitting entire files. */
2050 /* Handle UPDATE_ENTRIES_RCS_DIFF. */
2052 if (!isfile (filename))
2053 error (1, 0, "patch original file %s does not exist",
2059 get_file (filename, short_pathname, bin ? FOPEN_BINARY_READ : "r",
2060 &filebuf, &filebufsize, &nread);
2061 /* At this point the contents of the existing file are in
2062 FILEBUF, and the length of the contents is in NREAD.
2063 The contents of the patch from the network are in BUF,
2064 and the length of the patch is in SIZE. */
2066 if (! rcs_change_text (short_pathname, filebuf, nread, buf, size,
2067 &patchedbuf, &patchedlen))
2071 if (stored_checksum_valid)
2073 struct cvs_MD5Context context;
2074 unsigned char checksum[16];
2076 /* We have a checksum. Check it before writing
2077 the file out, so that we don't have to read it
2079 cvs_MD5Init (&context);
2080 cvs_MD5Update (&context,
2081 (unsigned char *) patchedbuf, patchedlen);
2082 cvs_MD5Final (checksum, &context);
2083 if (memcmp (checksum, stored_checksum, 16) != 0)
2086 "checksum failure after patch to %s; will refetch",
2092 stored_checksum_valid = 0;
2099 e = open_file (temp_filename,
2100 bin ? FOPEN_BINARY_WRITE : "w");
2101 if (fwrite (patchedbuf, 1, patchedlen, e) != patchedlen)
2102 error (1, errno, "cannot write %s", temp_filename);
2103 if (fclose (e) == EOF)
2104 error (1, errno, "cannot close %s", temp_filename);
2105 rename_file (temp_filename, filename);
2114 free (temp_filename);
2116 if (stored_checksum_valid && ! patch_failed)
2119 struct cvs_MD5Context context;
2120 unsigned char buf[8192];
2122 unsigned char checksum[16];
2125 * Compute the MD5 checksum. This will normally only be
2126 * used when receiving a patch, so we always compute it
2127 * here on the final file, rather than on the received
2130 * Note that if the file is a text file, we should read it
2131 * here using text mode, so its lines will be terminated the same
2132 * way they were transmitted.
2134 e = CVS_FOPEN (filename, "r");
2136 error (1, errno, "could not open %s", short_pathname);
2138 cvs_MD5Init (&context);
2139 while ((len = fread (buf, 1, sizeof buf, e)) != 0)
2140 cvs_MD5Update (&context, buf, len);
2142 error (1, errno, "could not read %s", short_pathname);
2143 cvs_MD5Final (checksum, &context);
2147 stored_checksum_valid = 0;
2149 if (memcmp (checksum, stored_checksum, 16) != 0)
2151 if (data->contents != UPDATE_ENTRIES_PATCH)
2152 error (1, 0, "checksum failure on %s",
2156 "checksum failure after patch to %s; will refetch",
2165 /* Save this file to retrieve later. */
2166 failed_patches = (char **) xrealloc ((char *) failed_patches,
2167 ((failed_patches_count + 1)
2168 * sizeof (char *)));
2169 failed_patches[failed_patches_count] = xstrdup (short_pathname);
2170 ++failed_patches_count;
2172 stored_checksum_valid = 0;
2176 free (scratch_entries);
2177 free (entries_line);
2183 int status = change_mode (filename, mode_string, 1);
2185 error (0, status, "cannot change mode of %s", short_pathname);
2192 if (stored_mode != NULL)
2194 change_mode (filename, stored_mode, 1);
2199 if (stored_modtime_valid)
2203 memset (&t, 0, sizeof (t));
2204 t.modtime = stored_modtime;
2205 (void) time (&t.actime);
2207 #ifdef UTIME_EXPECTS_WRITABLE
2208 if (!iswritable (filename))
2210 xchmod (filename, 1);
2213 #endif /* UTIME_EXPECTS_WRITABLE */
2215 if (utime (filename, &t) < 0)
2216 error (0, errno, "cannot set time on %s", filename);
2218 #ifdef UTIME_EXPECTS_WRITABLE
2221 xchmod (filename, 0);
2224 #endif /* UTIME_EXPECTS_WRITABLE */
2226 stored_modtime_valid = 0;
2230 * Process the entries line. Do this after we've written the file,
2231 * since we need the timestamp.
2233 if (strcmp (cvs_cmd_name, "export") != 0)
2235 char *local_timestamp;
2236 char *file_timestamp;
2238 (void) time (&last_register_time);
2240 local_timestamp = data->timestamp;
2241 if (local_timestamp == NULL || ts[0] == '+')
2242 file_timestamp = time_stamp (filename);
2244 file_timestamp = NULL;
2247 * These special version numbers signify that it is not up to
2248 * date. Create a dummy timestamp which will never compare
2249 * equal to the timestamp of the file.
2251 if (vn[0] == '\0' || strcmp (vn, "0") == 0 || vn[0] == '-')
2252 local_timestamp = "dummy timestamp";
2253 else if (local_timestamp == NULL)
2255 local_timestamp = file_timestamp;
2257 /* Checking for cvs_cmd_name of "commit" doesn't seem like
2258 the cleanest way to handle this, but it seem to roughly
2259 parallel what the :local: code which calls
2260 mark_up_to_date ends up amounting to. Some day, should
2261 think more about what the Checked-in response means
2262 vis-a-vis both Entries and Base and clarify
2263 cvsclient.texi accordingly. */
2265 if (!strcmp (cvs_cmd_name, "commit"))
2266 mark_up_to_date (filename);
2269 Register (ent_list, filename, vn, local_timestamp,
2270 options, tag, date, ts[0] == '+' ? file_timestamp : NULL);
2273 free (file_timestamp);
2276 free (scratch_entries);
2277 free (entries_line);
2281 handle_checked_in (args, len)
2285 struct update_entries_data dat;
2286 dat.contents = UPDATE_ENTRIES_CHECKIN;
2287 dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2288 dat.timestamp = NULL;
2289 call_in_directory (args, update_entries, (char *)&dat);
2293 handle_new_entry (args, len)
2297 struct update_entries_data dat;
2298 dat.contents = UPDATE_ENTRIES_CHECKIN;
2299 dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2300 dat.timestamp = "dummy timestamp from new-entry";
2301 call_in_directory (args, update_entries, (char *)&dat);
2305 handle_updated (args, len)
2309 struct update_entries_data dat;
2310 dat.contents = UPDATE_ENTRIES_UPDATE;
2311 dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2312 dat.timestamp = NULL;
2313 call_in_directory (args, update_entries, (char *)&dat);
2316 static void handle_created PROTO((char *, int));
2319 handle_created (args, len)
2323 struct update_entries_data dat;
2324 dat.contents = UPDATE_ENTRIES_UPDATE;
2325 dat.existp = UPDATE_ENTRIES_NEW;
2326 dat.timestamp = NULL;
2327 call_in_directory (args, update_entries, (char *)&dat);
2330 static void handle_update_existing PROTO((char *, int));
2333 handle_update_existing (args, len)
2337 struct update_entries_data dat;
2338 dat.contents = UPDATE_ENTRIES_UPDATE;
2339 dat.existp = UPDATE_ENTRIES_EXISTING;
2340 dat.timestamp = NULL;
2341 call_in_directory (args, update_entries, (char *)&dat);
2345 handle_merged (args, len)
2349 struct update_entries_data dat;
2350 dat.contents = UPDATE_ENTRIES_UPDATE;
2351 /* Think this could be UPDATE_ENTRIES_EXISTING, but just in case... */
2352 dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2353 dat.timestamp = "Result of merge";
2354 call_in_directory (args, update_entries, (char *)&dat);
2358 handle_patched (args, len)
2362 struct update_entries_data dat;
2363 dat.contents = UPDATE_ENTRIES_PATCH;
2364 /* Think this could be UPDATE_ENTRIES_EXISTING, but just in case... */
2365 dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2366 dat.timestamp = NULL;
2367 call_in_directory (args, update_entries, (char *)&dat);
2371 handle_rcs_diff (args, len)
2375 struct update_entries_data dat;
2376 dat.contents = UPDATE_ENTRIES_RCS_DIFF;
2377 /* Think this could be UPDATE_ENTRIES_EXISTING, but just in case... */
2378 dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2379 dat.timestamp = NULL;
2380 call_in_directory (args, update_entries, (char *)&dat);
2384 remove_entry (data, ent_list, short_pathname, filename)
2387 char *short_pathname;
2390 Scratch_Entry (ent_list, filename);
2394 handle_remove_entry (args, len)
2398 call_in_directory (args, remove_entry, (char *)NULL);
2402 remove_entry_and_file (data, ent_list, short_pathname, filename)
2405 char *short_pathname;
2408 Scratch_Entry (ent_list, filename);
2409 /* Note that we don't ignore existence_error's here. The server
2410 should be sending Remove-entry rather than Removed in cases
2411 where the file does not exist. And if the user removes the
2412 file halfway through a cvs command, we should be printing an
2414 if (unlink_file (filename) < 0)
2415 error (0, errno, "unable to remove %s", short_pathname);
2419 handle_removed (args, len)
2423 call_in_directory (args, remove_entry_and_file, (char *)NULL);
2426 /* Is this the top level (directory containing CVSROOT)? */
2428 is_cvsroot_level (pathname)
2431 if (strcmp (toplevel_repos, current_parsed_root->directory) != 0)
2434 return strchr (pathname, '/') == NULL;
2438 set_static (data, ent_list, short_pathname, filename)
2441 char *short_pathname;
2445 fp = open_file (CVSADM_ENTSTAT, "w+");
2446 if (fclose (fp) == EOF)
2447 error (1, errno, "cannot close %s", CVSADM_ENTSTAT);
2451 handle_set_static_directory (args, len)
2455 if (strcmp (cvs_cmd_name, "export") == 0)
2457 /* Swallow the repository. */
2461 call_in_directory (args, set_static, (char *)NULL);
2465 clear_static (data, ent_list, short_pathname, filename)
2468 char *short_pathname;
2471 if (unlink_file (CVSADM_ENTSTAT) < 0 && ! existence_error (errno))
2472 error (1, errno, "cannot remove file %s", CVSADM_ENTSTAT);
2476 handle_clear_static_directory (pathname, len)
2480 if (strcmp (cvs_cmd_name, "export") == 0)
2482 /* Swallow the repository. */
2487 if (is_cvsroot_level (pathname))
2490 * Top level (directory containing CVSROOT). This seems to normally
2491 * lack a CVS directory, so don't try to create files in it.
2495 call_in_directory (pathname, clear_static, (char *)NULL);
2499 set_sticky (data, ent_list, short_pathname, filename)
2502 char *short_pathname;
2508 read_line (&tagspec);
2510 /* FIXME-update-dir: error messages should include the directory. */
2511 f = CVS_FOPEN (CVSADM_TAG, "w+");
2514 /* Making this non-fatal is a bit of a kludge (see dirs2
2515 in testsuite). A better solution would be to avoid having
2516 the server tell us about a directory we shouldn't be doing
2517 anything with anyway (e.g. by handling directory
2518 addition/removal better). */
2519 error (0, errno, "cannot open %s", CVSADM_TAG);
2523 if (fprintf (f, "%s\n", tagspec) < 0)
2524 error (1, errno, "writing %s", CVSADM_TAG);
2525 if (fclose (f) == EOF)
2526 error (1, errno, "closing %s", CVSADM_TAG);
2531 handle_set_sticky (pathname, len)
2535 if (strcmp (cvs_cmd_name, "export") == 0)
2537 /* Swallow the repository. */
2539 /* Swallow the tag line. */
2543 if (is_cvsroot_level (pathname))
2546 * Top level (directory containing CVSROOT). This seems to normally
2547 * lack a CVS directory, so don't try to create files in it.
2550 /* Swallow the repository. */
2552 /* Swallow the tag line. */
2557 call_in_directory (pathname, set_sticky, (char *)NULL);
2561 clear_sticky (data, ent_list, short_pathname, filename)
2564 char *short_pathname;
2567 if (unlink_file (CVSADM_TAG) < 0 && ! existence_error (errno))
2568 error (1, errno, "cannot remove %s", CVSADM_TAG);
2572 handle_clear_sticky (pathname, len)
2576 if (strcmp (cvs_cmd_name, "export") == 0)
2578 /* Swallow the repository. */
2583 if (is_cvsroot_level (pathname))
2586 * Top level (directory containing CVSROOT). This seems to normally
2587 * lack a CVS directory, so don't try to create files in it.
2592 call_in_directory (pathname, clear_sticky, (char *)NULL);
2596 static void template PROTO ((char *, List *, char *, char *));
2599 template (data, ent_list, short_pathname, filename)
2602 char *short_pathname;
2605 char *buf = xmalloc ( strlen ( short_pathname )
2606 + strlen ( CVSADM_TEMPLATE )
2608 sprintf ( buf, "%s/%s", short_pathname, CVSADM_TEMPLATE );
2609 read_counted_file ( CVSADM_TEMPLATE, buf );
2613 static void handle_template PROTO ((char *, int));
2616 handle_template (pathname, len)
2620 call_in_directory (pathname, template, NULL);
2627 struct save_dir *next;
2630 struct save_dir *prune_candidates;
2633 add_prune_candidate (dir)
2638 if ((dir[0] == '.' && dir[1] == '\0')
2639 || (prune_candidates != NULL
2640 && strcmp (dir, prune_candidates->dir) == 0))
2642 p = (struct save_dir *) xmalloc (sizeof (struct save_dir));
2643 p->dir = xstrdup (dir);
2644 p->next = prune_candidates;
2645 prune_candidates = p;
2648 static void process_prune_candidates PROTO((void));
2651 process_prune_candidates ()
2656 if (toplevel_wd != NULL)
2658 if (CVS_CHDIR (toplevel_wd) < 0)
2659 error (1, errno, "could not chdir to %s", toplevel_wd);
2661 for (p = prune_candidates; p != NULL; )
2663 if (isemptydir (p->dir, 1))
2667 if (unlink_file_dir (p->dir) < 0)
2668 error (0, errno, "cannot remove %s", p->dir);
2669 b = strrchr (p->dir, '/');
2671 Subdir_Deregister ((List *) NULL, (char *) NULL, p->dir);
2675 Subdir_Deregister ((List *) NULL, p->dir, b + 1);
2683 prune_candidates = NULL;
2686 /* Send a Repository line. */
2688 static char *last_repos;
2689 static char *last_update_dir;
2691 static void send_repository PROTO((const char *, const char *, const char *));
2694 send_repository (dir, repos, update_dir)
2697 const char *update_dir;
2701 /* FIXME: this is probably not the best place to check; I wish I
2702 * knew where in here's callers to really trap this bug. To
2703 * reproduce the bug, just do this:
2707 * cvs -d some_repos update foo
2709 * Poof, CVS seg faults and dies! It's because it's trying to
2710 * send a NULL string to the server but dies in send_to_server.
2711 * That string was supposed to be the repository, but it doesn't
2712 * get set because there's no CVSADM dir, and somehow it's not
2713 * getting set from the -d argument either... ?
2717 /* Lame error. I want a real fix but can't stay up to track
2718 this down right now. */
2719 error (1, 0, "no repository");
2722 if (update_dir == NULL || update_dir[0] == '\0')
2725 if (last_repos != NULL
2726 && strcmp (repos, last_repos) == 0
2727 && last_update_dir != NULL
2728 && strcmp (update_dir, last_update_dir) == 0)
2729 /* We've already sent it. */
2732 if (client_prune_dirs)
2733 add_prune_candidate (update_dir);
2735 /* Add a directory name to the list of those sent to the
2737 if (update_dir && (*update_dir != '\0')
2738 && (strcmp (update_dir, ".") != 0)
2739 && (findnode (dirs_sent_to_server, update_dir) == NULL))
2743 n->type = NT_UNKNOWN;
2744 n->key = xstrdup (update_dir);
2747 if (addnode (dirs_sent_to_server, n))
2748 error (1, 0, "cannot add directory %s to list", n->key);
2751 /* 80 is large enough for any of CVSADM_*. */
2752 adm_name = xmalloc (strlen (dir) + 80);
2754 send_to_server ("Directory ", 0);
2756 /* Send the directory name. I know that this
2757 sort of duplicates code elsewhere, but each
2758 case seems slightly different... */
2760 const char *p = update_dir;
2763 assert (*p != '\012');
2767 send_to_server (buf, 1);
2772 send_to_server (buf, 1);
2777 send_to_server ("\012", 1);
2778 send_to_server (repos, 0);
2779 send_to_server ("\012", 1);
2781 if (supported_request ("Static-directory"))
2786 strcat (adm_name, dir);
2787 strcat (adm_name, "/");
2789 strcat (adm_name, CVSADM_ENTSTAT);
2790 if (isreadable (adm_name))
2792 send_to_server ("Static-directory\012", 0);
2795 if (supported_request ("Sticky"))
2799 strcpy (adm_name, CVSADM_TAG);
2801 sprintf (adm_name, "%s/%s", dir, CVSADM_TAG);
2803 f = CVS_FOPEN (adm_name, "r");
2806 if (! existence_error (errno))
2807 error (1, errno, "reading %s", adm_name);
2813 send_to_server ("Sticky ", 0);
2814 while (fgets (line, sizeof (line), f) != NULL)
2816 send_to_server (line, 0);
2817 nl = strchr (line, '\n');
2822 send_to_server ("\012", 1);
2823 if (fclose (f) == EOF)
2824 error (0, errno, "closing %s", adm_name);
2828 if (last_repos != NULL)
2830 if (last_update_dir != NULL)
2831 free (last_update_dir);
2832 last_repos = xstrdup (repos);
2833 last_update_dir = xstrdup (update_dir);
2836 /* Send a Repository line and set toplevel_repos. */
2839 send_a_repository (dir, repository, update_dir_in)
2841 const char *repository;
2842 const char *update_dir_in;
2844 char *update_dir = xstrdup (update_dir_in);
2846 if (toplevel_repos == NULL && repository != NULL)
2848 if (update_dir[0] == '\0'
2849 || (update_dir[0] == '.' && update_dir[1] == '\0'))
2850 toplevel_repos = xstrdup (repository);
2854 * Get the repository from a CVS/Repository file if update_dir
2855 * is absolute. This is not correct in general, because
2856 * the CVS/Repository file might not be the top-level one.
2857 * This is for cases like "cvs update /foo/bar" (I'm not
2858 * sure it matters what toplevel_repos we get, but it does
2859 * matter that we don't hit the "internal error" code below).
2861 if (update_dir[0] == '/')
2862 toplevel_repos = Name_Repository (update_dir, update_dir);
2866 * Guess the repository of that directory by looking at a
2867 * subdirectory and removing as many pathname components
2868 * as are in update_dir. I think that will always (or at
2869 * least almost always) be 1.
2871 * So this deals with directories which have been
2872 * renamed, though it doesn't necessarily deal with
2873 * directories which have been put inside other
2874 * directories (and cvs invoked on the containing
2875 * directory). I'm not sure the latter case needs to
2878 * 21 Aug 1998: Well, Mr. Above-Comment-Writer, it
2879 * does need to work after all. When we are using the
2880 * client in a multi-cvsroot environment, it will be
2881 * fairly common that we have the above case (e.g.,
2882 * cwd checked out from one repository but
2883 * subdirectory checked out from another). We can't
2884 * assume that by walking up a directory in our wd we
2885 * necessarily walk up a directory in the repository.
2888 * This gets toplevel_repos wrong for "cvs update ../foo"
2889 * but I'm not sure toplevel_repos matters in that case.
2892 int repository_len, update_dir_len;
2894 strip_trailing_slashes (update_dir);
2896 repository_len = strlen (repository);
2897 update_dir_len = strlen (update_dir);
2899 /* Try to remove the path components in UPDATE_DIR
2900 from REPOSITORY. If the path elements don't exist
2901 in REPOSITORY, or the removal of those path
2902 elements mean that we "step above"
2903 current_parsed_root->directory, set toplevel_repos to
2904 current_parsed_root->directory. */
2905 if ((repository_len > update_dir_len)
2906 && (strcmp (repository + repository_len - update_dir_len,
2908 /* TOPLEVEL_REPOS shouldn't be above current_parsed_root->directory */
2909 && ((size_t)(repository_len - update_dir_len)
2910 > strlen (current_parsed_root->directory)))
2912 /* The repository name contains UPDATE_DIR. Set
2913 toplevel_repos to the repository name without
2916 toplevel_repos = xmalloc (repository_len - update_dir_len);
2917 /* Note that we don't copy the trailing '/'. */
2918 strncpy (toplevel_repos, repository,
2919 repository_len - update_dir_len - 1);
2920 toplevel_repos[repository_len - update_dir_len - 1] = '\0';
2924 toplevel_repos = xstrdup (current_parsed_root->directory);
2930 send_repository (dir, repository, update_dir);
2936 /* The "expanded" modules. */
2937 static int modules_count;
2938 static int modules_allocated;
2939 static char **modules_vector;
2942 handle_module_expansion (args, len)
2946 if (modules_vector == NULL)
2948 modules_allocated = 1; /* Small for testing */
2949 modules_vector = (char **) xmalloc
2950 (modules_allocated * sizeof (modules_vector[0]));
2952 else if (modules_count >= modules_allocated)
2954 modules_allocated *= 2;
2955 modules_vector = (char **) xrealloc
2956 ((char *) modules_vector,
2957 modules_allocated * sizeof (modules_vector[0]));
2959 modules_vector[modules_count] = xmalloc (strlen (args) + 1);
2960 strcpy (modules_vector[modules_count], args);
2964 /* Original, not "expanded" modules. */
2965 static int module_argc;
2966 static char **module_argv;
2969 client_expand_modules (argc, argv, local)
2978 module_argv = (char **) xmalloc ((argc + 1) * sizeof (module_argv[0]));
2979 for (i = 0; i < argc; ++i)
2980 module_argv[i] = xstrdup (argv[i]);
2981 module_argv[argc] = NULL;
2983 for (i = 0; i < argc; ++i)
2985 send_a_repository ("", current_parsed_root->directory, "");
2987 send_to_server ("expand-modules\012", 0);
2989 errs = get_server_responses ();
2990 if (last_repos != NULL)
2993 if (last_update_dir != NULL)
2994 free (last_update_dir);
2995 last_update_dir = NULL;
2997 error (errs, 0, "cannot expand modules");
3001 client_send_expansions (local, where, build_dirs)
3009 /* Send the original module names. The "expanded" module name might
3010 not be suitable as an argument to a co request (e.g. it might be
3011 the result of a -d argument in the modules file). It might be
3012 cleaner if we genuinely expanded module names, all the way to a
3013 local directory and repository, but that isn't the way it works
3015 send_file_names (module_argc, module_argv, 0);
3017 for (i = 0; i < modules_count; ++i)
3019 argv[0] = where ? where : modules_vector[i];
3020 if (isfile (argv[0]))
3021 send_files (1, argv, local, 0, build_dirs ? SEND_BUILD_DIRS : 0);
3023 send_a_repository ("", current_parsed_root->directory, "");
3027 client_nonexpanded_setup ()
3029 send_a_repository ("", current_parsed_root->directory, "");
3032 /* Receive a cvswrappers line from the server; it must be a line
3033 containing an RCS option (e.g., "*.exe -k 'b'").
3035 Note that this doesn't try to handle -t/-f options (which are a
3036 whole separate issue which noone has thought much about, as far
3039 We need to know the keyword expansion mode so we know whether to
3040 read the file in text or binary mode. */
3043 handle_wrapper_rcs_option (args, len)
3049 /* Enforce the notes in cvsclient.texi about how the response is not
3050 as free-form as it looks. */
3051 p = strchr (args, ' ');
3059 if (strchr (p, '\'') == NULL)
3062 /* Add server-side cvswrappers line to our wrapper list. */
3066 error (0, errno, "protocol error: ignoring invalid wrappers %s", args);
3071 handle_m (args, len)
3075 /* In the case where stdout and stderr point to the same place,
3076 fflushing stderr will make output happen in the correct order.
3077 Often stderr will be line-buffered and this won't be needed,
3078 but not always (is that true? I think the comment is probably
3079 based on being confused between default buffering between
3080 stdout and stderr. But I'm not sure). */
3082 fwrite (args, len, sizeof (*args), stdout);
3083 putc ('\n', stdout);
3086 static void handle_mbinary PROTO ((char *, int));
3089 handle_mbinary (args, len)
3100 /* See comment at handle_m about (non)flush of stderr. */
3103 read_line (&size_string);
3104 size = atoi (size_string);
3107 /* OK, now get all the data. The algorithm here is that we read
3108 as much as the network wants to give us in
3109 try_read_from_server, and then we output it all, and then
3110 repeat, until we get all the data. */
3112 while (totalread < size)
3114 toread = size - totalread;
3115 if (toread > sizeof buf)
3116 toread = sizeof buf;
3118 nread = try_read_from_server (buf, toread);
3119 cvs_output_binary (buf, nread);
3125 handle_e (args, len)
3129 /* In the case where stdout and stderr point to the same place,
3130 fflushing stdout will make output happen in the correct order. */
3132 fwrite (args, len, sizeof (*args), stderr);
3133 putc ('\n', stderr);
3138 handle_f (args, len)
3145 static void handle_mt PROTO ((char *, int));
3148 handle_mt (args, len)
3156 /* See comment at handle_m for more details. */
3159 p = strchr (args, ' ');
3171 if (strcmp (tag, "+updated") == 0)
3173 else if (strcmp (tag, "+importmergecmd") == 0)
3174 importmergecmd.seen = 1;
3177 if (strcmp (tag, "-updated") == 0)
3179 else if (strcmp (tag, "-importmergecmd") == 0)
3183 /* Now that we have gathered the information, we can
3184 output the suggested merge command. */
3186 if (importmergecmd.conflicts == 0
3187 || importmergecmd.mergetag1 == NULL
3188 || importmergecmd.mergetag2 == NULL
3189 || importmergecmd.repository == NULL)
3192 "invalid server: incomplete importmergecmd tags");
3196 sprintf (buf, "\n%d conflicts created by this import.\n",
3197 importmergecmd.conflicts);
3198 cvs_output (buf, 0);
3199 cvs_output ("Use the following command to help the merge:\n\n",
3201 cvs_output ("\t", 1);
3202 cvs_output (program_name, 0);
3203 if (CVSroot_cmdline != NULL)
3205 cvs_output (" -d ", 0);
3206 cvs_output (CVSroot_cmdline, 0);
3208 cvs_output (" checkout -j", 0);
3209 cvs_output (importmergecmd.mergetag1, 0);
3210 cvs_output (" -j", 0);
3211 cvs_output (importmergecmd.mergetag2, 0);
3212 cvs_output (" ", 1);
3213 cvs_output (importmergecmd.repository, 0);
3214 cvs_output ("\n\n", 0);
3216 /* Clear the static variables so that everything is
3217 ready for any subsequent importmergecmd tag. */
3218 importmergecmd.conflicts = 0;
3219 free (importmergecmd.mergetag1);
3220 importmergecmd.mergetag1 = NULL;
3221 free (importmergecmd.mergetag2);
3222 importmergecmd.mergetag2 = NULL;
3223 free (importmergecmd.repository);
3224 importmergecmd.repository = NULL;
3226 importmergecmd.seen = 0;
3232 if (strcmp (tag, "fname") == 0)
3234 if (updated_fname != NULL)
3236 /* Output the previous message now. This can happen
3237 if there was no Update-existing or other such
3238 response, due to the -n global option. */
3239 cvs_output ("U ", 0);
3240 cvs_output (updated_fname, 0);
3241 cvs_output ("\n", 1);
3242 free (updated_fname);
3244 updated_fname = xstrdup (text);
3246 /* Swallow all other tags. Either they are extraneous
3247 or they reflect future extensions that we can
3250 else if (importmergecmd.seen)
3252 if (strcmp (tag, "conflicts") == 0)
3253 importmergecmd.conflicts = atoi (text);
3254 else if (strcmp (tag, "mergetag1") == 0)
3255 importmergecmd.mergetag1 = xstrdup (text);
3256 else if (strcmp (tag, "mergetag2") == 0)
3257 importmergecmd.mergetag2 = xstrdup (text);
3258 else if (strcmp (tag, "repository") == 0)
3259 importmergecmd.repository = xstrdup (text);
3260 /* Swallow all other tags. Either they are text for
3261 which we are going to print our own version when we
3262 see -importmergecmd, or they are future extensions
3263 we can safely ignore. */
3265 else if (strcmp (tag, "newline") == 0)
3267 else if (text != NULL)
3268 printf ("%s", text);
3272 #endif /* CLIENT_SUPPORT */
3273 #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
3275 /* This table must be writeable if the server code is included. */
3276 struct response responses[] =
3278 #ifdef CLIENT_SUPPORT
3279 #define RSP_LINE(n, f, t, s) {n, f, t, s}
3280 #else /* ! CLIENT_SUPPORT */
3281 #define RSP_LINE(n, f, t, s) {n, s}
3282 #endif /* CLIENT_SUPPORT */
3284 RSP_LINE("ok", handle_ok, response_type_ok, rs_essential),
3285 RSP_LINE("error", handle_error, response_type_error, rs_essential),
3286 RSP_LINE("Valid-requests", handle_valid_requests, response_type_normal,
3288 RSP_LINE("Checked-in", handle_checked_in, response_type_normal,
3290 RSP_LINE("New-entry", handle_new_entry, response_type_normal, rs_optional),
3291 RSP_LINE("Checksum", handle_checksum, response_type_normal, rs_optional),
3292 RSP_LINE("Copy-file", handle_copy_file, response_type_normal, rs_optional),
3293 RSP_LINE("Updated", handle_updated, response_type_normal, rs_essential),
3294 RSP_LINE("Created", handle_created, response_type_normal, rs_optional),
3295 RSP_LINE("Update-existing", handle_update_existing, response_type_normal,
3297 RSP_LINE("Merged", handle_merged, response_type_normal, rs_essential),
3298 RSP_LINE("Patched", handle_patched, response_type_normal, rs_optional),
3299 RSP_LINE("Rcs-diff", handle_rcs_diff, response_type_normal, rs_optional),
3300 RSP_LINE("Mode", handle_mode, response_type_normal, rs_optional),
3301 RSP_LINE("Mod-time", handle_mod_time, response_type_normal, rs_optional),
3302 RSP_LINE("Removed", handle_removed, response_type_normal, rs_essential),
3303 RSP_LINE("Remove-entry", handle_remove_entry, response_type_normal,
3305 RSP_LINE("Set-static-directory", handle_set_static_directory,
3306 response_type_normal,
3308 RSP_LINE("Clear-static-directory", handle_clear_static_directory,
3309 response_type_normal,
3311 RSP_LINE("Set-sticky", handle_set_sticky, response_type_normal,
3313 RSP_LINE("Clear-sticky", handle_clear_sticky, response_type_normal,
3315 RSP_LINE("Template", handle_template, response_type_normal,
3317 RSP_LINE("Notified", handle_notified, response_type_normal, rs_optional),
3318 RSP_LINE("Module-expansion", handle_module_expansion, response_type_normal,
3320 RSP_LINE("Wrapper-rcsOption", handle_wrapper_rcs_option,
3321 response_type_normal,
3323 RSP_LINE("M", handle_m, response_type_normal, rs_essential),
3324 RSP_LINE("Mbinary", handle_mbinary, response_type_normal, rs_optional),
3325 RSP_LINE("E", handle_e, response_type_normal, rs_essential),
3326 RSP_LINE("F", handle_f, response_type_normal, rs_optional),
3327 RSP_LINE("MT", handle_mt, response_type_normal, rs_optional),
3328 /* Possibly should be response_type_error. */
3329 RSP_LINE(NULL, NULL, response_type_normal, rs_essential)
3334 #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
3335 #ifdef CLIENT_SUPPORT
3338 * If LEN is 0, then send_to_server() computes string's length itself.
3340 * Therefore, pass the real length when transmitting data that might
3344 send_to_server (str, len)
3353 buf_output (to_server, str, len);
3355 /* There is no reason not to send data to the server, so do it
3356 whenever we've accumulated enough information in the buffer to
3357 make it worth sending. */
3359 if (nbytes >= 2 * BUFFER_DATA_SIZE)
3363 status = buf_send_output (to_server);
3365 error (1, status, "error writing to server");
3370 /* Read up to LEN bytes from the server. Returns actual number of
3371 bytes read, which will always be at least one; blocks if there is
3372 no data available at all. Gives a fatal error on EOF or error. */
3374 try_read_from_server (buf, len)
3381 status = buf_read_data (from_server, len, &data, &nread);
3386 "end of file from server (consult above messages if any)");
3387 else if (status == -2)
3388 error (1, 0, "out of memory");
3390 error (1, status, "reading from server");
3393 memcpy (buf, data, nread);
3399 * Read LEN bytes from the server or die trying.
3402 read_from_server (buf, len)
3409 red += try_read_from_server (buf + red, len - red);
3416 * Get some server responses and process them. Returns nonzero for
3417 * error, 0 for success. */
3419 get_server_responses ()
3421 struct response *rs;
3427 len = read_line (&cmd);
3428 for (rs = responses; rs->name != NULL; ++rs)
3429 if (strncmp (cmd, rs->name, strlen (rs->name)) == 0)
3431 int cmdlen = strlen (rs->name);
3432 if (cmd[cmdlen] == '\0')
3434 else if (cmd[cmdlen] == ' ')
3438 * The first len characters match, but it's a different
3439 * response. e.g. the response is "oklahoma" but we
3443 (*rs->func) (cmd + cmdlen, len - cmdlen);
3446 if (rs->name == NULL)
3447 /* It's OK to print just to the first '\0'. */
3448 /* We might want to handle control characters and the like
3449 in some other way other than just sending them to stdout.
3450 One common reason for this error is if people use :ext:
3451 with a version of rsh which is doing CRLF translation or
3452 something, and so the client gets "ok^M" instead of "ok".
3453 Right now that will tend to print part of this error
3454 message over the other part of it. It seems like we could
3455 do better (either in general, by quoting or omitting all
3456 control characters, and/or specifically, by detecting the CRLF
3457 case and printing a specific error message). */
3459 "warning: unrecognized response `%s' from cvs server",
3462 } while (rs->type == response_type_normal);
3464 if (updated_fname != NULL)
3466 /* Output the previous message now. This can happen
3467 if there was no Update-existing or other such
3468 response, due to the -n global option. */
3469 cvs_output ("U ", 0);
3470 cvs_output (updated_fname, 0);
3471 cvs_output ("\n", 1);
3472 free (updated_fname);
3473 updated_fname = NULL;
3476 if (rs->type == response_type_error)
3485 /* Get the responses and then close the connection. */
3488 * Flag var; we'll set it in start_server() and not one of its
3489 * callees, such as start_rsh_server(). This means that there might
3490 * be a small window between the starting of the server and the
3491 * setting of this var, but all the code in that window shouldn't care
3492 * because it's busy checking return values to see if the server got
3493 * started successfully anyway.
3495 int server_started = 0;
3498 get_responses_and_close ()
3500 int errs = get_server_responses ();
3503 /* The following is necessary when working with multiple cvsroots, at least
3504 * with commit. It used to be buried nicely in do_deferred_progs() before
3505 * that function was removed. I suspect it wouldn't be necessary if
3506 * call_in_directory() saved its working directory via save_cwd() before
3507 * changing its directory and restored the saved working directory via
3508 * restore_cwd() before exiting. Of course, calling CVS_CHDIR only once,
3509 * here, may be more efficient.
3511 if( toplevel_wd != NULL )
3513 if( CVS_CHDIR( toplevel_wd ) < 0 )
3514 error( 1, errno, "could not chdir to %s", toplevel_wd );
3517 if (client_prune_dirs)
3518 process_prune_candidates ();
3520 /* First we shut down TO_SERVER. That tells the server that its input is
3521 * finished. It then shuts down the buffer it is sending to us, at which
3522 * point our shut down of FROM_SERVER will complete.
3525 status = buf_shutdown (to_server);
3527 error (0, status, "shutting down buffer to server");
3528 buf_free (to_server);
3531 status = buf_shutdown (from_server);
3533 error (0, status, "shutting down buffer from server");
3534 buf_free (from_server);
3538 /* see if we need to sleep before returning to avoid time-stamp races */
3539 if (last_register_time)
3541 sleep_past (last_register_time);
3547 #ifndef NO_EXT_METHOD
3548 static void start_rsh_server PROTO((cvsroot_t *, struct buffer **, struct buffer **));
3552 supported_request (name)
3557 for (rq = requests; rq->name; rq++)
3558 if (!strcmp (rq->name, name))
3559 return (rq->flags & RQ_SUPPORTED) != 0;
3560 error (1, 0, "internal error: testing support for unknown option?");
3567 #if defined (AUTH_CLIENT_SUPPORT) || defined (HAVE_KERBEROS) || defined (HAVE_GSSAPI)
3568 static struct hostent *init_sockaddr PROTO ((struct sockaddr_in *, char *,
3571 static struct hostent *
3572 init_sockaddr (name, hostname, port)
3573 struct sockaddr_in *name;
3577 struct hostent *hostinfo;
3578 unsigned short shortport = port;
3580 memset (name, 0, sizeof (*name));
3581 name->sin_family = AF_INET;
3582 name->sin_port = htons (shortport);
3583 hostinfo = gethostbyname (hostname);
3584 if (hostinfo == NULL)
3586 fprintf (stderr, "Unknown host %s.\n", hostname);
3589 name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
3595 /* Generic function to do port number lookup tasks.
3597 * In order of precedence, will return:
3598 * getenv (envname), if defined
3599 * getservbyname (portname), if defined
3603 get_port_number (envname, portname, defaultport)
3604 const char *envname;
3605 const char *portname;
3611 if (envname && (port_s = getenv (envname)))
3613 int port = atoi (port_s);
3616 error (0, 0, "%s must be a positive integer! If you", envname);
3617 error (0, 0, "are trying to force a connection via rsh, please");
3618 error (0, 0, "put \":server:\" at the beginning of your CVSROOT");
3619 error (1, 0, "variable.");
3623 else if (portname && (s = getservbyname (portname, "tcp")))
3624 return ntohs (s->s_port);
3631 /* get the port number for a client to connect to based on the port
3632 * and method of a cvsroot_t.
3634 * we do this here instead of in parse_cvsroot so that we can keep network
3635 * code confined to a localized area and also to delay the lookup until the
3636 * last possible moment so it remains possible to run cvs client commands that
3637 * skip opening connections to the server (i.e. skip network operations
3640 * and yes, I know none of the commands do that now, but here's to planning
3641 * for the future, eh? cheers.
3643 * FIXME - We could cache the port lookup safely right now as we never change
3644 * it for a single root on the fly, but we'd have to un'const some other
3645 * functions - REMOVE_FIXME? This may be unecessary. We're talking about,
3646 * what, usually one, sometimes two lookups of the port per invocation. I
3647 * think twice is by far the rarer of the two cases - only the login function
3648 * will need to do it to save the canonical CVSROOT. -DRP
3651 get_cvs_port_number (root)
3652 const cvsroot_t *root;
3655 if (root->port) return root->port;
3657 switch (root->method)
3660 case gserver_method:
3661 # endif /* HAVE_GSSAPI */
3662 # ifdef AUTH_CLIENT_SUPPORT
3663 case pserver_method:
3664 # endif /* AUTH_CLIENT_SUPPORT */
3665 # if defined (AUTH_CLIENT_SUPPORT) || defined (HAVE_GSSAPI)
3666 return get_port_number ("CVS_CLIENT_PORT", "cvspserver", CVS_AUTH_PORT);
3667 # endif /* defined (AUTH_CLIENT_SUPPORT) || defined (HAVE_GSSAPI) */
3668 # ifdef HAVE_KERBEROS
3669 case kserver_method:
3670 return get_port_number ("CVS_CLIENT_PORT", "cvs", CVS_PORT);
3671 # endif /* HAVE_KERBEROS */
3673 error(1, EINVAL, "internal error: get_cvs_port_number called for invalid connection method (%s)",
3674 method_names[root->method]);
3684 make_bufs_from_fds (tofd, fromfd, child_pid, to_server, from_server, is_sock)
3688 struct buffer **to_server;
3689 struct buffer **from_server;
3693 FILE *from_server_fp;
3695 # ifdef NO_SOCKET_TO_FD
3698 assert (tofd == fromfd);
3699 *to_server = socket_buffer_initialize (tofd, 0,
3700 (BUFMEMERRPROC) NULL);
3701 *from_server = socket_buffer_initialize (tofd, 1,
3702 (BUFMEMERRPROC) NULL);
3705 # endif /* NO_SOCKET_TO_FD */
3707 /* todo: some OS's don't need these calls... */
3708 close_on_exec (tofd);
3709 close_on_exec (fromfd);
3711 /* SCO 3 and AIX have a nasty bug in the I/O libraries which precludes
3712 fdopening the same file descriptor twice, so dup it if it is the
3716 fromfd = dup (tofd);
3718 error (1, errno, "cannot dup net connection");
3721 /* These will use binary mode on systems which have it. */
3723 * Also, we know that from_server is shut down second, so we pass
3724 * child_pid in there. In theory, it should be stored in both
3725 * buffers with a ref count...
3727 to_server_fp = fdopen (tofd, FOPEN_BINARY_WRITE);
3728 if (to_server_fp == NULL)
3729 error (1, errno, "cannot fdopen %d for write", tofd);
3730 *to_server = stdio_buffer_initialize (to_server_fp, 0, 0,
3731 (BUFMEMERRPROC) NULL);
3733 from_server_fp = fdopen (fromfd, FOPEN_BINARY_READ);
3734 if (from_server_fp == NULL)
3735 error (1, errno, "cannot fdopen %d for read", fromfd);
3736 *from_server = stdio_buffer_initialize (from_server_fp, child_pid, 1,
3737 (BUFMEMERRPROC) NULL);
3740 #endif /* defined (AUTH_CLIENT_SUPPORT) || defined (HAVE_KERBEROS) || defined(HAVE_GSSAPI) */
3744 #if defined (AUTH_CLIENT_SUPPORT) || defined(HAVE_GSSAPI)
3745 /* Connect to the authenticating server.
3747 If VERIFY_ONLY is non-zero, then just verify that the password is
3748 correct and then shutdown the connection.
3750 If VERIFY_ONLY is 0, then really connect to the server.
3752 If DO_GSSAPI is non-zero, then we use GSSAPI authentication rather
3753 than the pserver password authentication.
3755 If we fail to connect or if access is denied, then die with fatal
3758 connect_to_pserver (root, to_server_p, from_server_p, verify_only, do_gssapi)
3760 struct buffer **to_server_p;
3761 struct buffer **from_server_p;
3767 struct sockaddr_in client_sai;
3768 struct hostent *hostinfo;
3769 struct buffer *to_server, *from_server;
3771 sock = socket (AF_INET, SOCK_STREAM, 0);
3774 error (1, 0, "cannot create socket: %s", SOCK_STRERROR (SOCK_ERRNO));
3776 port_number = get_cvs_port_number (root);
3777 hostinfo = init_sockaddr (&client_sai, root->hostname, port_number);
3780 fprintf (stderr, " -> Connecting to %s(%s):%d\n",
3782 inet_ntoa (client_sai.sin_addr), port_number);
3784 if (connect (sock, (struct sockaddr *) &client_sai, sizeof (client_sai))
3786 error (1, 0, "connect to %s(%s):%d failed: %s",
3788 inet_ntoa (client_sai.sin_addr),
3789 port_number, SOCK_STRERROR (SOCK_ERRNO));
3791 make_bufs_from_fds (sock, sock, 0, &to_server, &from_server, 1);
3793 auth_server (root, to_server, from_server, verify_only, do_gssapi, hostinfo);
3799 status = buf_shutdown (to_server);
3801 error (0, status, "shutting down buffer to server");
3802 buf_free (to_server);
3805 status = buf_shutdown (from_server);
3807 error (0, status, "shutting down buffer from server");
3808 buf_free (from_server);
3811 /* Don't need to set server_started = 0 since we don't set it to 1
3812 * until returning from this call.
3817 *to_server_p = to_server;
3818 *from_server_p = from_server;
3827 auth_server (root, lto_server, lfrom_server, verify_only, do_gssapi, hostinfo)
3829 struct buffer *lto_server;
3830 struct buffer *lfrom_server;
3833 struct hostent *hostinfo;
3835 char *username; /* the username we use to connect */
3836 char no_passwd = 0; /* gets set if no password found */
3838 /* FIXME!!!!!!!!!!!!!!!!!!
3840 * THIS IS REALLY UGLY!
3842 * I'm setting the globals here so we can make calls to send_to_server &
3843 * read_line. This happens again _after_ we return if we're not in
3844 * verify_only mode. We should be relying on the values we passed in, but
3845 * sent_to_server and read_line don't require an outside buf yet.
3847 to_server = lto_server;
3848 from_server = lfrom_server;
3850 /* Run the authorization mini-protocol before anything else. */
3854 FILE *fp = stdio_buffer_get_file(lto_server);
3855 int fd = fp ? fileno(fp) : -1;
3858 if ((fd < 0) || (fstat (fd, &s) < 0) || !S_ISSOCK(s.st_mode))
3860 error (1, 0, "gserver currently only enabled for socket connections");
3863 if (! connect_to_gserver (root, fd, hostinfo))
3866 "authorization failed: server %s rejected access to %s",
3867 root->hostname, root->directory);
3869 # else /* ! HAVE_GSSAPI */
3870 error (1, 0, "INTERNAL ERROR: This client does not support GSSAPI authentication");
3871 # endif /* HAVE_GSSAPI */
3873 else /* ! do_gssapi */
3875 # ifdef AUTH_CLIENT_SUPPORT
3877 char *password = NULL;
3882 begin = "BEGIN VERIFICATION REQUEST";
3883 end = "END VERIFICATION REQUEST";
3887 begin = "BEGIN AUTH REQUEST";
3888 end = "END AUTH REQUEST";
3891 /* Get the password, probably from ~/.cvspass. */
3892 password = get_cvs_password ();
3893 username = root->username ? root->username : getcaller();
3895 /* Send the empty string by default. This is so anonymous CVS
3896 access doesn't require client to have done "cvs login". */
3897 if (password == NULL)
3900 password = scramble ("");
3903 /* Announce that we're starting the authorization protocol. */
3904 send_to_server(begin, 0);
3905 send_to_server("\012", 1);
3907 /* Send the data the server needs. */
3908 send_to_server(root->directory, 0);
3909 send_to_server("\012", 1);
3910 send_to_server(username, 0);
3911 send_to_server("\012", 1);
3912 send_to_server(password, 0);
3913 send_to_server("\012", 1);
3915 /* Announce that we're ending the authorization protocol. */
3916 send_to_server(end, 0);
3917 send_to_server("\012", 1);
3920 memset (password, 0, strlen (password));
3921 # else /* ! AUTH_CLIENT_SUPPORT */
3922 error (1, 0, "INTERNAL ERROR: This client does not support pserver authentication");
3923 # endif /* AUTH_CLIENT_SUPPORT */
3924 } /* if (do_gssapi) */
3929 /* Loop, getting responses from the server. */
3932 read_line (&read_buf);
3934 if (strcmp (read_buf, "I HATE YOU") == 0)
3936 /* Authorization not granted.
3938 * This is a little confusing since we can reach this while loop in GSSAPI
3939 * mode, but if GSSAPI authentication failed, we already jumped to the
3940 * rejected label (there is no case where the connect_to_gserver function
3941 * can return 1 and we will not receive "I LOVE YOU" from the server, barring
3942 * broken connections and garbled messages, of course).
3944 * i.e. This is a pserver specific error message and should be since
3945 * GSSAPI doesn't use username.
3948 "authorization failed: server %s rejected access to %s for user %s",
3949 root->hostname, root->directory, username);
3951 /* Output a special error message if authentication was attempted
3952 with no password -- the user should be made aware that they may
3953 have missed a step. */
3957 "used empty password; try \"cvs login\" with a real password");
3961 else if (strncmp (read_buf, "E ", 2) == 0)
3963 fprintf (stderr, "%s\n", read_buf + 2);
3965 /* Continue with the authentication protocol. */
3967 else if (strncmp (read_buf, "error ", 6) == 0)
3971 /* First skip the code. */
3973 while (*p != ' ' && *p != '\0')
3976 /* Skip the space that follows the code. */
3980 /* Now output the text. */
3981 fprintf (stderr, "%s\n", p);
3984 else if (strcmp (read_buf, "I LOVE YOU") == 0)
3992 "unrecognized auth response from %s: %s",
3993 root->hostname, read_buf);
3999 #endif /* defined (AUTH_CLIENT_SUPPORT) || defined(HAVE_GSSAPI) */
4003 #ifdef CLIENT_SUPPORT
4005 * connect_to_forked_server ( struct buffer **to_server,
4006 * struct buffer **from_server )
4008 * Connect to a forked server process.
4011 connect_to_forked_server (to_server, from_server)
4012 struct buffer **to_server;
4013 struct buffer **from_server;
4018 /* This is pretty simple. All we need to do is choose the correct
4019 cvs binary and call piped_child. */
4021 const char *command[3];
4023 command[0] = getenv ("CVS_SERVER");
4025 command[0] = program_path;
4027 command[1] = "server";
4032 fprintf (stderr, " -> Forking server: %s %s\n", command[0], command[1]);
4035 child_pid = piped_child (command, &tofd, &fromfd);
4037 error (1, 0, "could not fork server process");
4039 make_bufs_from_fds (tofd, fromfd, child_pid, to_server, from_server, 0);
4041 #endif /* CLIENT_SUPPORT */
4045 #ifdef HAVE_KERBEROS
4046 /* This function has not been changed to deal with NO_SOCKET_TO_FD
4047 (i.e., systems on which sockets cannot be converted to file
4048 descriptors). The first person to try building a kerberos client
4049 on such a system (OS/2, Windows 95, and maybe others) will have to
4050 take care of this. */
4052 start_tcp_server (root, to_server, from_server)
4054 struct buffer **to_server;
4055 struct buffer **from_server;
4058 const char *portenv;
4061 struct sockaddr_in sin;
4064 s = socket (AF_INET, SOCK_STREAM, 0);
4066 error (1, 0, "cannot create socket: %s", SOCK_STRERROR (SOCK_ERRNO));
4068 port = get_cvs_port_number (root);
4070 hp = init_sockaddr (&sin, root->hostname, port);
4072 hname = xstrdup (hp->h_name);
4076 fprintf (stderr, " -> Connecting to %s(%s):%d\n",
4078 inet_ntoa (sin.sin_addr), port);
4081 if (connect (s, (struct sockaddr *) &sin, sizeof sin) < 0)
4082 error (1, 0, "connect to %s(%s):%d failed: %s",
4084 inet_ntoa (sin.sin_addr),
4085 port, SOCK_STRERROR (SOCK_ERRNO));
4089 struct sockaddr_in laddr;
4096 realm = krb_realmofhost (hname);
4098 laddrlen = sizeof (laddr);
4099 if (getsockname (s, (struct sockaddr *) &laddr, &laddrlen) < 0)
4100 error (1, 0, "getsockname failed: %s", SOCK_STRERROR (SOCK_ERRNO));
4102 /* We don't care about the checksum, and pass it as zero. */
4103 status = krb_sendauth (KOPT_DO_MUTUAL, s, &ticket, "rcmd",
4104 hname, realm, (unsigned long) 0, &msg_data,
4105 &cred, sched, &laddr, &sin, "KCVSV1.0");
4106 if (status != KSUCCESS)
4107 error (1, 0, "kerberos authentication failed: %s",
4108 krb_get_err_text (status));
4109 memcpy (kblock, cred.session, sizeof (C_Block));
4116 /* Give caller the values it wants. */
4117 make_bufs_from_fds (s, s, 0, to_server, from_server, 1);
4120 #endif /* HAVE_KERBEROS */
4124 /* Receive a given number of bytes. */
4127 recv_bytes (sock, buf, need)
4136 got = recv (sock, buf, need, 0);
4138 error (1, 0, "recv() from server %s: %s", current_parsed_root->hostname,
4139 got == 0 ? "EOF" : SOCK_STRERROR (SOCK_ERRNO));
4146 /* Connect to the server using GSSAPI authentication. */
4150 * This really needs to be rewritten to use a buffer and not a socket.
4151 * This would enable gserver to work with the SSL code I'm about to commit
4152 * since the SSL connection is going to look like a FIFO and not a socket.
4154 * I think, basically, it will need to use buf_output and buf_read directly
4155 * since I don't think there is a read_bytes function - only read_line.
4157 * recv_bytes could then be removed too.
4159 * Besides, I added some cruft to reenable the socket which shouldn't be
4160 * there. This would also enable its removal.
4162 #define BUFSIZE 1024
4164 connect_to_gserver (root, sock, hostinfo)
4167 struct hostent *hostinfo;
4171 gss_buffer_desc *tok_in_ptr, tok_in, tok_out;
4172 OM_uint32 stat_min, stat_maj;
4173 gss_name_t server_name;
4175 str = "BEGIN GSSAPI REQUEST\012";
4177 if (send (sock, str, strlen (str), 0) < 0)
4178 error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
4180 if (strlen (hostinfo->h_name) > BUFSIZE - 5)
4181 error (1, 0, "Internal error: hostname exceeds length of buffer");
4182 sprintf (buf, "cvs@%s", hostinfo->h_name);
4183 tok_in.length = strlen (buf);
4185 gss_import_name (&stat_min, &tok_in, GSS_C_NT_HOSTBASED_SERVICE,
4188 tok_in_ptr = GSS_C_NO_BUFFER;
4189 gcontext = GSS_C_NO_CONTEXT;
4193 stat_maj = gss_init_sec_context (&stat_min, GSS_C_NO_CREDENTIAL,
4194 &gcontext, server_name,
4197 | GSS_C_REPLAY_FLAG),
4198 0, NULL, tok_in_ptr, NULL, &tok_out,
4200 if (stat_maj != GSS_S_COMPLETE && stat_maj != GSS_S_CONTINUE_NEEDED)
4202 OM_uint32 message_context;
4203 OM_uint32 new_stat_min;
4205 message_context = 0;
4206 gss_display_status (&new_stat_min, stat_maj, GSS_C_GSS_CODE,
4207 GSS_C_NULL_OID, &message_context, &tok_out);
4208 error (0, 0, "GSSAPI authentication failed: %s",
4209 (char *) tok_out.value);
4211 message_context = 0;
4212 gss_display_status (&new_stat_min, stat_min, GSS_C_MECH_CODE,
4213 GSS_C_NULL_OID, &message_context, &tok_out);
4214 error (1, 0, "GSSAPI authentication failed: %s",
4215 (char *) tok_out.value);
4218 if (tok_out.length == 0)
4227 cbuf[0] = (tok_out.length >> 8) & 0xff;
4228 cbuf[1] = tok_out.length & 0xff;
4229 if (send (sock, cbuf, 2, 0) < 0)
4230 error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
4231 if (send (sock, tok_out.value, tok_out.length, 0) < 0)
4232 error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
4234 recv_bytes (sock, cbuf, 2);
4235 need = ((cbuf[0] & 0xff) << 8) | (cbuf[1] & 0xff);
4237 if (need > sizeof buf)
4241 /* This usually means that the server sent us an error
4242 message. Read it byte by byte and print it out.
4243 FIXME: This is a terrible error handling strategy.
4244 However, even if we fix the server, we will still
4245 want to do this to work with older servers. */
4248 got = recv (sock, buf + 2, sizeof buf - 2, 0);
4250 error (1, 0, "recv() from server %s: %s",
4251 root->hostname, SOCK_STRERROR (SOCK_ERRNO));
4252 buf[got + 2] = '\0';
4253 if (buf[got + 1] == '\n')
4254 buf[got + 1] = '\0';
4255 error (1, 0, "error from server %s: %s", root->hostname,
4259 recv_bytes (sock, buf, need);
4260 tok_in.length = need;
4264 tok_in_ptr = &tok_in;
4266 while (stat_maj == GSS_S_CONTINUE_NEEDED);
4271 #endif /* HAVE_GSSAPI */
4275 static int send_variable_proc PROTO ((Node *, void *));
4278 send_variable_proc (node, closure)
4282 send_to_server ("Set ", 0);
4283 send_to_server (node->key, 0);
4284 send_to_server ("=", 1);
4285 send_to_server (node->data, 0);
4286 send_to_server ("\012", 1);
4292 /* Contact the server. */
4297 char *log = getenv ("CVS_CLIENT_LOG");
4299 /* Clear our static variables for this invocation. */
4300 if (toplevel_repos != NULL)
4301 free (toplevel_repos);
4302 toplevel_repos = NULL;
4304 /* Note that generally speaking we do *not* fall back to a different
4305 way of connecting if the first one does not work. This is slow
4306 (*really* slow on a 14.4kbps link); the clean way to have a CVS
4307 which supports several ways of connecting is with access methods. */
4309 switch (current_parsed_root->method)
4312 #ifdef AUTH_CLIENT_SUPPORT
4313 case pserver_method:
4314 /* Toss the return value. It will die with an error message if
4315 * anything goes wrong anyway.
4317 connect_to_pserver (current_parsed_root, &to_server, &from_server, 0, 0);
4319 #endif /* AUTH_CLIENT_SUPPORT */
4322 case kserver_method:
4323 start_tcp_server (current_parsed_root, &to_server, &from_server);
4325 #endif /* HAVE_KERBEROS */
4328 case gserver_method:
4329 /* GSSAPI authentication is handled by the pserver. */
4330 connect_to_pserver (current_parsed_root, &to_server, &from_server, 0, 1);
4332 #endif /* HAVE_GSSAPI */
4335 #ifdef NO_EXT_METHOD
4336 error (0, 0, ":ext: method not supported by this port of CVS");
4337 error (1, 0, "try :server: instead");
4338 #else /* ! NO_EXT_METHOD */
4339 start_rsh_server (current_parsed_root, &to_server, &from_server);
4340 #endif /* NO_EXT_METHOD */
4347 START_SERVER (&tofd, &fromfd, getcaller (),
4348 current_parsed_root->username, current_parsed_root->hostname,
4349 current_parsed_root->directory);
4350 # ifdef START_SERVER_RETURNS_SOCKET
4351 make_bufs_from_fds (tofd, fromfd, 0, &to_server, &from_server, 1);
4352 # else /* ! START_SERVER_RETURNS_SOCKET */
4353 make_bufs_from_fds (tofd, fromfd, 0, &to_server, &from_server, 0);
4354 # endif /* START_SERVER_RETURNS_SOCKET */
4356 #else /* ! START_SERVER */
4357 /* FIXME: It should be possible to implement this portably,
4358 like pserver, which would get rid of the duplicated code
4359 in {vms,windows-NT,...}/startserver.c. */
4361 "the :server: access method is not supported by this port of CVS");
4362 #endif /* START_SERVER */
4366 connect_to_forked_server (&to_server, &from_server);
4371 (start_server internal error): unknown access method");
4375 /* "Hi, I'm Darlene and I'll be your server tonight..." */
4378 /* Set up logfiles, if any.
4380 * We do this _after_ authentication on purpose. Wouldn't really like to
4381 * worry about logging passwords...
4385 int len = strlen (log);
4386 char *buf = xmalloc (len + 5);
4393 /* Open logfiles in binary mode so that they reflect
4394 exactly what was transmitted and received (that is
4395 more important than that they be maximally
4396 convenient to view). */
4397 /* Note that if we create several connections in a single CVS client
4398 (currently used by update.c), then the last set of logfiles will
4399 overwrite the others. There is currently no way around this. */
4401 fp = open_file (buf, "wb");
4403 error (0, errno, "opening to-server logfile %s", buf);
4405 to_server = log_buffer_initialize (to_server, fp, 0,
4406 (BUFMEMERRPROC) NULL);
4409 fp = open_file (buf, "wb");
4411 error (0, errno, "opening from-server logfile %s", buf);
4413 from_server = log_buffer_initialize (from_server, fp, 1,
4414 (BUFMEMERRPROC) NULL);
4419 /* Clear static variables. */
4420 if (toplevel_repos != NULL)
4421 free (toplevel_repos);
4422 toplevel_repos = NULL;
4423 if (last_repos != NULL)
4426 if (last_update_dir != NULL)
4427 free (last_update_dir);
4428 last_update_dir = NULL;
4429 stored_checksum_valid = 0;
4430 if (stored_mode != NULL)
4436 rootless = (strcmp (cvs_cmd_name, "init") == 0);
4439 send_to_server ("Root ", 0);
4440 send_to_server (current_parsed_root->directory, 0);
4441 send_to_server ("\012", 1);
4445 struct response *rs;
4447 send_to_server ("Valid-responses", 0);
4449 for (rs = responses; rs->name != NULL; ++rs)
4451 send_to_server (" ", 0);
4452 send_to_server (rs->name, 0);
4454 send_to_server ("\012", 1);
4456 send_to_server ("valid-requests\012", 0);
4458 if (get_server_responses ())
4462 * Now handle global options.
4464 * -H, -f, -d, -e should be handled OK locally.
4466 * -b we ignore (treating it as a server installation issue).
4467 * FIXME: should be an error message.
4469 * -v we print local version info; FIXME: Add a protocol request to get
4470 * the version from the server so we can print that too.
4472 * -l -t -r -w -q -n and -Q need to go to the server.
4476 int have_global = supported_request ("Global_option");
4482 send_to_server ("Global_option -n\012", 0);
4486 "This server does not support the global -n option.");
4492 send_to_server ("Global_option -q\012", 0);
4496 "This server does not support the global -q option.");
4502 send_to_server ("Global_option -Q\012", 0);
4506 "This server does not support the global -Q option.");
4512 send_to_server ("Global_option -r\012", 0);
4516 "This server does not support the global -r option.");
4522 send_to_server ("Global_option -t\012", 0);
4526 "This server does not support the global -t option.");
4530 /* Find out about server-side cvswrappers. An extra network
4531 turnaround for cvs import seems to be unavoidable, unless we
4532 want to add some kind of client-side place to configure which
4533 filenames imply binary. For cvs add, we could avoid the
4534 problem by keeping a copy of the wrappers in CVSADM (the main
4535 reason to bother would be so we could make add work without
4536 contacting the server, I suspect). */
4538 if ((strcmp (cvs_cmd_name, "import") == 0)
4539 || (strcmp (cvs_cmd_name, "add") == 0))
4541 if (supported_request ("wrapper-sendme-rcsOptions"))
4544 send_to_server ("wrapper-sendme-rcsOptions\012", 0);
4545 err = get_server_responses ();
4547 error (err, 0, "error reading from server");
4551 if (cvsencrypt && !rootless)
4554 /* Turn on encryption before turning on compression. We do
4555 not want to try to compress the encrypted stream. Instead,
4556 we want to encrypt the compressed stream. If we can't turn
4557 on encryption, bomb out; don't let the user think the data
4558 is being encrypted when it is not. */
4559 #ifdef HAVE_KERBEROS
4560 if (current_parsed_root->method == kserver_method)
4562 if (! supported_request ("Kerberos-encrypt"))
4563 error (1, 0, "This server does not support encryption");
4564 send_to_server ("Kerberos-encrypt\012", 0);
4565 to_server = krb_encrypt_buffer_initialize (to_server, 0, sched,
4567 (BUFMEMERRPROC) NULL);
4568 from_server = krb_encrypt_buffer_initialize (from_server, 1,
4570 (BUFMEMERRPROC) NULL);
4573 #endif /* HAVE_KERBEROS */
4575 if (current_parsed_root->method == gserver_method)
4577 if (! supported_request ("Gssapi-encrypt"))
4578 error (1, 0, "This server does not support encryption");
4579 send_to_server ("Gssapi-encrypt\012", 0);
4580 to_server = cvs_gssapi_wrap_buffer_initialize (to_server, 0,
4584 from_server = cvs_gssapi_wrap_buffer_initialize (from_server, 1,
4588 cvs_gssapi_encrypt = 1;
4591 #endif /* HAVE_GSSAPI */
4592 error (1, 0, "Encryption is only supported when using GSSAPI or Kerberos");
4593 #else /* ! ENCRYPTION */
4594 error (1, 0, "This client does not support encryption");
4595 #endif /* ! ENCRYPTION */
4598 if (gzip_level && !rootless)
4600 if (supported_request ("Gzip-stream"))
4602 char gzip_level_buf[5];
4603 send_to_server ("Gzip-stream ", 0);
4604 sprintf (gzip_level_buf, "%d", gzip_level);
4605 send_to_server (gzip_level_buf, 0);
4606 send_to_server ("\012", 1);
4608 /* All further communication with the server will be
4611 to_server = compress_buffer_initialize (to_server, 0, gzip_level,
4612 (BUFMEMERRPROC) NULL);
4613 from_server = compress_buffer_initialize (from_server, 1,
4615 (BUFMEMERRPROC) NULL);
4617 #ifndef NO_CLIENT_GZIP_PROCESS
4618 else if (supported_request ("gzip-file-contents"))
4620 char gzip_level_buf[5];
4621 send_to_server ("gzip-file-contents ", 0);
4622 sprintf (gzip_level_buf, "%d", gzip_level);
4623 send_to_server (gzip_level_buf, 0);
4625 send_to_server ("\012", 1);
4627 file_gzip_level = gzip_level;
4632 fprintf (stderr, "server doesn't support gzip-file-contents\n");
4633 /* Setting gzip_level to 0 prevents us from giving the
4634 error twice if update has to contact the server again
4635 to fetch unpatchable files. */
4640 if (cvsauthenticate && ! cvsencrypt && !rootless)
4642 /* Turn on authentication after turning on compression, so
4643 that we can compress the authentication information. We
4644 assume that encrypted data is always authenticated--the
4645 ability to decrypt the data stream is itself a form of
4648 if (current_parsed_root->method == gserver_method)
4650 if (! supported_request ("Gssapi-authenticate"))
4652 "This server does not support stream authentication");
4653 send_to_server ("Gssapi-authenticate\012", 0);
4654 to_server = cvs_gssapi_wrap_buffer_initialize (to_server, 0,
4658 from_server = cvs_gssapi_wrap_buffer_initialize (from_server, 1,
4664 error (1, 0, "Stream authentication is only supported when using GSSAPI");
4665 #else /* ! HAVE_GSSAPI */
4666 error (1, 0, "This client does not support stream authentication");
4667 #endif /* ! HAVE_GSSAPI */
4670 /* If "Set" is not supported, just silently fail to send the variables.
4671 Users with an old server should get a useful error message when it
4672 fails to recognize the ${=foo} syntax. This way if someone uses
4673 several servers, some of which are new and some old, they can still
4674 set user variables in their .cvsrc without trouble. */
4675 if (supported_request ("Set"))
4676 walklist (variable_list, send_variable_proc, NULL);
4681 #ifndef NO_EXT_METHOD
4683 /* Contact the server by starting it with rsh. */
4685 /* Right now, we have two different definitions for this function,
4686 depending on whether we start the rsh server using popenRW or not.
4687 This isn't ideal, and the best thing would probably be to change
4688 the OS/2 port to be more like the regular Unix client (i.e., by
4689 implementing piped_child)... but I'm doing something else at the
4690 moment, and wish to make only one change at a time. -Karl */
4692 # ifdef START_RSH_WITH_POPEN_RW
4694 /* This is actually a crock -- it's OS/2-specific, for no one else
4695 uses it. If I get time, I want to make piped_child and all the
4696 other stuff in os2/run.c work right. In the meantime, this gets us
4697 up and running, and that's most important. */
4700 start_rsh_server (root, to_server, from_server)
4702 struct buffer **to_server;
4703 struct buffer **from_server;
4708 /* If you're working through firewalls, you can set the
4709 CVS_RSH environment variable to a script which uses rsh to
4710 invoke another rsh on a proxy machine. */
4711 char *cvs_rsh = getenv ("CVS_RSH");
4712 char *cvs_server = getenv ("CVS_SERVER");
4714 /* This needs to fit "rsh", "-b", "-l", "USER", "host",
4715 "cmd (w/ args)", and NULL. We leave some room to grow. */
4719 /* People sometimes suggest or assume that this should default
4720 to "remsh" on systems like HPUX in which that is the
4721 system-supplied name for the rsh program. However, that
4722 causes various problems (keep in mind that systems such as
4723 HPUX might have non-system-supplied versions of "rsh", like
4724 a Kerberized one, which one might want to use). If we
4725 based the name on what is found in the PATH of the person
4726 who runs configure, that would make it harder to
4727 consistently produce the same result in the face of
4728 different people producing binary distributions. If we
4729 based it on "remsh" always being the default for HPUX
4730 (e.g. based on uname), that might be slightly better but
4731 would require us to keep track of what the defaults are for
4732 each system type, and probably would cope poorly if the
4733 existence of remsh or rsh varies from OS version to OS
4734 version. Therefore, it seems best to have the default
4735 remain "rsh", and tell HPUX users to specify remsh, for
4736 example in CVS_RSH or other such mechanisms to be devised,
4737 if that is what they want (the manual already tells them
4743 /* The command line starts out with rsh. */
4744 rsh_argv[i++] = cvs_rsh;
4746 # ifdef RSH_NEEDS_BINARY_FLAG
4747 /* "-b" for binary, under OS/2. */
4748 rsh_argv[i++] = "-b";
4749 # endif /* RSH_NEEDS_BINARY_FLAG */
4751 /* Then we strcat more things on the end one by one. */
4752 if (root->username != NULL)
4754 rsh_argv[i++] = "-l";
4755 rsh_argv[i++] = root->username;
4758 rsh_argv[i++] = root->hostname;
4759 rsh_argv[i++] = cvs_server;
4760 rsh_argv[i++] = "server";
4762 /* Mark the end of the arg list. */
4763 rsh_argv[i] = (char *) NULL;
4767 fprintf (stderr, " -> Starting server: ");
4768 for (i = 0; rsh_argv[i]; i++)
4769 fprintf (stderr, "%s ", rsh_argv[i]);
4770 putc ('\n', stderr);
4774 child_pid = popenRW (rsh_argv, pipes);
4776 error (1, errno, "cannot start server via rsh");
4778 /* Give caller the file descriptors in a form it can deal with. */
4779 make_bufs_from_fds (pipes[0], pipes[1], child_pid, to_server, from_server, 0);
4782 # else /* ! START_RSH_WITH_POPEN_RW */
4785 start_rsh_server (root, to_server, from_server)
4787 struct buffer **to_server;
4788 struct buffer **from_server;
4790 /* If you're working through firewalls, you can set the
4791 CVS_RSH environment variable to a script which uses rsh to
4792 invoke another rsh on a proxy machine. */
4793 char *cvs_rsh = getenv ("CVS_RSH");
4794 char *cvs_server = getenv ("CVS_SERVER");
4804 /* Pass the command to rsh as a single string. This shouldn't
4805 affect most rsh servers at all, and will pacify some buggy
4806 versions of rsh that grab switches out of the middle of the
4807 command (they're calling the GNU getopt routines incorrectly). */
4808 command = xmalloc (strlen (cvs_server) + 8);
4810 /* If you are running a very old (Nov 3, 1994, before 1.5)
4811 * version of the server, you need to make sure that your .bashrc
4812 * on the server machine does not set CVSROOT to something
4813 * containing a colon (or better yet, upgrade the server). */
4814 sprintf (command, "%s server", cvs_server);
4817 const char *argv[10];
4818 const char **p = argv;
4822 /* If the login names differ between client and server
4823 * pass it on to rsh.
4825 if (root->username != NULL)
4828 *p++ = root->username;
4831 *p++ = root->hostname;
4839 fprintf (stderr, " -> Starting server: ");
4840 for (i = 0; argv[i]; i++)
4841 fprintf (stderr, "%s ", argv[i]);
4842 putc ('\n', stderr);
4844 child_pid = piped_child (argv, &tofd, &fromfd);
4847 error (1, errno, "cannot start server via rsh");
4851 make_bufs_from_fds (tofd, fromfd, child_pid, to_server, from_server, 0);
4854 # endif /* START_RSH_WITH_POPEN_RW */
4856 #endif /* NO_EXT_METHOD */
4860 /* Send an argument STRING. */
4868 send_to_server ("Argument ", 0);
4874 send_to_server ("\012Argumentx ", 0);
4879 send_to_server (buf, 1);
4883 send_to_server ("\012", 1);
4888 static void send_modified PROTO ((const char *, const char *, Vers_TS *));
4890 /* VERS->OPTIONS specifies whether the file is binary or not. NOTE: BEFORE
4891 using any other fields of the struct vers, we would need to fix
4892 client_process_import_file to set them up. */
4895 send_modified (file, short_pathname, vers)
4897 const char *short_pathname;
4900 /* File was modified, send it. */
4909 (void) fprintf (stderr, " -> Sending file `%s' to server\n", file);
4911 /* Don't think we can assume fstat exists. */
4912 if ( CVS_STAT (file, &sb) < 0)
4913 error (1, errno, "reading %s", short_pathname);
4915 mode_string = mode_to_string (sb.st_mode);
4917 /* Beware: on systems using CRLF line termination conventions,
4918 the read and write functions will convert CRLF to LF, so the
4919 number of characters read is not the same as sb.st_size. Text
4920 files should always be transmitted using the LF convention, so
4921 we don't want to disable this conversion. */
4922 bufsize = sb.st_size;
4923 buf = xmalloc (bufsize);
4925 /* Is the file marked as containing binary data by the "-kb" flag?
4926 If so, make sure to open it in binary mode: */
4928 if (vers && vers->options)
4929 bin = !(strcmp (vers->options, "-kb"));
4933 #ifdef BROKEN_READWRITE_CONVERSION
4936 /* If only stdio, not open/write/etc., do text/binary
4937 conversion, use convert_file which can compensate
4938 (FIXME: we could just use stdio instead which would
4939 avoid the whole problem). */
4940 char tfile[1024]; strcpy(tfile, file); strcat(tfile, ".CVSBFCTMP");
4941 convert_file (file, O_RDONLY,
4942 tfile, O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY);
4943 fd = CVS_OPEN (tfile, O_RDONLY | OPEN_BINARY);
4945 error (1, errno, "reading %s", short_pathname);
4948 fd = CVS_OPEN (file, O_RDONLY | OPEN_BINARY);
4950 fd = CVS_OPEN (file, O_RDONLY | (bin ? OPEN_BINARY : 0));
4954 error (1, errno, "reading %s", short_pathname);
4956 if (file_gzip_level && sb.st_size > 100)
4960 if (read_and_gzip (fd, short_pathname, (unsigned char **)&buf,
4963 error (1, 0, "aborting due to compression error");
4966 error (0, errno, "warning: can't close %s", short_pathname);
4971 send_to_server ("Modified ", 0);
4972 send_to_server (file, 0);
4973 send_to_server ("\012", 1);
4974 send_to_server (mode_string, 0);
4975 send_to_server ("\012z", 2);
4976 sprintf (tmp, "%lu\n", (unsigned long) newsize);
4977 send_to_server (tmp, 0);
4979 send_to_server (buf, newsize);
4990 /* FIXME: This is gross. It assumes that we might read
4991 less than st_size bytes (true on NT), but not more.
4992 Instead of this we should just be reading a block of
4993 data (e.g. 8192 bytes), writing it to the network, and
4995 while ((len = read (fd, bufp, (buf + sb.st_size) - bufp)) > 0)
4999 error (1, errno, "reading %s", short_pathname);
5001 newsize = bufp - buf;
5004 error (0, errno, "warning: can't close %s", short_pathname);
5009 send_to_server ("Modified ", 0);
5010 send_to_server (file, 0);
5011 send_to_server ("\012", 1);
5012 send_to_server (mode_string, 0);
5013 send_to_server ("\012", 1);
5014 sprintf (tmp, "%lu\012", (unsigned long) newsize);
5015 send_to_server (tmp, 0);
5017 #ifdef BROKEN_READWRITE_CONVERSION
5020 char tfile[1024]; strcpy(tfile, file); strcat(tfile, ".CVSBFCTMP");
5021 if (CVS_UNLINK (tfile) < 0)
5022 error (0, errno, "warning: can't remove temp file %s", tfile);
5027 * Note that this only ends with a newline if the file ended with
5031 send_to_server (buf, newsize);
5037 /* The address of an instance of this structure is passed to
5038 send_fileproc, send_filesdoneproc, and send_direntproc, as the
5039 callerdat parameter. */
5043 /* Each of the following flags are zero for clear or nonzero for set. */
5047 int backup_modified;
5050 static int send_fileproc PROTO ((void *callerdat, struct file_info *finfo));
5052 /* Deal with one file. */
5054 send_fileproc (callerdat, finfo)
5056 struct file_info *finfo;
5058 struct send_data *args = (struct send_data *) callerdat;
5060 struct file_info xfinfo;
5061 /* File name to actually use. Might differ in case from
5063 const char *filename;
5065 send_a_repository ("", finfo->repository, finfo->update_dir);
5068 xfinfo.repository = NULL;
5070 vers = Version_TS (&xfinfo, NULL, NULL, NULL, 0, 0);
5072 if (vers->entdata != NULL)
5073 filename = vers->entdata->user;
5075 filename = finfo->file;
5077 if (vers->vn_user != NULL)
5079 /* The Entries request. */
5080 send_to_server ("Entry /", 0);
5081 send_to_server (filename, 0);
5082 send_to_server ("/", 0);
5083 send_to_server (vers->vn_user, 0);
5084 send_to_server ("/", 0);
5085 if (vers->ts_conflict != NULL)
5087 if (vers->ts_user != NULL &&
5088 strcmp (vers->ts_conflict, vers->ts_user) == 0)
5089 send_to_server ("+=", 0);
5091 send_to_server ("+modified", 0);
5093 send_to_server ("/", 0);
5094 send_to_server (vers->entdata != NULL
5095 ? vers->entdata->options
5098 send_to_server ("/", 0);
5099 if (vers->entdata != NULL && vers->entdata->tag)
5101 send_to_server ("T", 0);
5102 send_to_server (vers->entdata->tag, 0);
5104 else if (vers->entdata != NULL && vers->entdata->date)
5106 send_to_server ("D", 0);
5107 send_to_server (vers->entdata->date, 0);
5109 send_to_server ("\012", 1);
5113 /* It seems a little silly to re-read this on each file, but
5114 send_dirent_proc doesn't get called if filenames are specified
5115 explicitly on the command line. */
5116 wrap_add_file (CVSDOTWRAPPER, 1);
5118 if (wrap_name_has (filename, WRAP_RCSOPTION))
5120 /* No "Entry", but the wrappers did give us a kopt so we better
5121 send it with "Kopt". As far as I know this only happens
5122 for "cvs add". Question: is there any reason why checking
5123 for options from wrappers isn't done in Version_TS?
5125 Note: it might have been better to just remember all the
5126 kopts on the client side, rather than send them to the server,
5127 and have it send us back the same kopts. But that seemed like
5128 a bigger change than I had in mind making now. */
5130 if (supported_request ("Kopt"))
5134 send_to_server ("Kopt ", 0);
5135 opt = wrap_rcsoption (filename, 1);
5136 send_to_server (opt, 0);
5137 send_to_server ("\012", 1);
5143 warning: ignoring -k options due to server limitations");
5147 if (vers->ts_user == NULL)
5150 * Do we want to print "file was lost" like normal CVS?
5151 * Would it always be appropriate?
5153 /* File no longer exists. Don't do anything, missing files
5156 else if (vers->ts_rcs == NULL
5158 || strcmp (vers->ts_user, vers->ts_rcs) != 0
5159 || (vers->vn_user && *vers->vn_user == '0'))
5161 if (args->no_contents
5162 && supported_request ("Is-modified"))
5164 send_to_server ("Is-modified ", 0);
5165 send_to_server (filename, 0);
5166 send_to_server ("\012", 1);
5169 send_modified (filename, finfo->fullname, vers);
5171 if (args->backup_modified)
5174 bakname = backup_file (filename, vers->vn_user);
5175 /* This behavior is sufficiently unexpected to
5176 justify overinformativeness, I think. */
5178 printf ("(Locally modified %s moved to %s)\n",
5185 send_to_server ("Unchanged ", 0);
5186 send_to_server (filename, 0);
5187 send_to_server ("\012", 1);
5190 /* if this directory has an ignore list, add this file to it */
5197 p->key = xstrdup (finfo->file);
5198 (void) addnode (ignlist, p);
5201 freevers_ts (&vers);
5207 static void send_ignproc PROTO ((const char *, const char *));
5210 send_ignproc (file, dir)
5214 if (ign_inhibit_server || !supported_request ("Questionable"))
5217 (void) printf ("? %s/%s\n", dir, file);
5219 (void) printf ("? %s\n", file);
5223 send_to_server ("Questionable ", 0);
5224 send_to_server (file, 0);
5225 send_to_server ("\012", 1);
5231 static int send_filesdoneproc PROTO ((void *, int, const char *, const char *,
5235 send_filesdoneproc (callerdat, err, repository, update_dir, entries)
5238 const char *repository;
5239 const char *update_dir;
5242 /* if this directory has an ignore list, process it then free it */
5245 ignore_files (ignlist, entries, update_dir, send_ignproc);
5252 static Dtype send_dirent_proc PROTO ((void *, const char *, const char *,
5253 const char *, List *));
5256 * send_dirent_proc () is called back by the recursion processor before a
5257 * sub-directory is processed for update.
5258 * A return code of 0 indicates the directory should be
5259 * processed by the recursion code. A return of non-zero indicates the
5260 * recursion code should skip this directory.
5264 send_dirent_proc (callerdat, dir, repository, update_dir, entries)
5267 const char *repository;
5268 const char *update_dir;
5271 struct send_data *args = (struct send_data *) callerdat;
5275 if (ignore_directory (update_dir))
5277 /* print the warm fuzzy message */
5279 error (0, 0, "Ignoring %s", update_dir);
5280 return (R_SKIP_ALL);
5284 * If the directory does not exist yet (e.g. "cvs update -d foo"),
5285 * no need to send any files from it. If the directory does not
5286 * have a CVS directory, then we pretend that it does not exist.
5287 * Otherwise, we will fail when trying to open the Entries file.
5288 * This case will happen when checking out a module defined as
5291 cvsadm_name = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
5292 sprintf (cvsadm_name, "%s/%s", dir, CVSADM);
5293 dir_exists = isdir (cvsadm_name);
5297 * If there is an empty directory (e.g. we are doing `cvs add' on a
5298 * newly-created directory), the server still needs to know about it.
5304 * Get the repository from a CVS/Repository file whenever possible.
5305 * The repository variable is wrong if the names in the local
5306 * directory don't match the names in the repository.
5308 char *repos = Name_Repository (dir, update_dir);
5309 send_a_repository (dir, repos, update_dir);
5312 /* initialize the ignore list for this directory */
5313 ignlist = getlist ();
5317 /* It doesn't make sense to send a non-existent directory,
5318 because there is no way to get the correct value for
5319 the repository (I suppose maybe via the expand-modules
5320 request). In the case where the "obvious" choice for
5321 repository is correct, the server can figure out whether
5322 to recreate the directory; in the case where it is wrong
5323 (that is, does not match what modules give us), we might as
5324 well just fail to recreate it.
5326 Checking for noexec is a kludge for "cvs -n add dir". */
5327 /* Don't send a non-existent directory unless we are building
5328 new directories (build_dirs is true). Otherwise, CVS may
5329 see a D line in an Entries file, and recreate a directory
5330 which the user removed by hand. */
5331 if (args->build_dirs && noexec)
5332 send_a_repository (dir, repository, update_dir);
5335 return (dir_exists ? R_PROCESS : R_SKIP_ALL);
5340 static int send_dirleave_proc PROTO ((void *, const char *, int, const char *,
5344 * send_dirleave_proc () is called back by the recursion code upon leaving
5345 * a directory. All it does is delete the ignore list if it hasn't already
5346 * been done (by send_filesdone_proc).
5350 send_dirleave_proc (callerdat, dir, err, update_dir, entries)
5354 const char *update_dir;
5358 /* Delete the ignore list if it hasn't already been done. */
5365 * Send each option in a string to the server, one by one.
5366 * This assumes that the options are separated by spaces, for example
5367 * STRING might be "--foo -C5 -y".
5371 send_option_string (string)
5377 copy = xstrdup (string);
5384 for (s = p; *s != ' ' && *s != '\0'; s++)
5399 /* Send the names of all the argument files to the server. */
5401 send_file_names (argc, argv, flags)
5408 /* The fact that we do this here as well as start_recursion is a bit
5409 of a performance hit. Perhaps worth cleaning up someday. */
5410 if (flags & SEND_EXPAND_WILD)
5411 expand_wild (argc, argv, &argc, &argv);
5413 for (i = 0; i < argc; ++i)
5417 #ifdef FILENAMES_CASE_INSENSITIVE
5418 char *line = xmalloc (1);
5420 #endif /* FILENAMES_CASE_INSENSITIVE */
5422 if (arg_should_not_be_sent_to_server (argv[i]))
5425 #ifdef FILENAMES_CASE_INSENSITIVE
5426 /* We want to send the path as it appears in the
5427 CVS/Entries files. We put this inside an ifdef
5428 to avoid doing all these system calls in
5429 cases where fncmp is just strcmp anyway. */
5430 /* The isdir (CVSADM) check could more gracefully be replaced
5431 with a way of having Entries_Open report back the
5432 error to us and letting us ignore existence_error.
5436 size_t line_len = 0;
5438 struct saved_cwd sdir;
5440 /* Split the argument onto the stack. */
5442 r = xstrdup (argv[i]);
5443 /* It's okay to discard the const from the last_component return
5444 * below since we know we passed in an arg that was not const.
5446 while ((q = (char *)last_component (r)) != r)
5448 push (stack, xstrdup (q));
5453 /* Normalize the path into outstr. */
5455 while (q = pop (stack))
5462 /* Note that if we are adding a directory,
5463 the following will read the entry
5464 that we just wrote there, that is, we
5465 will get the case specified on the
5466 command line, not the case of the
5467 directory in the filesystem. This
5468 is correct behavior. */
5469 entries = Entries_Open (0, NULL);
5470 node = findnode_fn (entries, q);
5473 /* Add the slash unless this is our first element. */
5475 xrealloc_and_strcat (&line, &line_len, "/");
5476 xrealloc_and_strcat (&line, &line_len, node->key);
5479 Entries_Close (entries);
5482 /* If node is still NULL then we either didn't find CVSADM or
5483 * we didn't find an entry there.
5487 /* Add the slash unless this is our first element. */
5489 xrealloc_and_strcat (&line, &line_len, "/");
5490 xrealloc_and_strcat (&line, &line_len, q);
5494 /* And descend the tree. */
5499 restore_cwd (&sdir, NULL);
5502 /* Now put everything we didn't find entries for back on. */
5503 while (q = pop (stack))
5506 xrealloc_and_strcat (&line, &line_len, "/");
5507 xrealloc_and_strcat (&line, &line_len, q);
5515 #else /* !FILENAMES_CASE_INSENSITIVE */
5517 #endif /* FILENAMES_CASE_INSENSITIVE */
5519 send_to_server ("Argument ", 0);
5525 send_to_server ("\012Argumentx ", 0);
5527 else if (ISDIRSEP (*p))
5530 send_to_server (buf, 1);
5535 send_to_server (buf, 1);
5539 send_to_server ("\012", 1);
5540 #ifdef FILENAMES_CASE_INSENSITIVE
5542 #endif /* FILENAMES_CASE_INSENSITIVE */
5545 if (flags & SEND_EXPAND_WILD)
5548 for (i = 0; i < argc; ++i)
5556 /* Calculate and send max-dotdot to the server */
5558 send_max_dotdot (argc, argv)
5566 /* Send Max-dotdot if needed. */
5567 for (i = 0; i < argc; ++i)
5569 level = pathname_levels (argv[i]);
5572 if (uppaths == NULL) uppaths = getlist();
5573 push_string (uppaths, xstrdup (argv[i]));
5575 if (level > max_level)
5581 if (supported_request ("Max-dotdot"))
5584 sprintf (buf, "%d", max_level);
5586 send_to_server ("Max-dotdot ", 0);
5587 send_to_server (buf, 0);
5588 send_to_server ("\012", 1);
5593 "backreference in path (`..') not supported by old (pre-Max-dotdot) servers");
5600 /* Send Repository, Modified and Entry. argc and argv contain only
5601 the files to operate on (or empty for everything), not options.
5602 local is nonzero if we should not recurse (-l option). flags &
5603 SEND_BUILD_DIRS is nonzero if nonexistent directories should be
5604 sent. flags & SEND_FORCE is nonzero if we should send unmodified
5605 files to the server as though they were modified. flags &
5606 SEND_NO_CONTENTS means that this command only needs to know
5607 _whether_ a file is modified, not the contents. Also sends Argument
5608 lines for argc and argv, so should be called after options are sent. */
5610 send_files (argc, argv, local, aflag, flags)
5617 struct send_data args;
5620 send_max_dotdot (argc, argv);
5623 * aflag controls whether the tag/date is copied into the vers_ts.
5624 * But we don't actually use it, so I don't think it matters what we pass
5627 args.build_dirs = flags & SEND_BUILD_DIRS;
5628 args.force = flags & SEND_FORCE;
5629 args.no_contents = flags & SEND_NO_CONTENTS;
5630 args.backup_modified = flags & BACKUP_MODIFIED_FILES;
5631 err = start_recursion
5632 (send_fileproc, send_filesdoneproc,
5633 send_dirent_proc, send_dirleave_proc, (void *) &args,
5634 argc, argv, local, W_LOCAL, aflag, CVS_LOCK_NONE, (char *) NULL, 0,
5638 if (toplevel_repos == NULL)
5640 * This happens if we are not processing any files,
5641 * or for checkouts in directories without any existing stuff
5642 * checked out. The following assignment is correct for the
5643 * latter case; I don't think toplevel_repos matters for the
5646 toplevel_repos = xstrdup (current_parsed_root->directory);
5647 send_repository ("", toplevel_repos, ".");
5651 client_import_setup (repository)
5654 if (toplevel_repos == NULL) /* should always be true */
5655 send_a_repository ("", repository, "");
5659 * Process the argument import file.
5662 client_process_import_file (message, vfile, vtag, targc, targv, repository,
5663 all_files_binary, modtime)
5670 int all_files_binary;
5672 /* Nonzero for "import -d". */
5679 assert (toplevel_repos != NULL);
5681 if (strncmp (repository, toplevel_repos, strlen (toplevel_repos)) != 0)
5683 "internal error: pathname `%s' doesn't specify file in `%s'",
5684 repository, toplevel_repos);
5686 if (strcmp (repository, toplevel_repos) == 0)
5689 fullname = xstrdup (vfile);
5693 update_dir = repository + strlen (toplevel_repos) + 1;
5695 fullname = xmalloc (strlen (vfile) + strlen (update_dir) + 10);
5696 strcpy (fullname, update_dir);
5697 strcat (fullname, "/");
5698 strcat (fullname, vfile);
5701 send_a_repository ("", repository, update_dir);
5702 if (all_files_binary)
5704 vers.options = xmalloc (4); /* strlen("-kb") + 1 */
5705 strcpy (vers.options, "-kb");
5709 vers.options = wrap_rcsoption (vfile, 1);
5711 if (vers.options != NULL)
5713 if (supported_request ("Kopt"))
5715 send_to_server ("Kopt ", 0);
5716 send_to_server (vers.options, 0);
5717 send_to_server ("\012", 1);
5721 "warning: ignoring -k options due to server limitations");
5725 if (supported_request ("Checkin-time"))
5729 char netdate[MAXDATELEN];
5731 if (CVS_STAT (vfile, &sb) < 0)
5732 error (1, errno, "cannot stat %s", fullname);
5733 rcsdate = date_from_time_t (sb.st_mtime);
5734 date_to_internet (netdate, rcsdate);
5737 send_to_server ("Checkin-time ", 0);
5738 send_to_server (netdate, 0);
5739 send_to_server ("\012", 1);
5743 "warning: ignoring -d option due to server limitations");
5745 send_modified (vfile, fullname, &vers);
5746 if (vers.options != NULL)
5747 free (vers.options);
5753 client_import_done ()
5755 if (toplevel_repos == NULL)
5757 * This happens if we are not processing any files,
5758 * or for checkouts in directories without any existing stuff
5759 * checked out. The following assignment is correct for the
5760 * latter case; I don't think toplevel_repos matters for the
5763 /* FIXME: "can't happen" now that we call client_import_setup
5764 at the beginning. */
5765 toplevel_repos = xstrdup (current_parsed_root->directory);
5766 send_repository ("", toplevel_repos, ".");
5772 notified_a_file (data, ent_list, short_pathname, filename)
5775 char *short_pathname;
5780 size_t line_len = 8192;
5781 char *line = xmalloc (line_len);
5787 fp = open_file (CVSADM_NOTIFY, "r");
5788 if (getline (&line, &line_len, fp) < 0)
5791 error (0, 0, "cannot read %s: end of file", CVSADM_NOTIFY);
5793 error (0, errno, "cannot read %s", CVSADM_NOTIFY);
5796 cp = strchr (line, '\t');
5799 error (0, 0, "malformed %s file", CVSADM_NOTIFY);
5803 if (strcmp (filename, line + 1) != 0)
5805 error (0, 0, "protocol error: notified %s, expected %s", filename,
5809 if (getline (&line, &line_len, fp) < 0)
5814 if (fclose (fp) < 0)
5815 error (0, errno, "cannot close %s", CVSADM_NOTIFY);
5816 if ( CVS_UNLINK (CVSADM_NOTIFY) < 0)
5817 error (0, errno, "cannot remove %s", CVSADM_NOTIFY);
5822 error (0, errno, "cannot read %s", CVSADM_NOTIFY);
5826 newf = open_file (CVSADM_NOTIFYTMP, "w");
5827 if (fputs (line, newf) < 0)
5829 error (0, errno, "cannot write %s", CVSADM_NOTIFYTMP);
5832 while ((nread = fread (line, 1, line_len, fp)) > 0)
5835 while ((nwritten = fwrite (p, 1, nread, newf)) > 0)
5842 error (0, errno, "cannot write %s", CVSADM_NOTIFYTMP);
5848 error (0, errno, "cannot read %s", CVSADM_NOTIFY);
5851 if (fclose (newf) < 0)
5853 error (0, errno, "cannot close %s", CVSADM_NOTIFYTMP);
5857 if (fclose (fp) < 0)
5859 error (0, errno, "cannot close %s", CVSADM_NOTIFY);
5864 /* In this case, we want rename_file() to ignore noexec. */
5865 int saved_noexec = noexec;
5867 rename_file (CVSADM_NOTIFYTMP, CVSADM_NOTIFY);
5868 noexec = saved_noexec;
5873 (void) fclose (newf);
5880 handle_notified (args, len)
5884 call_in_directory (args, notified_a_file, NULL);
5888 client_notify (repository, update_dir, filename, notif_type, val)
5889 const char *repository;
5890 const char *update_dir;
5891 const char *filename;
5897 send_a_repository ("", repository, update_dir);
5898 send_to_server ("Notify ", 0);
5899 send_to_server (filename, 0);
5900 send_to_server ("\012", 1);
5901 buf[0] = notif_type;
5903 send_to_server (buf, 1);
5904 send_to_server ("\t", 1);
5905 send_to_server (val, 0);
5909 * Send an option with an argument, dealing correctly with newlines in
5910 * the argument. If ARG is NULL, forget the whole thing.
5913 option_with_arg (option, arg)
5920 send_to_server ("Argument ", 0);
5921 send_to_server (option, 0);
5922 send_to_server ("\012", 1);
5927 /* Send a date to the server. The input DATE is in RCS format.
5928 The time will be GMT.
5930 We then convert that to the format required in the protocol
5931 (including the "-D" option) and send it. According to
5932 cvsclient.texi, RFC 822/1123 format is preferred. */
5935 client_senddate (date)
5938 char buf[MAXDATELEN];
5940 date_to_internet (buf, (char *)date);
5941 option_with_arg ("-D", buf);
5945 send_init_command ()
5947 /* This is here because we need the current_parsed_root->directory variable. */
5948 send_to_server ("init ", 0);
5949 send_to_server (current_parsed_root->directory, 0);
5950 send_to_server ("\012", 0);
5953 #endif /* CLIENT_SUPPORT */