/contrib/cvs/src/client.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 5948 lines · 4003 code · 769 blank · 1176 comment · 924 complexity · 9511b64d9b8ee98d0142435b9235f1e7 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* CVS client-related stuff.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2, or (at your option)
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details. */
  10. /*
  11. * $FreeBSD$
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif /* HAVE_CONFIG_H */
  16. #include <assert.h>
  17. #include "cvs.h"
  18. #include "getline.h"
  19. #include "edit.h"
  20. #include "buffer.h"
  21. #include "savecwd.h"
  22. #ifdef CLIENT_SUPPORT
  23. # include "md5.h"
  24. # if defined(AUTH_CLIENT_SUPPORT) || defined(HAVE_KERBEROS) || defined(HAVE_GSSAPI) || defined(SOCK_ERRNO) || defined(SOCK_STRERROR)
  25. # ifdef HAVE_WINSOCK_H
  26. # include <winsock.h>
  27. # else /* No winsock.h */
  28. # include <sys/socket.h>
  29. # include <netinet/in.h>
  30. # include <arpa/inet.h>
  31. # include <netdb.h>
  32. # endif /* No winsock.h */
  33. # endif
  34. /* If SOCK_ERRNO is defined, then send()/recv() and other socket calls
  35. do not set errno, but that this macro should be used to obtain an
  36. error code. This probably doesn't make sense unless
  37. NO_SOCKET_TO_FD is also defined. */
  38. # ifndef SOCK_ERRNO
  39. # define SOCK_ERRNO errno
  40. # endif
  41. /* If SOCK_STRERROR is defined, then the error codes returned by
  42. socket operations are not known to strerror, and this macro must be
  43. used instead to convert those error codes to strings. */
  44. # ifndef SOCK_STRERROR
  45. # define SOCK_STRERROR strerror
  46. # if STDC_HEADERS
  47. # include <string.h>
  48. # endif
  49. # ifndef strerror
  50. extern char *strerror ();
  51. # endif
  52. # endif /* ! SOCK_STRERROR */
  53. # if HAVE_KERBEROS
  54. # include <krb.h>
  55. extern char *krb_realmofhost ();
  56. # ifndef HAVE_KRB_GET_ERR_TEXT
  57. # define krb_get_err_text(status) krb_err_txt[status]
  58. # endif /* HAVE_KRB_GET_ERR_TEXT */
  59. /* Information we need if we are going to use Kerberos encryption. */
  60. static C_Block kblock;
  61. static Key_schedule sched;
  62. # endif /* HAVE_KERBEROS */
  63. # ifdef HAVE_GSSAPI
  64. # include "xgssapi.h"
  65. /* This is needed for GSSAPI encryption. */
  66. static gss_ctx_id_t gcontext;
  67. static int connect_to_gserver PROTO((cvsroot_t *, int, struct hostent *));
  68. # endif /* HAVE_GSSAPI */
  69. /* Keep track of any paths we are sending for Max-dotdot so that we can verify
  70. * that uplevel paths coming back form the server are valid.
  71. *
  72. * FIXME: The correct way to do this is probably provide some sort of virtual
  73. * path map on the client side. This would be generic enough to be applied to
  74. * absolute paths supplied by the user too.
  75. */
  76. static List *uppaths = NULL;
  77. static void add_prune_candidate PROTO((const char *));
  78. /* All the commands. */
  79. int add PROTO((int argc, char **argv));
  80. int admin PROTO((int argc, char **argv));
  81. int checkout PROTO((int argc, char **argv));
  82. int commit PROTO((int argc, char **argv));
  83. int diff PROTO((int argc, char **argv));
  84. int history PROTO((int argc, char **argv));
  85. int import PROTO((int argc, char **argv));
  86. int cvslog PROTO((int argc, char **argv));
  87. int patch PROTO((int argc, char **argv));
  88. int release PROTO((int argc, char **argv));
  89. int cvsremove PROTO((int argc, char **argv));
  90. int rtag PROTO((int argc, char **argv));
  91. int status PROTO((int argc, char **argv));
  92. int tag PROTO((int argc, char **argv));
  93. int update PROTO((int argc, char **argv));
  94. /* All the response handling functions. */
  95. static void handle_ok PROTO((char *, int));
  96. static void handle_error PROTO((char *, int));
  97. static void handle_valid_requests PROTO((char *, int));
  98. static void handle_checked_in PROTO((char *, int));
  99. static void handle_new_entry PROTO((char *, int));
  100. static void handle_checksum PROTO((char *, int));
  101. static void handle_copy_file PROTO((char *, int));
  102. static void handle_updated PROTO((char *, int));
  103. static void handle_merged PROTO((char *, int));
  104. static void handle_patched PROTO((char *, int));
  105. static void handle_rcs_diff PROTO((char *, int));
  106. static void handle_removed PROTO((char *, int));
  107. static void handle_remove_entry PROTO((char *, int));
  108. static void handle_set_static_directory PROTO((char *, int));
  109. static void handle_clear_static_directory PROTO((char *, int));
  110. static void handle_set_sticky PROTO((char *, int));
  111. static void handle_clear_sticky PROTO((char *, int));
  112. static void handle_module_expansion PROTO((char *, int));
  113. static void handle_wrapper_rcs_option PROTO((char *, int));
  114. static void handle_m PROTO((char *, int));
  115. static void handle_e PROTO((char *, int));
  116. static void handle_f PROTO((char *, int));
  117. static void handle_notified PROTO((char *, int));
  118. static size_t try_read_from_server PROTO ((char *, size_t));
  119. static void auth_server PROTO ((cvsroot_t *, struct buffer *, struct buffer *,
  120. int, int, struct hostent *));
  121. /* We need to keep track of the list of directories we've sent to the
  122. server. This list, along with the current CVSROOT, will help us
  123. decide which command-line arguments to send. */
  124. List *dirs_sent_to_server = NULL;
  125. static int is_arg_a_parent_or_listed_dir PROTO((Node *, void *));
  126. static int
  127. is_arg_a_parent_or_listed_dir (n, d)
  128. Node *n;
  129. void *d;
  130. {
  131. char *directory = n->key; /* name of the dir sent to server */
  132. char *this_argv_elem = xstrdup (d); /* this argv element */
  133. int retval;
  134. /* Say we should send this argument if the argument matches the
  135. beginning of a directory name sent to the server. This way,
  136. the server will know to start at the top of that directory
  137. hierarchy and descend. */
  138. strip_trailing_slashes (this_argv_elem);
  139. if (strncmp (directory, this_argv_elem, strlen (this_argv_elem)) == 0)
  140. retval = 1;
  141. else
  142. retval = 0;
  143. free (this_argv_elem);
  144. return retval;
  145. }
  146. static int arg_should_not_be_sent_to_server PROTO((char *));
  147. /* Return nonzero if this argument should not be sent to the
  148. server. */
  149. static int
  150. arg_should_not_be_sent_to_server (arg)
  151. char *arg;
  152. {
  153. /* Decide if we should send this directory name to the server. We
  154. should always send argv[i] if:
  155. 1) the list of directories sent to the server is empty (as it
  156. will be for checkout, etc.).
  157. 2) the argument is "."
  158. 3) the argument is a file in the cwd and the cwd is checked out
  159. from the current root
  160. 4) the argument lies within one of the paths in
  161. dirs_sent_to_server.
  162. */
  163. if (list_isempty (dirs_sent_to_server))
  164. return 0; /* always send it */
  165. if (strcmp (arg, ".") == 0)
  166. return 0; /* always send it */
  167. /* We should send arg if it is one of the directories sent to the
  168. server or the parent of one; this tells the server to descend
  169. the hierarchy starting at this level. */
  170. if (isdir (arg))
  171. {
  172. if (walklist (dirs_sent_to_server, is_arg_a_parent_or_listed_dir, arg))
  173. return 0;
  174. /* If arg wasn't a parent, we don't know anything about it (we
  175. would have seen something related to it during the
  176. send_files phase). Don't send it. */
  177. return 1;
  178. }
  179. /* Try to decide whether we should send arg to the server by
  180. checking the contents of the corresponding CVSADM directory. */
  181. {
  182. char *t, *root_string;
  183. cvsroot_t *this_root = NULL;
  184. /* Calculate "dirname arg" */
  185. for (t = arg + strlen (arg) - 1; t >= arg; t--)
  186. {
  187. if (ISDIRSEP(*t))
  188. break;
  189. }
  190. /* Now we're either poiting to the beginning of the
  191. string, or we found a path separator. */
  192. if (t >= arg)
  193. {
  194. /* Found a path separator. */
  195. char c = *t;
  196. *t = '\0';
  197. /* First, check to see if we sent this directory to the
  198. server, because it takes less time than actually
  199. opening the stuff in the CVSADM directory. */
  200. if (walklist (dirs_sent_to_server, is_arg_a_parent_or_listed_dir,
  201. arg))
  202. {
  203. *t = c; /* make sure to un-truncate the arg */
  204. return 0;
  205. }
  206. /* Since we didn't find it in the list, check the CVSADM
  207. files on disk. */
  208. this_root = Name_Root (arg, (char *) NULL);
  209. root_string = this_root->original;
  210. *t = c;
  211. }
  212. else
  213. {
  214. /* We're at the beginning of the string. Look at the
  215. CVSADM files in cwd. */
  216. if (CVSroot_cmdline)
  217. root_string = CVSroot_cmdline;
  218. else
  219. {
  220. this_root = Name_Root ((char *) NULL, (char *) NULL);
  221. root_string = this_root->original;
  222. }
  223. }
  224. /* Now check the value for root. */
  225. if (CVSroot_cmdline == NULL &&
  226. root_string && current_parsed_root
  227. && (strcmp (root_string, current_parsed_root->original) != 0))
  228. {
  229. /* Don't send this, since the CVSROOTs don't match. */
  230. if (this_root) free_cvsroot_t (this_root);
  231. return 1;
  232. }
  233. if (this_root) free_cvsroot_t (this_root);
  234. }
  235. /* OK, let's send it. */
  236. return 0;
  237. }
  238. #endif /* CLIENT_SUPPORT */
  239. #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
  240. /* Shared with server. */
  241. /*
  242. * Return a malloc'd, '\0'-terminated string
  243. * corresponding to the mode in SB.
  244. */
  245. char *
  246. #ifdef __STDC__
  247. mode_to_string (mode_t mode)
  248. #else /* ! __STDC__ */
  249. mode_to_string (mode)
  250. mode_t mode;
  251. #endif /* __STDC__ */
  252. {
  253. char buf[18], u[4], g[4], o[4];
  254. int i;
  255. i = 0;
  256. if (mode & S_IRUSR) u[i++] = 'r';
  257. if (mode & S_IWUSR) u[i++] = 'w';
  258. if (mode & S_IXUSR) u[i++] = 'x';
  259. u[i] = '\0';
  260. i = 0;
  261. if (mode & S_IRGRP) g[i++] = 'r';
  262. if (mode & S_IWGRP) g[i++] = 'w';
  263. if (mode & S_IXGRP) g[i++] = 'x';
  264. g[i] = '\0';
  265. i = 0;
  266. if (mode & S_IROTH) o[i++] = 'r';
  267. if (mode & S_IWOTH) o[i++] = 'w';
  268. if (mode & S_IXOTH) o[i++] = 'x';
  269. o[i] = '\0';
  270. sprintf(buf, "u=%s,g=%s,o=%s", u, g, o);
  271. return xstrdup(buf);
  272. }
  273. /*
  274. * Change mode of FILENAME to MODE_STRING.
  275. * Returns 0 for success or errno code.
  276. * If RESPECT_UMASK is set, then honor the umask.
  277. */
  278. int
  279. change_mode (filename, mode_string, respect_umask)
  280. char *filename;
  281. char *mode_string;
  282. int respect_umask;
  283. {
  284. #ifdef CHMOD_BROKEN
  285. char *p;
  286. int writeable = 0;
  287. /* We can only distinguish between
  288. 1) readable
  289. 2) writeable
  290. 3) Picasso's "Blue Period"
  291. We handle the first two. */
  292. p = mode_string;
  293. while (*p != '\0')
  294. {
  295. if ((p[0] == 'u' || p[0] == 'g' || p[0] == 'o') && p[1] == '=')
  296. {
  297. char *q = p + 2;
  298. while (*q != ',' && *q != '\0')
  299. {
  300. if (*q == 'w')
  301. writeable = 1;
  302. ++q;
  303. }
  304. }
  305. /* Skip to the next field. */
  306. while (*p != ',' && *p != '\0')
  307. ++p;
  308. if (*p == ',')
  309. ++p;
  310. }
  311. /* xchmod honors the umask for us. In the !respect_umask case, we
  312. don't try to cope with it (probably to handle that well, the server
  313. needs to deal with modes in data structures, rather than via the
  314. modes in temporary files). */
  315. xchmod (filename, writeable);
  316. return 0;
  317. #else /* ! CHMOD_BROKEN */
  318. char *p;
  319. mode_t mode = 0;
  320. mode_t oumask;
  321. p = mode_string;
  322. while (*p != '\0')
  323. {
  324. if ((p[0] == 'u' || p[0] == 'g' || p[0] == 'o') && p[1] == '=')
  325. {
  326. int can_read = 0, can_write = 0, can_execute = 0;
  327. char *q = p + 2;
  328. while (*q != ',' && *q != '\0')
  329. {
  330. if (*q == 'r')
  331. can_read = 1;
  332. else if (*q == 'w')
  333. can_write = 1;
  334. else if (*q == 'x')
  335. can_execute = 1;
  336. ++q;
  337. }
  338. if (p[0] == 'u')
  339. {
  340. if (can_read)
  341. mode |= S_IRUSR;
  342. if (can_write)
  343. mode |= S_IWUSR;
  344. if (can_execute)
  345. mode |= S_IXUSR;
  346. }
  347. else if (p[0] == 'g')
  348. {
  349. if (can_read)
  350. mode |= S_IRGRP;
  351. if (can_write)
  352. mode |= S_IWGRP;
  353. if (can_execute)
  354. mode |= S_IXGRP;
  355. }
  356. else if (p[0] == 'o')
  357. {
  358. if (can_read)
  359. mode |= S_IROTH;
  360. if (can_write)
  361. mode |= S_IWOTH;
  362. if (can_execute)
  363. mode |= S_IXOTH;
  364. }
  365. }
  366. /* Skip to the next field. */
  367. while (*p != ',' && *p != '\0')
  368. ++p;
  369. if (*p == ',')
  370. ++p;
  371. }
  372. if (respect_umask)
  373. {
  374. oumask = umask (0);
  375. (void) umask (oumask);
  376. mode &= ~oumask;
  377. }
  378. if (chmod (filename, mode) < 0)
  379. return errno;
  380. return 0;
  381. #endif /* ! CHMOD_BROKEN */
  382. }
  383. #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
  384. #ifdef CLIENT_SUPPORT
  385. int client_prune_dirs;
  386. static List *ignlist = (List *) NULL;
  387. /* Buffer to write to the server. */
  388. static struct buffer *to_server;
  389. /* Buffer used to read from the server. */
  390. static struct buffer *from_server;
  391. /* We want to be able to log data sent between us and the server. We
  392. do it using log buffers. Each log buffer has another buffer which
  393. handles the actual I/O, and a file to log information to.
  394. This structure is the closure field of a log buffer. */
  395. struct log_buffer
  396. {
  397. /* The underlying buffer. */
  398. struct buffer *buf;
  399. /* The file to log information to. */
  400. FILE *log;
  401. };
  402. static struct buffer *log_buffer_initialize
  403. PROTO((struct buffer *, FILE *, int, void (*) (struct buffer *)));
  404. static int log_buffer_input PROTO((void *, char *, int, int, int *));
  405. static int log_buffer_output PROTO((void *, const char *, int, int *));
  406. static int log_buffer_flush PROTO((void *));
  407. static int log_buffer_block PROTO((void *, int));
  408. static int log_buffer_shutdown PROTO((struct buffer *));
  409. /* Create a log buffer. */
  410. static struct buffer *
  411. log_buffer_initialize (buf, fp, input, memory)
  412. struct buffer *buf;
  413. FILE *fp;
  414. int input;
  415. void (*memory) PROTO((struct buffer *));
  416. {
  417. struct log_buffer *n;
  418. n = (struct log_buffer *) xmalloc (sizeof *n);
  419. n->buf = buf;
  420. n->log = fp;
  421. return buf_initialize (input ? log_buffer_input : NULL,
  422. input ? NULL : log_buffer_output,
  423. input ? NULL : log_buffer_flush,
  424. log_buffer_block,
  425. log_buffer_shutdown,
  426. memory,
  427. n);
  428. }
  429. /* The input function for a log buffer. */
  430. static int
  431. log_buffer_input (closure, data, need, size, got)
  432. void *closure;
  433. char *data;
  434. int need;
  435. int size;
  436. int *got;
  437. {
  438. struct log_buffer *lb = (struct log_buffer *) closure;
  439. int status;
  440. size_t n_to_write;
  441. if (lb->buf->input == NULL)
  442. abort ();
  443. status = (*lb->buf->input) (lb->buf->closure, data, need, size, got);
  444. if (status != 0)
  445. return status;
  446. if (*got > 0)
  447. {
  448. n_to_write = *got;
  449. if (fwrite (data, 1, n_to_write, lb->log) != n_to_write)
  450. error (0, errno, "writing to log file");
  451. }
  452. return 0;
  453. }
  454. /* The output function for a log buffer. */
  455. static int
  456. log_buffer_output (closure, data, have, wrote)
  457. void *closure;
  458. const char *data;
  459. int have;
  460. int *wrote;
  461. {
  462. struct log_buffer *lb = (struct log_buffer *) closure;
  463. int status;
  464. size_t n_to_write;
  465. if (lb->buf->output == NULL)
  466. abort ();
  467. status = (*lb->buf->output) (lb->buf->closure, data, have, wrote);
  468. if (status != 0)
  469. return status;
  470. if (*wrote > 0)
  471. {
  472. n_to_write = *wrote;
  473. if (fwrite (data, 1, n_to_write, lb->log) != n_to_write)
  474. error (0, errno, "writing to log file");
  475. }
  476. return 0;
  477. }
  478. /* The flush function for a log buffer. */
  479. static int
  480. log_buffer_flush (closure)
  481. void *closure;
  482. {
  483. struct log_buffer *lb = (struct log_buffer *) closure;
  484. if (lb->buf->flush == NULL)
  485. abort ();
  486. /* We don't really have to flush the log file here, but doing it
  487. will let tail -f on the log file show what is sent to the
  488. network as it is sent. */
  489. if (fflush (lb->log) != 0)
  490. error (0, errno, "flushing log file");
  491. return (*lb->buf->flush) (lb->buf->closure);
  492. }
  493. /* The block function for a log buffer. */
  494. static int
  495. log_buffer_block (closure, block)
  496. void *closure;
  497. int block;
  498. {
  499. struct log_buffer *lb = (struct log_buffer *) closure;
  500. if (block)
  501. return set_block (lb->buf);
  502. else
  503. return set_nonblock (lb->buf);
  504. }
  505. /* The shutdown function for a log buffer. */
  506. static int
  507. log_buffer_shutdown (buf)
  508. struct buffer *buf;
  509. {
  510. struct log_buffer *lb = (struct log_buffer *) buf->closure;
  511. int retval;
  512. retval = buf_shutdown (lb->buf);
  513. if (fclose (lb->log) < 0)
  514. error (0, errno, "closing log file");
  515. return retval;
  516. }
  517. #ifdef NO_SOCKET_TO_FD
  518. /* Under certain circumstances, we must communicate with the server
  519. via a socket using send() and recv(). This is because under some
  520. operating systems (OS/2 and Windows 95 come to mind), a socket
  521. cannot be converted to a file descriptor -- it must be treated as a
  522. socket and nothing else.
  523. We may also need to deal with socket routine error codes differently
  524. in these cases. This is handled through the SOCK_ERRNO and
  525. SOCK_STRERROR macros. */
  526. /* These routines implement a buffer structure which uses send and
  527. recv. The buffer is always in blocking mode so we don't implement
  528. the block routine. */
  529. /* Note that it is important that these routines always handle errors
  530. internally and never return a positive errno code, since it would in
  531. general be impossible for the caller to know in general whether any
  532. error code came from a socket routine (to decide whether to use
  533. SOCK_STRERROR or simply strerror to print an error message). */
  534. /* We use an instance of this structure as the closure field. */
  535. struct socket_buffer
  536. {
  537. /* The socket number. */
  538. int socket;
  539. };
  540. static struct buffer *socket_buffer_initialize
  541. PROTO ((int, int, void (*) (struct buffer *)));
  542. static int socket_buffer_input PROTO((void *, char *, int, int, int *));
  543. static int socket_buffer_output PROTO((void *, const char *, int, int *));
  544. static int socket_buffer_flush PROTO((void *));
  545. static int socket_buffer_shutdown PROTO((struct buffer *));
  546. /* Create a buffer based on a socket. */
  547. static struct buffer *
  548. socket_buffer_initialize (socket, input, memory)
  549. int socket;
  550. int input;
  551. void (*memory) PROTO((struct buffer *));
  552. {
  553. struct socket_buffer *n;
  554. n = (struct socket_buffer *) xmalloc (sizeof *n);
  555. n->socket = socket;
  556. return buf_initialize (input ? socket_buffer_input : NULL,
  557. input ? NULL : socket_buffer_output,
  558. input ? NULL : socket_buffer_flush,
  559. (int (*) PROTO((void *, int))) NULL,
  560. socket_buffer_shutdown,
  561. memory,
  562. n);
  563. }
  564. /* The buffer input function for a buffer built on a socket. */
  565. static int
  566. socket_buffer_input (closure, data, need, size, got)
  567. void *closure;
  568. char *data;
  569. int need;
  570. int size;
  571. int *got;
  572. {
  573. struct socket_buffer *sb = (struct socket_buffer *) closure;
  574. int nbytes;
  575. /* I believe that the recv function gives us exactly the semantics
  576. we want. If there is a message, it returns immediately with
  577. whatever it could get. If there is no message, it waits until
  578. one comes in. In other words, it is not like read, which in
  579. blocking mode normally waits until all the requested data is
  580. available. */
  581. *got = 0;
  582. do
  583. {
  584. /* Note that for certain (broken?) networking stacks, like
  585. VMS's UCX (not sure what version, problem reported with
  586. recv() in 1997), and (according to windows-NT/config.h)
  587. Windows NT 3.51, we must call recv or send with a
  588. moderately sized buffer (say, less than 200K or something),
  589. or else there may be network errors (somewhat hard to
  590. produce, e.g. WAN not LAN or some such). buf_read_data
  591. makes sure that we only recv() BUFFER_DATA_SIZE bytes at
  592. a time. */
  593. nbytes = recv (sb->socket, data, size, 0);
  594. if (nbytes < 0)
  595. error (1, 0, "reading from server: %s", SOCK_STRERROR (SOCK_ERRNO));
  596. if (nbytes == 0)
  597. {
  598. /* End of file (for example, the server has closed
  599. the connection). If we've already read something, we
  600. just tell the caller about the data, not about the end of
  601. file. If we've read nothing, we return end of file. */
  602. if (*got == 0)
  603. return -1;
  604. else
  605. return 0;
  606. }
  607. need -= nbytes;
  608. size -= nbytes;
  609. data += nbytes;
  610. *got += nbytes;
  611. }
  612. while (need > 0);
  613. return 0;
  614. }
  615. /* The buffer output function for a buffer built on a socket. */
  616. static int
  617. socket_buffer_output (closure, data, have, wrote)
  618. void *closure;
  619. const char *data;
  620. int have;
  621. int *wrote;
  622. {
  623. struct socket_buffer *sb = (struct socket_buffer *) closure;
  624. *wrote = have;
  625. /* See comment in socket_buffer_input regarding buffer size we pass
  626. to send and recv. */
  627. #ifdef SEND_NEVER_PARTIAL
  628. /* If send() never will produce a partial write, then just do it. This
  629. is needed for systems where its return value is something other than
  630. the number of bytes written. */
  631. if (send (sb->socket, data, have, 0) < 0)
  632. error (1, 0, "writing to server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
  633. #else
  634. while (have > 0)
  635. {
  636. int nbytes;
  637. nbytes = send (sb->socket, data, have, 0);
  638. if (nbytes < 0)
  639. error (1, 0, "writing to server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
  640. have -= nbytes;
  641. data += nbytes;
  642. }
  643. #endif
  644. return 0;
  645. }
  646. /* The buffer flush function for a buffer built on a socket. */
  647. /*ARGSUSED*/
  648. static int
  649. socket_buffer_flush (closure)
  650. void *closure;
  651. {
  652. /* Nothing to do. Sockets are always flushed. */
  653. return 0;
  654. }
  655. static int
  656. socket_buffer_shutdown (buf)
  657. struct buffer *buf;
  658. {
  659. struct socket_buffer *n = (struct socket_buffer *) buf->closure;
  660. char tmp;
  661. /* no need to flush children of an endpoint buffer here */
  662. if (buf->input)
  663. {
  664. int err = 0;
  665. if (! buf_empty_p (buf)
  666. || (err = recv (n->socket, &tmp, 1, 0)) > 0)
  667. error (0, 0, "dying gasps from %s unexpected", current_parsed_root->hostname);
  668. else if (err == -1)
  669. error (0, 0, "reading from %s: %s", current_parsed_root->hostname, SOCK_STRERROR (SOCK_ERRNO));
  670. /* shutdown() socket */
  671. # ifdef SHUTDOWN_SERVER
  672. if (current_parsed_root->method != server_method)
  673. # endif
  674. if (shutdown (n->socket, 0) < 0)
  675. {
  676. error (1, 0, "shutting down server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
  677. }
  678. buf->input = NULL;
  679. }
  680. else if (buf->output)
  681. {
  682. /* shutdown() socket */
  683. # ifdef SHUTDOWN_SERVER
  684. /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
  685. * SHUTDOWN_SERVER_OUTPUT
  686. */
  687. if (current_parsed_root->method == server_method)
  688. SHUTDOWN_SERVER (n->socket);
  689. else
  690. # endif
  691. if (shutdown (n->socket, 1) < 0)
  692. {
  693. error (1, 0, "shutting down server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
  694. }
  695. buf->output = NULL;
  696. }
  697. return 0;
  698. }
  699. #endif /* NO_SOCKET_TO_FD */
  700. /*
  701. * Read a line from the server. Result does not include the terminating \n.
  702. *
  703. * Space for the result is malloc'd and should be freed by the caller.
  704. *
  705. * Returns number of bytes read.
  706. */
  707. static int
  708. read_line (resultp)
  709. char **resultp;
  710. {
  711. int status;
  712. char *result;
  713. int len;
  714. status = buf_flush (to_server, 1);
  715. if (status != 0)
  716. error (1, status, "writing to server");
  717. status = buf_read_line (from_server, &result, &len);
  718. if (status != 0)
  719. {
  720. if (status == -1)
  721. error (1, 0, "end of file from server (consult above messages if any)");
  722. else if (status == -2)
  723. error (1, 0, "out of memory");
  724. else
  725. error (1, status, "reading from server");
  726. }
  727. if (resultp != NULL)
  728. *resultp = result;
  729. else
  730. free (result);
  731. return len;
  732. }
  733. #endif /* CLIENT_SUPPORT */
  734. #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
  735. /*
  736. * Level of compression to use when running gzip on a single file.
  737. */
  738. int file_gzip_level;
  739. #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
  740. #ifdef CLIENT_SUPPORT
  741. /*
  742. * The Repository for the top level of this command (not necessarily
  743. * the CVSROOT, just the current directory at the time we do it).
  744. */
  745. static char *toplevel_repos = NULL;
  746. /* Working directory when we first started. Note: we could speed things
  747. up on some systems by using savecwd.h here instead of just always
  748. storing a name. */
  749. char *toplevel_wd;
  750. static void
  751. handle_ok (args, len)
  752. char *args;
  753. int len;
  754. {
  755. return;
  756. }
  757. static void
  758. handle_error (args, len)
  759. char *args;
  760. int len;
  761. {
  762. int something_printed;
  763. /*
  764. * First there is a symbolic error code followed by a space, which
  765. * we ignore.
  766. */
  767. char *p = strchr (args, ' ');
  768. if (p == NULL)
  769. {
  770. error (0, 0, "invalid data from cvs server");
  771. return;
  772. }
  773. ++p;
  774. /* Next we print the text of the message from the server. We
  775. probably should be prefixing it with "server error" or some
  776. such, because if it is something like "Out of memory", the
  777. current behavior doesn't say which machine is out of
  778. memory. */
  779. len -= p - args;
  780. something_printed = 0;
  781. for (; len > 0; --len)
  782. {
  783. something_printed = 1;
  784. putc (*p++, stderr);
  785. }
  786. if (something_printed)
  787. putc ('\n', stderr);
  788. }
  789. static void
  790. handle_valid_requests (args, len)
  791. char *args;
  792. int len;
  793. {
  794. char *p = args;
  795. char *q;
  796. struct request *rq;
  797. do
  798. {
  799. q = strchr (p, ' ');
  800. if (q != NULL)
  801. *q++ = '\0';
  802. for (rq = requests; rq->name != NULL; ++rq)
  803. {
  804. if (strcmp (rq->name, p) == 0)
  805. break;
  806. }
  807. if (rq->name == NULL)
  808. /*
  809. * It is a request we have never heard of (and thus never
  810. * will want to use). So don't worry about it.
  811. */
  812. ;
  813. else
  814. {
  815. if (rq->flags & RQ_ENABLEME)
  816. {
  817. /*
  818. * Server wants to know if we have this, to enable the
  819. * feature.
  820. */
  821. send_to_server (rq->name, 0);
  822. send_to_server ("\012", 0);
  823. }
  824. else
  825. rq->flags |= RQ_SUPPORTED;
  826. }
  827. p = q;
  828. } while (q != NULL);
  829. for (rq = requests; rq->name != NULL; ++rq)
  830. {
  831. if ((rq->flags & RQ_SUPPORTED)
  832. || (rq->flags & RQ_ENABLEME))
  833. continue;
  834. if (rq->flags & RQ_ESSENTIAL)
  835. error (1, 0, "request `%s' not supported by server", rq->name);
  836. }
  837. }
  838. /*
  839. * This is a proc for walklist(). It inverts the error return premise of
  840. * walklist.
  841. *
  842. * RETURNS
  843. * True If this path is prefixed by one of the paths in walklist and
  844. * does not step above the prefix path.
  845. * False Otherwise.
  846. */
  847. static
  848. int path_list_prefixed (p, closure)
  849. Node *p;
  850. void *closure;
  851. {
  852. const char *questionable = closure;
  853. const char *prefix = p->key;
  854. if (strncmp (prefix, questionable, strlen (prefix))) return 0;
  855. questionable += strlen (prefix);
  856. while (ISDIRSEP (*questionable)) questionable++;
  857. if (*questionable == '\0') return 1;
  858. return pathname_levels (questionable);
  859. }
  860. /*
  861. * Need to validate the client pathname. Disallowed paths include:
  862. *
  863. * 1. Absolute paths.
  864. * 2. Pathnames that do not reference a specifically requested update
  865. * directory.
  866. *
  867. * In case 2, we actually only check that the directory is under the uppermost
  868. * directories mentioned on the command line.
  869. *
  870. * RETURNS
  871. * True If the path is valid.
  872. * False Otherwise.
  873. */
  874. static
  875. int is_valid_client_path (pathname)
  876. const char *pathname;
  877. {
  878. /* 1. Absolute paths. */
  879. if (isabsolute (pathname)) return 0;
  880. /* 2. No up-references in path. */
  881. if (pathname_levels (pathname) == 0) return 1;
  882. /* 2. No Max-dotdot paths registered. */
  883. if (uppaths == NULL) return 0;
  884. return walklist (uppaths, path_list_prefixed, (void *)pathname);
  885. }
  886. /*
  887. * Do all the processing for PATHNAME, where pathname consists of the
  888. * repository and the filename. The parameters we pass to FUNC are:
  889. * DATA is just the DATA parameter which was passed to
  890. * call_in_directory; ENT_LIST is a pointer to an entries list (which
  891. * we manage the storage for); SHORT_PATHNAME is the pathname of the
  892. * file relative to the (overall) directory in which the command is
  893. * taking place; and FILENAME is the filename portion only of
  894. * SHORT_PATHNAME. When we call FUNC, the curent directory points to
  895. * the directory portion of SHORT_PATHNAME. */
  896. static void
  897. call_in_directory (pathname, func, data)
  898. char *pathname;
  899. void (*func) PROTO((char *data, List *ent_list, char *short_pathname,
  900. char *filename));
  901. char *data;
  902. {
  903. /* This variable holds the result of Entries_Open. */
  904. List *last_entries = NULL;
  905. char *dir_name;
  906. char *filename;
  907. /* This is what we get when we hook up the directory (working directory
  908. name) from PATHNAME with the filename from REPOSNAME. For example:
  909. pathname: ccvs/src/
  910. reposname: /u/src/master/ccvs/foo/ChangeLog
  911. short_pathname: ccvs/src/ChangeLog
  912. */
  913. char *short_pathname;
  914. char *p;
  915. /*
  916. * Do the whole descent in parallel for the repositories, so we
  917. * know what to put in CVS/Repository files. I'm not sure the
  918. * full hair is necessary since the server does a similar
  919. * computation; I suspect that we only end up creating one
  920. * directory at a time anyway.
  921. *
  922. * Also note that we must *only* worry about this stuff when we
  923. * are creating directories; `cvs co foo/bar; cd foo/bar; cvs co
  924. * CVSROOT; cvs update' is legitimate, but in this case
  925. * foo/bar/CVSROOT/CVS/Repository is not a subdirectory of
  926. * foo/bar/CVS/Repository.
  927. */
  928. char *reposname;
  929. char *short_repos;
  930. char *reposdirname;
  931. char *rdirp;
  932. int reposdirname_absolute;
  933. int newdir = 0;
  934. assert (pathname);
  935. reposname = NULL;
  936. read_line (&reposname);
  937. assert (reposname != NULL);
  938. reposdirname_absolute = 0;
  939. if (strncmp (reposname, toplevel_repos, strlen (toplevel_repos)) != 0)
  940. {
  941. reposdirname_absolute = 1;
  942. short_repos = reposname;
  943. }
  944. else
  945. {
  946. short_repos = reposname + strlen (toplevel_repos) + 1;
  947. if (short_repos[-1] != '/')
  948. {
  949. reposdirname_absolute = 1;
  950. short_repos = reposname;
  951. }
  952. }
  953. /* Now that we have SHORT_REPOS, we can calculate the path to the file we
  954. * are being requested to operate on.
  955. */
  956. filename = strrchr (short_repos, '/');
  957. if (filename == NULL)
  958. filename = short_repos;
  959. else
  960. ++filename;
  961. short_pathname = xmalloc (strlen (pathname) + strlen (filename) + 5);
  962. strcpy (short_pathname, pathname);
  963. strcat (short_pathname, filename);
  964. /* Now that we know the path to the file we were requested to operate on,
  965. * we can verify that it is valid.
  966. *
  967. * For security reasons, if SHORT_PATHNAME is absolute or attempts to
  968. * ascend outside of the current sanbbox, we abort. The server should not
  969. * send us anything but relative paths which remain inside the sandbox
  970. * here. Anything less means a trojan CVS server could create and edit
  971. * arbitrary files on the client.
  972. */
  973. if (!is_valid_client_path (short_pathname))
  974. {
  975. error (0, 0,
  976. "Server attempted to update a file via an invalid pathname:");
  977. error (1, 0, "`%s'.", short_pathname);
  978. }
  979. reposdirname = xstrdup (short_repos);
  980. p = strrchr (reposdirname, '/');
  981. if (p == NULL)
  982. {
  983. reposdirname = xrealloc (reposdirname, 2);
  984. reposdirname[0] = '.'; reposdirname[1] = '\0';
  985. }
  986. else
  987. *p = '\0';
  988. dir_name = xstrdup (pathname);
  989. p = strrchr (dir_name, '/');
  990. if (p == NULL)
  991. {
  992. dir_name = xrealloc (dir_name, 2);
  993. dir_name[0] = '.'; dir_name[1] = '\0';
  994. }
  995. else
  996. *p = '\0';
  997. if (client_prune_dirs)
  998. add_prune_candidate (dir_name);
  999. if (toplevel_wd == NULL)
  1000. {
  1001. toplevel_wd = xgetwd ();
  1002. if (toplevel_wd == NULL)
  1003. error (1, errno, "could not get working directory");
  1004. }
  1005. if (CVS_CHDIR (toplevel_wd) < 0)
  1006. error (1, errno, "could not chdir to %s", toplevel_wd);
  1007. if (CVS_CHDIR (dir_name) < 0)
  1008. {
  1009. char *dir;
  1010. char *dirp;
  1011. if (! existence_error (errno))
  1012. error (1, errno, "could not chdir to %s", dir_name);
  1013. /* Directory does not exist, we need to create it. */
  1014. newdir = 1;
  1015. /* Provided we are willing to assume that directories get
  1016. created one at a time, we could simplify this a lot.
  1017. Do note that one aspect still would need to walk the
  1018. dir_name path: the checking for "fncmp (dir, CVSADM)". */
  1019. dir = xmalloc (strlen (dir_name) + 1);
  1020. dirp = dir_name;
  1021. rdirp = reposdirname;
  1022. /* This algorithm makes nested directories one at a time
  1023. and create CVS administration files in them. For
  1024. example, we're checking out foo/bar/baz from the
  1025. repository:
  1026. 1) create foo, point CVS/Repository to <root>/foo
  1027. 2) .. foo/bar .. <root>/foo/bar
  1028. 3) .. foo/bar/baz .. <root>/foo/bar/baz
  1029. As you can see, we're just stepping along DIR_NAME (with
  1030. DIRP) and REPOSDIRNAME (with RDIRP) respectively.
  1031. We need to be careful when we are checking out a
  1032. module, however, since DIR_NAME and REPOSDIRNAME are not
  1033. going to be the same. Since modules will not have any
  1034. slashes in their names, we should watch the output of
  1035. STRCHR to decide whether or not we should use STRCHR on
  1036. the RDIRP. That is, if we're down to a module name,
  1037. don't keep picking apart the repository directory name. */
  1038. do
  1039. {
  1040. dirp = strchr (dirp, '/');
  1041. if (dirp)
  1042. {
  1043. strncpy (dir, dir_name, dirp - dir_name);
  1044. dir[dirp - dir_name] = '\0';
  1045. /* Skip the slash. */
  1046. ++dirp;
  1047. if (rdirp == NULL)
  1048. /* This just means that the repository string has
  1049. fewer components than the dir_name string. But
  1050. that is OK (e.g. see modules3-8 in testsuite). */
  1051. ;
  1052. else
  1053. rdirp = strchr (rdirp, '/');
  1054. }
  1055. else
  1056. {
  1057. /* If there are no more slashes in the dir name,
  1058. we're down to the most nested directory -OR- to
  1059. the name of a module. In the first case, we
  1060. should be down to a DIRP that has no slashes,
  1061. so it won't help/hurt to do another STRCHR call
  1062. on DIRP. It will definitely hurt, however, if
  1063. we're down to a module name, since a module
  1064. name can point to a nested directory (that is,
  1065. DIRP will still have slashes in it. Therefore,
  1066. we should set it to NULL so the routine below
  1067. copies the contents of REMOTEDIRNAME onto the
  1068. root repository directory (does this if rdirp
  1069. is set to NULL, because we used to do an extra
  1070. STRCHR call here). */
  1071. rdirp = NULL;
  1072. strcpy (dir, dir_name);
  1073. }
  1074. if (fncmp (dir, CVSADM) == 0)
  1075. {
  1076. error (0, 0, "cannot create a directory named %s", dir);
  1077. error (0, 0, "because CVS uses \"%s\" for its own uses",
  1078. CVSADM);
  1079. error (1, 0, "rename the directory and try again");
  1080. }
  1081. if (mkdir_if_needed (dir))
  1082. {
  1083. /* It already existed, fine. Just keep going. */
  1084. }
  1085. else if (strcmp (cvs_cmd_name, "export") == 0)
  1086. /* Don't create CVSADM directories if this is export. */
  1087. ;
  1088. else
  1089. {
  1090. /*
  1091. * Put repository in CVS/Repository. For historical
  1092. * (pre-CVS/Root) reasons, this is an absolute pathname,
  1093. * but what really matters is the part of it which is
  1094. * relative to cvsroot.
  1095. */
  1096. char *repo;
  1097. char *r, *b;
  1098. repo = xmalloc (strlen (reposdirname)
  1099. + strlen (toplevel_repos)
  1100. + 80);
  1101. if (reposdirname_absolute)
  1102. r = repo;
  1103. else
  1104. {
  1105. strcpy (repo, toplevel_repos);
  1106. strcat (repo, "/");
  1107. r = repo + strlen (repo);
  1108. }
  1109. if (rdirp)
  1110. {
  1111. /* See comment near start of function; the only
  1112. way that the server can put the right thing
  1113. in each CVS/Repository file is to create the
  1114. directories one at a time. I think that the
  1115. CVS server has been doing this all along. */
  1116. error (0, 0, "\
  1117. warning: server is not creating directories one at a time");
  1118. strncpy (r, reposdirname, rdirp - reposdirname);
  1119. r[rdirp - reposdirname] = '\0';
  1120. }
  1121. else
  1122. strcpy (r, reposdirname);
  1123. Create_Admin (dir, dir, repo,
  1124. (char *)NULL, (char *)NULL, 0, 0, 1);
  1125. free (repo);
  1126. b = strrchr (dir, '/');
  1127. if (b == NULL)
  1128. Subdir_Register ((List *) NULL, (char *) NULL, dir);
  1129. else
  1130. {
  1131. *b = '\0';
  1132. Subdir_Register ((List *) NULL, dir, b + 1);
  1133. *b = '/';
  1134. }
  1135. }
  1136. if (rdirp != NULL)
  1137. {
  1138. /* Skip the slash. */
  1139. ++rdirp;
  1140. }
  1141. } while (dirp != NULL);
  1142. free (dir);
  1143. /* Now it better work. */
  1144. if ( CVS_CHDIR (dir_name) < 0)
  1145. error (1, errno, "could not chdir to %s", dir_name);
  1146. }
  1147. else if (strcmp (cvs_cmd_name, "export") == 0)
  1148. /* Don't create CVSADM directories if this is export. */
  1149. ;
  1150. else if (!isdir (CVSADM))
  1151. {
  1152. /*
  1153. * Put repository in CVS/Repository. For historical
  1154. * (pre-CVS/Root) reasons, this is an absolute pathname,
  1155. * but what really matters is the part of it which is
  1156. * relative to cvsroot.
  1157. */
  1158. char *repo;
  1159. if (reposdirname_absolute)
  1160. repo = reposdirname;
  1161. else
  1162. {
  1163. repo = xmalloc (strlen (reposdirname)
  1164. + strlen (toplevel_repos)
  1165. + 10);
  1166. strcpy (repo, toplevel_repos);
  1167. strcat (repo, "/");
  1168. strcat (repo, reposdirname);
  1169. }
  1170. Create_Admin (".", ".", repo, (char *)NULL, (char *)NULL, 0, 1, 1);
  1171. if (repo != reposdirname)
  1172. free (repo);
  1173. }
  1174. if (strcmp (cvs_cmd_name, "export") != 0)
  1175. {
  1176. last_entries = Entries_Open (0, dir_name);
  1177. /* If this is a newly created directory, we will record
  1178. all subdirectory information, so call Subdirs_Known in
  1179. case there are no subdirectories. If this is not a
  1180. newly created directory, it may be an old working
  1181. directory from before we recorded subdirectory
  1182. information in the Entries file. We force a search for
  1183. all subdirectories now, to make sure our subdirectory
  1184. information is up to date. If the Entries file does
  1185. record subdirectory information, then this call only
  1186. does list manipulation. */
  1187. if (newdir)
  1188. Subdirs_Known (last_entries);
  1189. else
  1190. {
  1191. List *dirlist;
  1192. dirlist = Find_Directories ((char *) NULL, W_LOCAL,
  1193. last_entries);
  1194. dellist (&dirlist);
  1195. }
  1196. }
  1197. free (reposdirname);
  1198. (*func) (data, last_entries, short_pathname, filename);
  1199. if (last_entries != NULL)
  1200. Entries_Close (last_entries);
  1201. free (dir_name);
  1202. free (short_pathname);
  1203. free (reposname);
  1204. }
  1205. static void
  1206. copy_a_file (data, ent_list, short_pathname, filename)
  1207. char *data;
  1208. List *ent_list;
  1209. char *short_pathname;
  1210. char *filename;
  1211. {
  1212. char *newname;
  1213. #ifdef USE_VMS_FILENAMES
  1214. char *p;
  1215. #endif
  1216. read_line (&newname);
  1217. #ifdef USE_VMS_FILENAMES
  1218. /* Mogrify the filename so VMS is happy with it. */
  1219. for(p = newname; *p; p++)
  1220. if(*p == '.' || *p == '#') *p = '_';
  1221. #endif
  1222. /* cvsclient.texi has said for a long time that newname must be in the
  1223. same directory. Wouldn't want a malicious or buggy server overwriting
  1224. ~/.profile, /etc/passwd, or anything like that. */
  1225. if (last_component (newname) != newname)
  1226. error (1, 0, "protocol error: Copy-file tried to specify directory");
  1227. if (unlink_file (newname) && !existence_error (errno))
  1228. error (0, errno, "unable to remove %s", newname);
  1229. copy_file (filename, newname);
  1230. free (newname);
  1231. }
  1232. static void
  1233. handle_copy_file (args, len)
  1234. char *args;
  1235. int len;
  1236. {
  1237. call_in_directory (args, copy_a_file, (char *)NULL);
  1238. }
  1239. /* Attempt to read a file size from a string. Accepts base 8 (0N), base 16
  1240. * (0xN), or base 10. Exits on error.
  1241. *
  1242. * RETURNS
  1243. * The file size, in a size_t.
  1244. *
  1245. * FATAL ERRORS
  1246. * 1. As strtoul().
  1247. * 2. If the number read exceeds SIZE_MAX.
  1248. */
  1249. static size_t
  1250. strto_file_size (const char *s)
  1251. {
  1252. unsigned long tmp;
  1253. char *endptr;
  1254. /* Read it. */
  1255. errno = 0;
  1256. tmp = strtoul (s, &endptr, 0);
  1257. /* Check for errors. */
  1258. if (errno || endptr == s)
  1259. error (1, errno, "Server sent invalid file size `%s'", s);
  1260. if (*endptr != '\0')
  1261. error (1, 0,
  1262. "Server sent trailing characters in file size `%s'",
  1263. endptr);
  1264. if (tmp > SIZE_MAX)
  1265. error (1, 0, "Server sent file size exceeding client max.");
  1266. /* Return it. */
  1267. return (size_t)tmp;
  1268. }
  1269. static void read_counted_file PROTO ((char *, char *));
  1270. /* Read from the server the count for the length of a file, then read
  1271. the contents of that file and write them to FILENAME. FULLNAME is
  1272. the name of the file for use in error messages. FIXME-someday:
  1273. extend this to deal with compressed files and make update_entries
  1274. use it. On error, gives a fatal error. */
  1275. static void
  1276. read_counted_file (filename, fullname)
  1277. char *filename;
  1278. char *fullname;
  1279. {
  1280. char *size_string;
  1281. size_t size;
  1282. char *buf;
  1283. /* Pointers in buf to the place to put data which will be read,
  1284. and the data which needs to be written, respectively. */
  1285. char *pread;
  1286. char *pwrite;
  1287. /* Number of bytes left to read and number of bytes in buf waiting to
  1288. be written, respectively. */
  1289. size_t nread;
  1290. size_t nwrite;
  1291. FILE *fp;
  1292. read_line (&size_string);
  1293. if (size_string[0] == 'z')
  1294. error (1, 0, "\
  1295. protocol error: compressed files not supported for that operation");
  1296. size = strto_file_size (size_string);
  1297. free (size_string);
  1298. /* A more sophisticated implementation would use only a limited amount
  1299. of buffer space (8K perhaps), and read that much at a time. We allocate
  1300. a buffer for the whole file only to make it easy to keep track what
  1301. needs to be read and written. */
  1302. buf = xmalloc (size);
  1303. /* FIXME-someday: caller should pass in a flag saying whether it
  1304. is binary or not. I haven't carefully looked into whether
  1305. CVS/Template files should use local text file conventions or
  1306. not. */
  1307. fp = CVS_FOPEN (filename, "wb");
  1308. if (fp == NULL)
  1309. error (1, errno, "cannot write %s", fullname);
  1310. nread = size;
  1311. nwrite = 0;
  1312. pread = buf;
  1313. pwrite = buf;
  1314. while (nread > 0 || nwrite > 0)
  1315. {
  1316. size_t n;
  1317. if (nread > 0)
  1318. {
  1319. n = try_read_from_server (pread, nread);
  1320. nread -= n;
  1321. pread += n;
  1322. nwrite += n;
  1323. }
  1324. if (nwrite > 0)
  1325. {
  1326. n = fwrite (pwrite, 1, nwrite, fp);
  1327. if (ferror (fp))
  1328. error (1, errno, "cannot write %s", fullname);
  1329. nwrite -= n;
  1330. pwrite += n;
  1331. }
  1332. }
  1333. free (buf);
  1334. if (fclose (fp) < 0)
  1335. error (1, errno, "cannot close %s", fullname);
  1336. }
  1337. /* OK, we want to swallow the "U foo.c" response and then output it only
  1338. if we can update the file. In the future we probably want some more
  1339. systematic approach to parsing tagged text, but for now we keep it
  1340. ad hoc. "Why," I hear you cry, "do we not just look at the
  1341. Update-existing and Created responses?" That is an excellent question,
  1342. and the answer is roughly conservatism/laziness--I haven't read through
  1343. update.c enough to figure out the exact correspondence or lack thereof
  1344. between those responses and a "U foo.c" line (note that Merged, from
  1345. join_file, can be either "C foo" or "U foo" depending on the context). */
  1346. /* Nonzero if we have seen +updated and not -updated. */
  1347. static int updated_seen;
  1348. /* Filename from an "fname" tagged response within +updated/-updated. */
  1349. static char *updated_fname;
  1350. /* This struct is used to hold data when reading the +importmergecmd
  1351. and -importmergecmd tags. We put the variables in a struct only
  1352. for namespace issues. FIXME: As noted above, we need to develop a
  1353. more systematic approach. */
  1354. static struct
  1355. {
  1356. /* Nonzero if we have seen +importmergecmd and not -importmergecmd. */
  1357. int seen;
  1358. /* Number of conflicts, from a "conflicts" tagged response. */
  1359. int conflicts;
  1360. /* First merge tag, from a "mergetag1" tagged response. */
  1361. char *mergetag1;
  1362. /* Second merge tag, from a "mergetag2" tagged response. */
  1363. char *mergetag2;
  1364. /* Repository, from a "repository" tagged response. */
  1365. char *repository;
  1366. } importmergecmd;
  1367. /* Nonzero if we should arrange to return with a failure exit status. */
  1368. static int failure_exit;
  1369. /*
  1370. * The time stamp of the last file we registered.
  1371. */
  1372. static time_t last_register_time;
  1373. /*
  1374. * The Checksum response gives the checksum for the file transferred
  1375. * over by the next Updated, Merged or Patch response. We just store
  1376. * it here, and then check it in update_entries.
  1377. */
  1378. static int stored_checksum_valid;
  1379. static unsigned char stored_checksum[16];
  1380. static void
  1381. handle_checksum (args, len)
  1382. char *args;
  1383. int len;
  1384. {
  1385. char *s;
  1386. char buf[3];
  1387. int i;
  1388. if (stored_checksum_valid)
  1389. error (1, 0, "Checksum received before last one was used");
  1390. s = args;
  1391. buf[2] = '\0';
  1392. for (i = 0; i < 16; i++)
  1393. {
  1394. char *bufend;
  1395. buf[0] = *s++;
  1396. buf[1] = *s++;
  1397. stored_checksum[i] = (char) strtol (buf, &bufend, 16);
  1398. if (bufend != buf + 2)
  1399. break;
  1400. }
  1401. if (i < 16 || *s != '\0')
  1402. error (1, 0, "Invalid Checksum response: `%s'", args);
  1403. stored_checksum_valid = 1;
  1404. }
  1405. /* Mode that we got in a "Mode" response (malloc'd), or NULL if none. */
  1406. static char *stored_mode;
  1407. static void handle_mode PROTO ((char *, int));
  1408. static void
  1409. handle_mode (args, len)
  1410. char *args;
  1411. int len;
  1412. {
  1413. if (stored_mode != NULL)
  1414. error (1, 0, "protocol error: duplicate Mode");
  1415. stored_mode = xstrdup (args);
  1416. }
  1417. /* Nonzero if time was specified in Mod-time. */
  1418. static int stored_modtime_valid;
  1419. /* Time specified in Mod-time. */
  1420. static time_t stored_modtime;
  1421. static void handle_mod_time PROTO ((char *, int));
  1422. static void
  1423. handle_mod_time (args, len)
  1424. char *args;
  1425. int len;
  1426. {
  1427. if (stored_modtime_valid)
  1428. error (0, 0, "protocol error: duplicate Mod-time");
  1429. stored_modtime = get_date (args, NULL);
  1430. if (stored_modtime == (time_t) -1)
  1431. error (0, 0, "protocol error: cannot parse date %s", args);
  1432. else
  1433. stored_modtime_valid = 1;
  1434. }
  1435. /*
  1436. * If we receive a patch, but the patch program fails to apply it, we
  1437. * want to request the original file. We keep a list of files whose
  1438. * patches have failed.
  1439. */
  1440. char **failed_patches;
  1441. int failed_patches_count;
  1442. struct update_entries_data
  1443. {
  1444. enum {
  1445. /*
  1446. * We are just getting an Entries line; the local file is
  1447. * correct.
  1448. */
  1449. UPDATE_ENTRIES_CHECKIN,
  1450. /* We are getting the file contents as well. */
  1451. UPDATE_ENTRIES_UPDATE,
  1452. /*
  1453. * We are getting a patch against the existing local file, not
  1454. * an entire new file.
  1455. */
  1456. UPDATE_ENTRIES_PATCH,
  1457. /*
  1458. * We are getting an RCS change text (diff -n output) against
  1459. * the existing local file, not an entire new file.
  1460. */
  1461. UPDATE_ENTRIES_RCS_DIFF
  1462. } contents;
  1463. enum {
  1464. /* We are replacing an existing file. */
  1465. UPDATE_ENTRIES_EXISTING,
  1466. /* We are creating a new file. */
  1467. UPDATE_ENTRIES_NEW,
  1468. /* We don't know whether it is existing or new. */
  1469. UPDATE_ENTRIES_EXISTING_OR_NEW
  1470. } existp;
  1471. /*
  1472. * String to put in the timestamp field or NULL to use the timestamp
  1473. * of the file.
  1474. */
  1475. char *timestamp;
  1476. };
  1477. /* Update the Entries line for this file. */
  1478. static void
  1479. update_entries (data_arg, ent_list, short_pathname, filename)
  1480. char *data_arg;
  1481. List *ent_list;
  1482. char *short_pathname;
  1483. char *filename;
  1484. {
  1485. char *entries_line;
  1486. struct update_entries_data *data = (struct update_entries_data *)data_arg;
  1487. char *cp;
  1488. char *user;
  1489. char *vn;
  1490. /* Timestamp field. Always empty according to the protocol. */
  1491. char *ts;
  1492. char *options = NULL;
  1493. char *tag = NULL;
  1494. char *date = NULL;
  1495. char *tag_or_date;
  1496. char *scratch_entries = NULL;
  1497. int bin;
  1498. #ifdef UTIME_EXPECTS_WRITABLE
  1499. int change_it_back = 0;
  1500. #endif
  1501. read_line (&entries_line);
  1502. /*
  1503. * Parse the entries line.
  1504. */
  1505. scratch_entries = xstrdup (entries_line);
  1506. if (scratch_entries[0] != '/')
  1507. error (1, 0, "bad entries line `%s' from server", entries_line);
  1508. user = scratch_entries + 1;
  1509. if ((cp = strchr (user, '/')) == NULL)
  1510. error (1, 0, "bad entries line `%s' from server", entries_line);
  1511. *cp++ = '\0';
  1512. vn = cp;
  1513. if ((cp = strchr (vn, '/')) == NULL)
  1514. error (1, 0, "bad entries line `%s' from server", entries_line);
  1515. *cp++ = '\0';
  1516. ts = cp;
  1517. if ((cp = strchr (ts, '/')) == NULL)
  1518. error (1, 0, "bad entries line `%s' from server", entries_line);
  1519. *cp++ = '\0';
  1520. options = cp;
  1521. if ((cp = strchr (options, '/')) == NULL)
  1522. error (1, 0, "bad entries line `%s' from server", entries_line);
  1523. *cp++ = '\0';
  1524. tag_or_date = cp;
  1525. /* If a slash ends the tag_or_date, ignore everything after it. */
  1526. cp = strchr (tag_or_date, '/');
  1527. if (cp != NULL)
  1528. *cp = '\0';
  1529. if (*tag_or_date == 'T')
  1530. tag = tag_or_date + 1;
  1531. else if (*tag_or_date == 'D')
  1532. date = tag_or_date + 1;
  1533. /* Done parsing the entries line. */
  1534. if (data->contents == UPDATE_ENTRIES_UPDATE
  1535. || data->contents == UPDATE_ENTRIES_PATCH
  1536. || data->contents == UPDATE_ENTRIES_RCS_DIFF)
  1537. {
  1538. char *size_string;
  1539. char *mode_string;
  1540. size_t size;
  1541. char *buf;
  1542. char *temp_filename;
  1543. int use_gzip;
  1544. int patch_failed;
  1545. char *s;
  1546. read_line (&mode_string);
  1547. read_line (&size_string);
  1548. if (size_string[0] == 'z')
  1549. {
  1550. use_gzip = 1;
  1551. s = size_string + 1;
  1552. }
  1553. else
  1554. {
  1555. use_gzip = 0;
  1556. s = size_string;
  1557. }
  1558. size = strto_file_size (s);
  1559. free (size_string);
  1560. /* Note that checking this separately from writing the file is
  1561. a race condition: if the existence or lack thereof of the
  1562. file changes between now and the actual calls which
  1563. operate on it, we lose. However (a) there are so many
  1564. cases, I'm reluctant to try to fix them all, (b) in some
  1565. cases the system might not even have a system call which
  1566. does the right thing, and (c) it isn't clear this needs to
  1567. work. */
  1568. if (data->existp == UPDATE_ENTRIES_EXISTING
  1569. && !isfile (filename))
  1570. /* Emit a warning and update the file anyway. */
  1571. error (0, 0, "warning: %s unexpectedly disappeared",
  1572. short_pathname);
  1573. if (data->existp == UPDATE_ENTRIES_NEW
  1574. && isfile (filename))
  1575. {
  1576. /* Emit a warning and refuse to update the file; we don't want
  1577. to clobber a user's file. */
  1578. size_t nread;
  1579. size_t toread;
  1580. /* size should be unsigned, but until we get around to fixing
  1581. that, work around it. */
  1582. size_t usize;
  1583. char buf[8192];
  1584. /* This error might be confusing; it isn't really clear to
  1585. the user what to do about it. Keep in mind that it has
  1586. several causes: (1) something/someone creates the file
  1587. during the time that CVS is running,…