/contrib/cvs/src/update.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 3049 lines · 2026 code · 336 blank · 687 comment · 583 complexity · b0fed84b6bd263cbe9cf98c38fdca803 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
  3. *
  4. * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
  5. * and others.
  6. *
  7. * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
  8. * Portions Copyright (C) 1989-1992, Brian Berliner
  9. *
  10. * You may distribute under the terms of the GNU General Public License as
  11. * specified in the README file that comes with the CVS source distribution.
  12. *
  13. * "update" updates the version in the present directory with respect to the RCS
  14. * repository. The present version must have been created by "checkout". The
  15. * user can keep up-to-date by calling "update" whenever he feels like it.
  16. *
  17. * The present version can be committed by "commit", but this keeps the version
  18. * in tact.
  19. *
  20. * Arguments following the options are taken to be file names to be updated,
  21. * rather than updating the entire directory.
  22. *
  23. * Modified or non-existent RCS files are checked out and reported as U
  24. * <user_file>
  25. *
  26. * Modified user files are reported as M <user_file>. If both the RCS file and
  27. * the user file have been modified, the user file is replaced by the result
  28. * of rcsmerge, and a backup file is written for the user in .#file.version.
  29. * If this throws up irreconcilable differences, the file is reported as C
  30. * <user_file>, and as M <user_file> otherwise.
  31. *
  32. * Files added but not yet committed are reported as A <user_file>. Files
  33. * removed but not yet committed are reported as R <user_file>.
  34. *
  35. * If the current directory contains subdirectories that hold concurrent
  36. * versions, these are updated too. If the -d option was specified, new
  37. * directories added to the repository are automatically created and updated
  38. * as well.
  39. *
  40. * $FreeBSD$
  41. */
  42. #include "cvs.h"
  43. #include <assert.h>
  44. #include "savecwd.h"
  45. #ifdef SERVER_SUPPORT
  46. # include "md5.h"
  47. #endif
  48. #include "watch.h"
  49. #include "fileattr.h"
  50. #include "edit.h"
  51. #include "getline.h"
  52. #include "buffer.h"
  53. #include "hardlink.h"
  54. static int checkout_file PROTO ((struct file_info *finfo, Vers_TS *vers_ts,
  55. int adding, int merging, int update_server));
  56. #ifdef SERVER_SUPPORT
  57. static void checkout_to_buffer PROTO ((void *, const char *, size_t));
  58. static int patch_file PROTO ((struct file_info *finfo,
  59. Vers_TS *vers_ts,
  60. int *docheckout, struct stat *file_info,
  61. unsigned char *checksum));
  62. static void patch_file_write PROTO ((void *, const char *, size_t));
  63. #endif /* SERVER_SUPPORT */
  64. static int merge_file PROTO ((struct file_info *finfo, Vers_TS *vers));
  65. static int scratch_file PROTO((struct file_info *finfo, Vers_TS *vers));
  66. static Dtype update_dirent_proc PROTO ((void *callerdat, const char *dir,
  67. const char *repository,
  68. const char *update_dir,
  69. List *entries));
  70. static int update_dirleave_proc PROTO ((void *callerdat, const char *dir,
  71. int err, const char *update_dir,
  72. List *entries));
  73. static int update_fileproc PROTO ((void *callerdat, struct file_info *));
  74. static int update_filesdone_proc PROTO ((void *callerdat, int err,
  75. const char *repository,
  76. const char *update_dir,
  77. List *entries));
  78. #ifdef PRESERVE_PERMISSIONS_SUPPORT
  79. static int get_linkinfo_proc PROTO ((void *callerdat, struct file_info *));
  80. #endif
  81. static void join_file PROTO ((struct file_info *finfo, Vers_TS *vers_ts));
  82. static char *options = NULL;
  83. static char *tag = NULL;
  84. static char *date = NULL;
  85. /* This is a bit of a kludge. We call WriteTag at the beginning
  86. before we know whether nonbranch is set or not. And then at the
  87. end, once we have the right value for nonbranch, we call WriteTag
  88. again. I don't know whether the first call is necessary or not.
  89. rewrite_tag is nonzero if we are going to have to make that second
  90. call. */
  91. static int rewrite_tag;
  92. static int nonbranch;
  93. /* If we set the tag or date for a subdirectory, we use this to undo
  94. the setting. See update_dirent_proc. */
  95. static char *tag_update_dir;
  96. static char *join_rev1, *date_rev1;
  97. static char *join_rev2, *date_rev2;
  98. static int aflag = 0;
  99. static int toss_local_changes = 0;
  100. static int force_tag_match = 1;
  101. static int pull_template = 0;
  102. static int update_build_dirs = 0;
  103. static int update_prune_dirs = 0;
  104. static int pipeout = 0;
  105. #ifdef SERVER_SUPPORT
  106. static int patches = 0;
  107. static int rcs_diff_patches = 0;
  108. #endif
  109. static List *ignlist = (List *) NULL;
  110. static time_t last_register_time;
  111. static const char *const update_usage[] =
  112. {
  113. "Usage: %s %s [-APCdflRp] [-k kopt] [-r rev] [-D date] [-j rev]\n",
  114. " [-I ign] [-W spec] [files...]\n",
  115. "\t-A\tReset any sticky tags/date/kopts.\n",
  116. "\t-P\tPrune empty directories.\n",
  117. "\t-C\tOverwrite locally modified files with clean repository copies.\n",
  118. "\t-d\tBuild directories, like checkout does.\n",
  119. "\t-f\tForce a head revision match if tag/date not found.\n",
  120. "\t-l\tLocal directory only, no recursion.\n",
  121. "\t-R\tProcess directories recursively.\n",
  122. "\t-p\tSend updates to standard output (avoids stickiness).\n",
  123. "\t-k kopt\tUse RCS kopt -k option on checkout. (is sticky)\n",
  124. "\t-r rev\tUpdate using specified revision/tag (is sticky).\n",
  125. "\t-D date\tSet date to update from (is sticky).\n",
  126. "\t-j rev\tMerge in changes made between current revision and rev.\n",
  127. "\t-I ign\tMore files to ignore (! to reset).\n",
  128. "\t-W spec\tWrappers specification line.\n",
  129. "\t-T\tCreate CVS/Template.\n",
  130. "(Specify the --help global option for a list of other help options)\n",
  131. NULL
  132. };
  133. /*
  134. * update is the argv,argc based front end for arg parsing
  135. */
  136. int
  137. update (argc, argv)
  138. int argc;
  139. char **argv;
  140. {
  141. int c, err;
  142. int local = 0; /* recursive by default */
  143. int which; /* where to look for files and dirs */
  144. int xpull_template = 0;
  145. if (argc == -1)
  146. usage (update_usage);
  147. ign_setup ();
  148. wrap_setup ();
  149. /* parse the args */
  150. optind = 0;
  151. while ((c = getopt (argc, argv, "+ApCPflRQTqduk:r:D:j:I:W:")) != -1)
  152. {
  153. switch (c)
  154. {
  155. case 'A':
  156. aflag = 1;
  157. break;
  158. case 'C':
  159. toss_local_changes = 1;
  160. break;
  161. case 'I':
  162. ign_add (optarg, 0);
  163. break;
  164. case 'W':
  165. wrap_add (optarg, 0);
  166. break;
  167. case 'k':
  168. if (options)
  169. free (options);
  170. options = RCS_check_kflag (optarg);
  171. break;
  172. case 'l':
  173. local = 1;
  174. break;
  175. case 'R':
  176. local = 0;
  177. break;
  178. case 'Q':
  179. case 'q':
  180. /* The CVS 1.5 client sends these options (in addition to
  181. Global_option requests), so we must ignore them. */
  182. if (!server_active)
  183. error (1, 0,
  184. "-q or -Q must be specified before \"%s\"",
  185. cvs_cmd_name);
  186. break;
  187. case 'T':
  188. xpull_template = 1;
  189. break;
  190. case 'd':
  191. update_build_dirs = 1;
  192. break;
  193. case 'f':
  194. force_tag_match = 0;
  195. break;
  196. case 'r':
  197. tag = optarg;
  198. break;
  199. case 'D':
  200. if (date) free (date);
  201. date = Make_Date (optarg);
  202. break;
  203. case 'P':
  204. update_prune_dirs = 1;
  205. break;
  206. case 'p':
  207. pipeout = 1;
  208. noexec = 1; /* so no locks will be created */
  209. break;
  210. case 'j':
  211. if (join_rev2)
  212. error (1, 0, "only two -j options can be specified");
  213. if (join_rev1)
  214. join_rev2 = optarg;
  215. else
  216. join_rev1 = optarg;
  217. break;
  218. case 'u':
  219. #ifdef SERVER_SUPPORT
  220. if (server_active)
  221. {
  222. patches = 1;
  223. rcs_diff_patches = server_use_rcs_diff ();
  224. }
  225. else
  226. #endif
  227. usage (update_usage);
  228. break;
  229. case '?':
  230. default:
  231. usage (update_usage);
  232. break;
  233. }
  234. }
  235. argc -= optind;
  236. argv += optind;
  237. #ifdef CLIENT_SUPPORT
  238. if (current_parsed_root->isremote)
  239. {
  240. int pass;
  241. /* The first pass does the regular update. If we receive at least
  242. one patch which failed, we do a second pass and just fetch
  243. those files whose patches failed. */
  244. pass = 1;
  245. do
  246. {
  247. int status;
  248. start_server ();
  249. if (local)
  250. send_arg("-l");
  251. if (update_build_dirs)
  252. send_arg("-d");
  253. if (pipeout)
  254. send_arg("-p");
  255. if (!force_tag_match)
  256. send_arg("-f");
  257. if (aflag)
  258. send_arg("-A");
  259. if (toss_local_changes)
  260. send_arg("-C");
  261. if (update_prune_dirs)
  262. send_arg("-P");
  263. client_prune_dirs = update_prune_dirs;
  264. option_with_arg ("-r", tag);
  265. if (options && options[0] != '\0')
  266. send_arg (options);
  267. if (date)
  268. client_senddate (date);
  269. if (join_rev1)
  270. option_with_arg ("-j", join_rev1);
  271. if (join_rev2)
  272. option_with_arg ("-j", join_rev2);
  273. wrap_send ();
  274. if (failed_patches_count == 0)
  275. {
  276. unsigned int flags = 0;
  277. /* If the server supports the command "update-patches", that
  278. means that it knows how to handle the -u argument to update,
  279. which means to send patches instead of complete files.
  280. We don't send -u if failed_patches != NULL, so that the
  281. server doesn't try to send patches which will just fail
  282. again. At least currently, the client also clobbers the
  283. file and tells the server it is lost, which also will get
  284. a full file instead of a patch, but it seems clean to omit
  285. -u. */
  286. if (supported_request ("update-patches"))
  287. send_arg ("-u");
  288. send_arg ("--");
  289. if (update_build_dirs)
  290. flags |= SEND_BUILD_DIRS;
  291. if (toss_local_changes) {
  292. flags |= SEND_NO_CONTENTS;
  293. flags |= BACKUP_MODIFIED_FILES;
  294. }
  295. /* If noexec, probably could be setting SEND_NO_CONTENTS.
  296. Same caveats as for "cvs status" apply. */
  297. send_files (argc, argv, local, aflag, flags);
  298. send_file_names (argc, argv, SEND_EXPAND_WILD);
  299. }
  300. else
  301. {
  302. int i;
  303. (void) printf ("%s client: refetching unpatchable files\n",
  304. program_name);
  305. if (toplevel_wd != NULL
  306. && CVS_CHDIR (toplevel_wd) < 0)
  307. {
  308. error (1, errno, "could not chdir to %s", toplevel_wd);
  309. }
  310. send_arg ("--");
  311. for (i = 0; i < failed_patches_count; i++)
  312. if (unlink_file (failed_patches[i]) < 0
  313. && !existence_error (errno))
  314. error (0, errno, "cannot remove %s",
  315. failed_patches[i]);
  316. send_files (failed_patches_count, failed_patches, local,
  317. aflag, update_build_dirs ? SEND_BUILD_DIRS : 0);
  318. send_file_names (failed_patches_count, failed_patches, 0);
  319. free_names (&failed_patches_count, failed_patches);
  320. }
  321. send_to_server ("update\012", 0);
  322. status = get_responses_and_close ();
  323. /* If there are any conflicts, the server will return a
  324. non-zero exit status. If any patches failed, we still
  325. want to run the update again. We use a pass count to
  326. avoid an endless loop. */
  327. /* Notes: (1) assuming that status != 0 implies a
  328. potential conflict is the best we can cleanly do given
  329. the current protocol. I suppose that trying to
  330. re-fetch in cases where there was a more serious error
  331. is probably more or less harmless, but it isn't really
  332. ideal. (2) it would be nice to have a testsuite case for the
  333. conflict-and-patch-failed case. */
  334. if (status != 0
  335. && (failed_patches_count == 0 || pass > 1))
  336. {
  337. if (failed_patches_count > 0)
  338. free_names (&failed_patches_count, failed_patches);
  339. return status;
  340. }
  341. ++pass;
  342. } while (failed_patches_count > 0);
  343. return 0;
  344. }
  345. #endif
  346. if (tag != NULL)
  347. tag_check_valid (tag, argc, argv, local, aflag, "");
  348. if (join_rev1 != NULL)
  349. tag_check_valid_join (join_rev1, argc, argv, local, aflag, "");
  350. if (join_rev2 != NULL)
  351. tag_check_valid_join (join_rev2, argc, argv, local, aflag, "");
  352. /*
  353. * If we are updating the entire directory (for real) and building dirs
  354. * as we go, we make sure there is no static entries file and write the
  355. * tag file as appropriate
  356. */
  357. if (argc <= 0 && !pipeout)
  358. {
  359. if (update_build_dirs)
  360. {
  361. if (unlink_file (CVSADM_ENTSTAT) < 0 && ! existence_error (errno))
  362. error (1, errno, "cannot remove file %s", CVSADM_ENTSTAT);
  363. #ifdef SERVER_SUPPORT
  364. if (server_active)
  365. {
  366. char *repos = Name_Repository (NULL, NULL);
  367. server_clear_entstat (".", repos);
  368. free (repos);
  369. }
  370. #endif
  371. }
  372. /* keep the CVS/Tag file current with the specified arguments */
  373. if (aflag || tag || date)
  374. {
  375. char *repos = Name_Repository (NULL, NULL);
  376. WriteTag ((char *) NULL, tag, date, 0, ".", repos);
  377. free (repos);
  378. rewrite_tag = 1;
  379. nonbranch = 0;
  380. }
  381. }
  382. /* look for files/dirs locally and in the repository */
  383. which = W_LOCAL | W_REPOS;
  384. /* look in the attic too if a tag or date is specified */
  385. if (tag != NULL || date != NULL || joining())
  386. which |= W_ATTIC;
  387. /* call the command line interface */
  388. err = do_update (argc, argv, options, tag, date, force_tag_match,
  389. local, update_build_dirs, aflag, update_prune_dirs,
  390. pipeout, which, join_rev1, join_rev2, (char *) NULL,
  391. xpull_template, (char *) NULL);
  392. /* free the space Make_Date allocated if necessary */
  393. if (date != NULL)
  394. free (date);
  395. return err;
  396. }
  397. /*
  398. * Command line interface to update (used by checkout)
  399. */
  400. int
  401. do_update (argc, argv, xoptions, xtag, xdate, xforce, local, xbuild, xaflag,
  402. xprune, xpipeout, which, xjoin_rev1, xjoin_rev2, preload_update_dir,
  403. xpull_template, repository)
  404. int argc;
  405. char **argv;
  406. char *xoptions;
  407. char *xtag;
  408. char *xdate;
  409. int xforce;
  410. int local;
  411. int xbuild;
  412. int xaflag;
  413. int xprune;
  414. int xpipeout;
  415. int which;
  416. char *xjoin_rev1;
  417. char *xjoin_rev2;
  418. char *preload_update_dir;
  419. int xpull_template;
  420. char *repository;
  421. {
  422. int err = 0;
  423. char *cp;
  424. /* fill in the statics */
  425. options = xoptions;
  426. tag = xtag;
  427. date = xdate;
  428. force_tag_match = xforce;
  429. update_build_dirs = xbuild;
  430. aflag = xaflag;
  431. update_prune_dirs = xprune;
  432. pipeout = xpipeout;
  433. pull_template = xpull_template;
  434. /* setup the join support */
  435. join_rev1 = xjoin_rev1;
  436. join_rev2 = xjoin_rev2;
  437. if (join_rev1 && (cp = strchr (join_rev1, ':')) != NULL)
  438. {
  439. *cp++ = '\0';
  440. date_rev1 = Make_Date (cp);
  441. }
  442. else
  443. date_rev1 = (char *) NULL;
  444. if (join_rev2 && (cp = strchr (join_rev2, ':')) != NULL)
  445. {
  446. *cp++ = '\0';
  447. date_rev2 = Make_Date (cp);
  448. }
  449. else
  450. date_rev2 = (char *) NULL;
  451. #ifdef PRESERVE_PERMISSIONS_SUPPORT
  452. if (preserve_perms)
  453. {
  454. /* We need to do an extra recursion, bleah. It's to make sure
  455. that we know as much as possible about file linkage. */
  456. hardlist = getlist();
  457. working_dir = xgetwd(); /* save top-level working dir */
  458. /* FIXME-twp: the arguments to start_recursion make me dizzy. This
  459. function call was copied from the update_fileproc call that
  460. follows it; someone should make sure that I did it right. */
  461. err = start_recursion (get_linkinfo_proc, (FILESDONEPROC) NULL,
  462. (DIRENTPROC) NULL, (DIRLEAVEPROC) NULL, NULL,
  463. argc, argv, local, which, aflag, CVS_LOCK_READ,
  464. preload_update_dir, 1, (char *) NULL);
  465. if (err)
  466. return err;
  467. /* FIXME-twp: at this point we should walk the hardlist
  468. and update the `links' field of each hardlink_info struct
  469. to list the files that are linked on dist. That would make
  470. it easier & more efficient to compare the disk linkage with
  471. the repository linkage (a simple strcmp). */
  472. }
  473. #endif
  474. /* call the recursion processor */
  475. err = start_recursion (update_fileproc, update_filesdone_proc,
  476. update_dirent_proc, update_dirleave_proc, NULL,
  477. argc, argv, local, which, aflag, CVS_LOCK_READ,
  478. preload_update_dir, 1, repository);
  479. /* see if we need to sleep before returning to avoid time-stamp races */
  480. if (!server_active && last_register_time)
  481. {
  482. sleep_past (last_register_time);
  483. }
  484. return err;
  485. }
  486. #ifdef PRESERVE_PERMISSIONS_SUPPORT
  487. /*
  488. * The get_linkinfo_proc callback adds each file to the hardlist
  489. * (see hardlink.c).
  490. */
  491. static int
  492. get_linkinfo_proc (callerdat, finfo)
  493. void *callerdat;
  494. struct file_info *finfo;
  495. {
  496. char *fullpath;
  497. Node *linkp;
  498. struct hardlink_info *hlinfo;
  499. /* Get the full pathname of the current file. */
  500. fullpath = xmalloc (strlen(working_dir) +
  501. strlen(finfo->fullname) + 2);
  502. sprintf (fullpath, "%s/%s", working_dir, finfo->fullname);
  503. /* To permit recursing into subdirectories, files
  504. are keyed on the full pathname and not on the basename. */
  505. linkp = lookup_file_by_inode (fullpath);
  506. if (linkp == NULL)
  507. {
  508. /* The file isn't on disk; we are probably restoring
  509. a file that was removed. */
  510. return 0;
  511. }
  512. /* Create a new, empty hardlink_info node. */
  513. hlinfo = (struct hardlink_info *)
  514. xmalloc (sizeof (struct hardlink_info));
  515. hlinfo->status = (Ctype) 0; /* is this dumb? */
  516. hlinfo->checked_out = 0;
  517. linkp->data = hlinfo;
  518. return 0;
  519. }
  520. #endif
  521. /*
  522. * This is the callback proc for update. It is called for each file in each
  523. * directory by the recursion code. The current directory is the local
  524. * instantiation. file is the file name we are to operate on. update_dir is
  525. * set to the path relative to where we started (for pretty printing).
  526. * repository is the repository. entries and srcfiles are the pre-parsed
  527. * entries and source control files.
  528. *
  529. * This routine decides what needs to be done for each file and does the
  530. * appropriate magic for checkout
  531. */
  532. static int
  533. update_fileproc (callerdat, finfo)
  534. void *callerdat;
  535. struct file_info *finfo;
  536. {
  537. int retval;
  538. Ctype status;
  539. Vers_TS *vers;
  540. status = Classify_File (finfo, tag, date, options, force_tag_match,
  541. aflag, &vers, pipeout);
  542. /* Keep track of whether TAG is a branch tag.
  543. Note that if it is a branch tag in some files and a nonbranch tag
  544. in others, treat it as a nonbranch tag. It is possible that case
  545. should elicit a warning or an error. */
  546. if (rewrite_tag
  547. && tag != NULL
  548. && finfo->rcs != NULL)
  549. {
  550. char *rev = RCS_getversion (finfo->rcs, tag, date, 1, NULL);
  551. if (rev != NULL
  552. && !RCS_nodeisbranch (finfo->rcs, tag))
  553. nonbranch = 1;
  554. if (rev != NULL)
  555. free (rev);
  556. }
  557. if (pipeout)
  558. {
  559. /*
  560. * We just return success without doing anything if any of the really
  561. * funky cases occur
  562. *
  563. * If there is still a valid RCS file, do a regular checkout type
  564. * operation
  565. */
  566. switch (status)
  567. {
  568. case T_UNKNOWN: /* unknown file was explicitly asked
  569. * about */
  570. case T_REMOVE_ENTRY: /* needs to be un-registered */
  571. case T_ADDED: /* added but not committed */
  572. retval = 0;
  573. break;
  574. case T_CONFLICT: /* old punt-type errors */
  575. retval = 1;
  576. break;
  577. case T_UPTODATE: /* file was already up-to-date */
  578. case T_NEEDS_MERGE: /* needs merging */
  579. case T_MODIFIED: /* locally modified */
  580. case T_REMOVED: /* removed but not committed */
  581. case T_CHECKOUT: /* needs checkout */
  582. case T_PATCH: /* needs patch */
  583. retval = checkout_file (finfo, vers, 0, 0, 0);
  584. break;
  585. default: /* can't ever happen :-) */
  586. error (0, 0,
  587. "unknown file status %d for file %s", status, finfo->file);
  588. retval = 0;
  589. break;
  590. }
  591. }
  592. else
  593. {
  594. switch (status)
  595. {
  596. case T_UNKNOWN: /* unknown file was explicitly asked
  597. * about */
  598. case T_UPTODATE: /* file was already up-to-date */
  599. retval = 0;
  600. break;
  601. case T_CONFLICT: /* old punt-type errors */
  602. retval = 1;
  603. write_letter (finfo, 'C');
  604. break;
  605. case T_NEEDS_MERGE: /* needs merging */
  606. if (! toss_local_changes)
  607. {
  608. retval = merge_file (finfo, vers);
  609. break;
  610. }
  611. /* else FALL THROUGH */
  612. case T_MODIFIED: /* locally modified */
  613. retval = 0;
  614. if (toss_local_changes)
  615. {
  616. char *bakname;
  617. bakname = backup_file (finfo->file, vers->vn_user);
  618. /* This behavior is sufficiently unexpected to
  619. justify overinformativeness, I think. */
  620. if (!really_quiet && !server_active)
  621. (void) printf ("(Locally modified %s moved to %s)\n",
  622. finfo->file, bakname);
  623. free (bakname);
  624. /* The locally modified file is still present, but
  625. it will be overwritten by the repository copy
  626. after this. */
  627. status = T_CHECKOUT;
  628. retval = checkout_file (finfo, vers, 0, 0, 1);
  629. }
  630. else
  631. {
  632. if (vers->ts_conflict)
  633. {
  634. if (file_has_markers (finfo))
  635. {
  636. write_letter (finfo, 'C');
  637. retval = 1;
  638. }
  639. else
  640. {
  641. /* Reregister to clear conflict flag. */
  642. Register (finfo->entries, finfo->file,
  643. vers->vn_rcs, vers->ts_rcs,
  644. vers->options, vers->tag,
  645. vers->date, (char *)0);
  646. }
  647. }
  648. if (!retval)
  649. write_letter (finfo, 'M');
  650. }
  651. break;
  652. case T_PATCH: /* needs patch */
  653. #ifdef SERVER_SUPPORT
  654. if (patches)
  655. {
  656. int docheckout;
  657. struct stat file_info;
  658. unsigned char checksum[16];
  659. retval = patch_file (finfo,
  660. vers, &docheckout,
  661. &file_info, checksum);
  662. if (! docheckout)
  663. {
  664. if (server_active && retval == 0)
  665. server_updated (finfo, vers,
  666. (rcs_diff_patches
  667. ? SERVER_RCS_DIFF
  668. : SERVER_PATCHED),
  669. file_info.st_mode, checksum,
  670. (struct buffer *) NULL);
  671. break;
  672. }
  673. }
  674. #endif
  675. /* If we're not running as a server, just check the
  676. file out. It's simpler and faster than producing
  677. and applying patches. */
  678. /* Fall through. */
  679. case T_CHECKOUT: /* needs checkout */
  680. retval = checkout_file (finfo, vers, 0, 0, 1);
  681. break;
  682. case T_ADDED: /* added but not committed */
  683. write_letter (finfo, 'A');
  684. retval = 0;
  685. break;
  686. case T_REMOVED: /* removed but not committed */
  687. write_letter (finfo, 'R');
  688. retval = 0;
  689. break;
  690. case T_REMOVE_ENTRY: /* needs to be un-registered */
  691. retval = scratch_file (finfo, vers);
  692. break;
  693. default: /* can't ever happen :-) */
  694. error (0, 0,
  695. "unknown file status %d for file %s", status, finfo->file);
  696. retval = 0;
  697. break;
  698. }
  699. }
  700. /* only try to join if things have gone well thus far */
  701. if (retval == 0 && join_rev1)
  702. join_file (finfo, vers);
  703. /* if this directory has an ignore list, add this file to it */
  704. if (ignlist && (status != T_UNKNOWN || vers->ts_user == NULL))
  705. {
  706. Node *p;
  707. p = getnode ();
  708. p->type = FILES;
  709. p->key = xstrdup (finfo->file);
  710. if (addnode (ignlist, p) != 0)
  711. freenode (p);
  712. }
  713. freevers_ts (&vers);
  714. return retval;
  715. }
  716. static void update_ignproc PROTO ((const char *, const char *));
  717. static void
  718. update_ignproc (file, dir)
  719. const char *file;
  720. const char *dir;
  721. {
  722. struct file_info finfo;
  723. char *tmp;
  724. memset (&finfo, 0, sizeof (finfo));
  725. finfo.file = file;
  726. finfo.update_dir = dir;
  727. if (dir[0] == '\0')
  728. tmp = xstrdup (file);
  729. else
  730. {
  731. tmp = xmalloc (strlen (file) + strlen (dir) + 10);
  732. strcpy (tmp, dir);
  733. strcat (tmp, "/");
  734. strcat (tmp, file);
  735. }
  736. finfo.fullname = tmp;
  737. write_letter (&finfo, '?');
  738. free (tmp);
  739. }
  740. /* ARGSUSED */
  741. static int
  742. update_filesdone_proc (callerdat, err, repository, update_dir, entries)
  743. void *callerdat;
  744. int err;
  745. const char *repository;
  746. const char *update_dir;
  747. List *entries;
  748. {
  749. if (rewrite_tag)
  750. {
  751. WriteTag (NULL, tag, date, nonbranch, update_dir, repository);
  752. rewrite_tag = 0;
  753. }
  754. /* if this directory has an ignore list, process it then free it */
  755. if (ignlist)
  756. {
  757. ignore_files (ignlist, entries, update_dir, update_ignproc);
  758. dellist (&ignlist);
  759. }
  760. /* Clean up CVS admin dirs if we are export */
  761. if (strcmp (cvs_cmd_name, "export") == 0)
  762. {
  763. /* I'm not sure the existence_error is actually possible (except
  764. in cases where we really should print a message), but since
  765. this code used to ignore all errors, I'll play it safe. */
  766. if (unlink_file_dir (CVSADM) < 0 && !existence_error (errno))
  767. error (0, errno, "cannot remove %s directory", CVSADM);
  768. }
  769. else if (!server_active && !pipeout)
  770. {
  771. /* If there is no CVS/Root file, add one */
  772. if (!isfile (CVSADM_ROOT))
  773. Create_Root ((char *) NULL, current_parsed_root->original);
  774. }
  775. return err;
  776. }
  777. /*
  778. * update_dirent_proc () is called back by the recursion processor before a
  779. * sub-directory is processed for update. In this case, update_dirent proc
  780. * will probably create the directory unless -d isn't specified and this is a
  781. * new directory. A return code of 0 indicates the directory should be
  782. * processed by the recursion code. A return of non-zero indicates the
  783. * recursion code should skip this directory.
  784. */
  785. static Dtype
  786. update_dirent_proc (callerdat, dir, repository, update_dir, entries)
  787. void *callerdat;
  788. const char *dir;
  789. const char *repository;
  790. const char *update_dir;
  791. List *entries;
  792. {
  793. if (ignore_directory (update_dir))
  794. {
  795. /* print the warm fuzzy message */
  796. if (!quiet)
  797. error (0, 0, "Ignoring %s", update_dir);
  798. return R_SKIP_ALL;
  799. }
  800. if (!isdir (dir))
  801. {
  802. /* if we aren't building dirs, blow it off */
  803. if (!update_build_dirs)
  804. return R_SKIP_ALL;
  805. /* Various CVS administrators are in the habit of removing
  806. the repository directory for things they don't want any
  807. more. I've even been known to do it myself (on rare
  808. occasions). Not the usual recommended practice, but we
  809. want to try to come up with some kind of
  810. reasonable/documented/sensible behavior. Generally
  811. the behavior is to just skip over that directory (see
  812. dirs test in sanity.sh; the case which reaches here
  813. is when update -d is specified, and the working directory
  814. is gone but the subdirectory is still mentioned in
  815. CVS/Entries). */
  816. /* In the remote case, the client should refrain from
  817. sending us the directory in the first place. So we
  818. want to continue to give an error, so clients make
  819. sure to do this. */
  820. if (!server_active && !isdir (repository))
  821. return R_SKIP_ALL;
  822. if (noexec)
  823. {
  824. /* ignore the missing dir if -n is specified */
  825. error (0, 0, "New directory `%s' -- ignored", update_dir);
  826. return R_SKIP_ALL;
  827. }
  828. else
  829. {
  830. /* otherwise, create the dir and appropriate adm files */
  831. /* If no tag or date were specified on the command line,
  832. and we're not using -A, we want the subdirectory to use
  833. the tag and date, if any, of the current directory.
  834. That way, update -d will work correctly when working on
  835. a branch.
  836. We use TAG_UPDATE_DIR to undo the tag setting in
  837. update_dirleave_proc. If we did not do this, we would
  838. not correctly handle a working directory with multiple
  839. tags (and maybe we should prohibit such working
  840. directories, but they work now and we shouldn't make
  841. them stop working without more thought). */
  842. if ((tag == NULL && date == NULL) && ! aflag)
  843. {
  844. ParseTag (&tag, &date, &nonbranch);
  845. if (tag != NULL || date != NULL)
  846. tag_update_dir = xstrdup (update_dir);
  847. }
  848. make_directory (dir);
  849. Create_Admin (dir, update_dir, repository, tag, date,
  850. /* This is a guess. We will rewrite it later
  851. via WriteTag. */
  852. 0,
  853. 0,
  854. pull_template);
  855. rewrite_tag = 1;
  856. nonbranch = 0;
  857. Subdir_Register (entries, (char *) NULL, dir);
  858. }
  859. }
  860. /* Do we need to check noexec here? */
  861. else if (!pipeout)
  862. {
  863. char *cvsadmdir;
  864. /* The directory exists. Check to see if it has a CVS
  865. subdirectory. */
  866. cvsadmdir = xmalloc (strlen (dir) + 80);
  867. strcpy (cvsadmdir, dir);
  868. strcat (cvsadmdir, "/");
  869. strcat (cvsadmdir, CVSADM);
  870. if (!isdir (cvsadmdir))
  871. {
  872. /* We cannot successfully recurse into a directory without a CVS
  873. subdirectory. Generally we will have already printed
  874. "? foo". */
  875. free (cvsadmdir);
  876. return R_SKIP_ALL;
  877. }
  878. free (cvsadmdir);
  879. }
  880. /*
  881. * If we are building dirs and not going to stdout, we make sure there is
  882. * no static entries file and write the tag file as appropriate
  883. */
  884. if (!pipeout)
  885. {
  886. if (update_build_dirs)
  887. {
  888. char *tmp;
  889. tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ENTSTAT) + 10);
  890. (void) sprintf (tmp, "%s/%s", dir, CVSADM_ENTSTAT);
  891. if (unlink_file (tmp) < 0 && ! existence_error (errno))
  892. error (1, errno, "cannot remove file %s", tmp);
  893. #ifdef SERVER_SUPPORT
  894. if (server_active)
  895. server_clear_entstat (update_dir, repository);
  896. #endif
  897. free (tmp);
  898. }
  899. /* keep the CVS/Tag file current with the specified arguments */
  900. if (aflag || tag || date)
  901. {
  902. WriteTag (dir, tag, date, 0, update_dir, repository);
  903. rewrite_tag = 1;
  904. nonbranch = 0;
  905. }
  906. /* keep the CVS/Template file current */
  907. if (pull_template)
  908. {
  909. WriteTemplate (dir, update_dir);
  910. }
  911. /* initialize the ignore list for this directory */
  912. ignlist = getlist ();
  913. }
  914. /* print the warm fuzzy message */
  915. if (!quiet)
  916. error (0, 0, "Updating %s", update_dir);
  917. return R_PROCESS;
  918. }
  919. /*
  920. * update_dirleave_proc () is called back by the recursion code upon leaving
  921. * a directory. It will prune empty directories if needed and will execute
  922. * any appropriate update programs.
  923. */
  924. /* ARGSUSED */
  925. static int
  926. update_dirleave_proc (callerdat, dir, err, update_dir, entries)
  927. void *callerdat;
  928. const char *dir;
  929. int err;
  930. const char *update_dir;
  931. List *entries;
  932. {
  933. /* Delete the ignore list if it hasn't already been done. */
  934. if (ignlist)
  935. dellist (&ignlist);
  936. /* If we set the tag or date for a new subdirectory in
  937. update_dirent_proc, and we're now done with that subdirectory,
  938. undo the tag/date setting. Note that we know that the tag and
  939. date were both originally NULL in this case. */
  940. if (tag_update_dir != NULL && strcmp (update_dir, tag_update_dir) == 0)
  941. {
  942. if (tag != NULL)
  943. {
  944. free (tag);
  945. tag = NULL;
  946. }
  947. if (date != NULL)
  948. {
  949. free (date);
  950. date = NULL;
  951. }
  952. nonbranch = 0;
  953. free (tag_update_dir);
  954. tag_update_dir = NULL;
  955. }
  956. if (strchr (dir, '/') == NULL)
  957. {
  958. /* FIXME: chdir ("..") loses with symlinks. */
  959. /* Prune empty dirs on the way out - if necessary */
  960. (void) CVS_CHDIR ("..");
  961. if (update_prune_dirs && isemptydir (dir, 0))
  962. {
  963. /* I'm not sure the existence_error is actually possible (except
  964. in cases where we really should print a message), but since
  965. this code used to ignore all errors, I'll play it safe. */
  966. if (unlink_file_dir (dir) < 0 && !existence_error (errno))
  967. error (0, errno, "cannot remove %s directory", dir);
  968. Subdir_Deregister (entries, (char *) NULL, dir);
  969. }
  970. }
  971. return err;
  972. }
  973. static int isremoved PROTO ((Node *, void *));
  974. /* Returns 1 if the file indicated by node has been removed. */
  975. static int
  976. isremoved (node, closure)
  977. Node *node;
  978. void *closure;
  979. {
  980. Entnode *entdata = node->data;
  981. /* If the first character of the version is a '-', the file has been
  982. removed. */
  983. return (entdata->version && entdata->version[0] == '-') ? 1 : 0;
  984. }
  985. /* Returns 1 if the argument directory is completely empty, other than the
  986. existence of the CVS directory entry. Zero otherwise. If MIGHT_NOT_EXIST
  987. and the directory doesn't exist, then just return 0. */
  988. int
  989. isemptydir (dir, might_not_exist)
  990. const char *dir;
  991. int might_not_exist;
  992. {
  993. DIR *dirp;
  994. struct dirent *dp;
  995. if ((dirp = CVS_OPENDIR (dir)) == NULL)
  996. {
  997. if (might_not_exist && existence_error (errno))
  998. return 0;
  999. error (0, errno, "cannot open directory %s for empty check", dir);
  1000. return 0;
  1001. }
  1002. errno = 0;
  1003. while ((dp = CVS_READDIR (dirp)) != NULL)
  1004. {
  1005. if (strcmp (dp->d_name, ".") != 0
  1006. && strcmp (dp->d_name, "..") != 0)
  1007. {
  1008. if (strcmp (dp->d_name, CVSADM) != 0)
  1009. {
  1010. /* An entry other than the CVS directory. The directory
  1011. is certainly not empty. */
  1012. (void) CVS_CLOSEDIR (dirp);
  1013. return 0;
  1014. }
  1015. else
  1016. {
  1017. /* The CVS directory entry. We don't have to worry about
  1018. this unless the Entries file indicates that files have
  1019. been removed, but not committed, in this directory.
  1020. (Removing the directory would prevent people from
  1021. comitting the fact that they removed the files!) */
  1022. List *l;
  1023. int files_removed;
  1024. struct saved_cwd cwd;
  1025. if (save_cwd (&cwd))
  1026. error_exit ();
  1027. if (CVS_CHDIR (dir) < 0)
  1028. error (1, errno, "cannot change directory to %s", dir);
  1029. l = Entries_Open (0, NULL);
  1030. files_removed = walklist (l, isremoved, 0);
  1031. Entries_Close (l);
  1032. if (restore_cwd (&cwd, NULL))
  1033. error_exit ();
  1034. free_cwd (&cwd);
  1035. if (files_removed != 0)
  1036. {
  1037. /* There are files that have been removed, but not
  1038. committed! Do not consider the directory empty. */
  1039. (void) CVS_CLOSEDIR (dirp);
  1040. return 0;
  1041. }
  1042. }
  1043. }
  1044. errno = 0;
  1045. }
  1046. if (errno != 0)
  1047. {
  1048. error (0, errno, "cannot read directory %s", dir);
  1049. (void) CVS_CLOSEDIR (dirp);
  1050. return 0;
  1051. }
  1052. (void) CVS_CLOSEDIR (dirp);
  1053. return 1;
  1054. }
  1055. /*
  1056. * scratch the Entries file entry associated with a file
  1057. */
  1058. static int
  1059. scratch_file (finfo, vers)
  1060. struct file_info *finfo;
  1061. Vers_TS *vers;
  1062. {
  1063. history_write ('W', finfo->update_dir, "", finfo->file, finfo->repository);
  1064. Scratch_Entry (finfo->entries, finfo->file);
  1065. #ifdef SERVER_SUPPORT
  1066. if (server_active)
  1067. {
  1068. if (vers->ts_user == NULL)
  1069. server_scratch_entry_only ();
  1070. server_updated (finfo, vers,
  1071. SERVER_UPDATED, (mode_t) -1,
  1072. (unsigned char *) NULL,
  1073. (struct buffer *) NULL);
  1074. }
  1075. #endif
  1076. if (unlink_file (finfo->file) < 0 && ! existence_error (errno))
  1077. error (0, errno, "unable to remove %s", finfo->fullname);
  1078. else if (!server_active)
  1079. {
  1080. /* skip this step when the server is running since
  1081. * server_updated should have handled it */
  1082. /* keep the vers structure up to date in case we do a join
  1083. * - if there isn't a file, it can't very well have a version number, can it?
  1084. */
  1085. if (vers->vn_user != NULL)
  1086. {
  1087. free (vers->vn_user);
  1088. vers->vn_user = NULL;
  1089. }
  1090. if (vers->ts_user != NULL)
  1091. {
  1092. free (vers->ts_user);
  1093. vers->ts_user = NULL;
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. /*
  1099. * Check out a file.
  1100. */
  1101. static int
  1102. checkout_file (finfo, vers_ts, adding, merging, update_server)
  1103. struct file_info *finfo;
  1104. Vers_TS *vers_ts;
  1105. int adding;
  1106. int merging;
  1107. int update_server;
  1108. {
  1109. char *backup;
  1110. int set_time, retval = 0;
  1111. int status;
  1112. int file_is_dead;
  1113. struct buffer *revbuf;
  1114. backup = NULL;
  1115. revbuf = NULL;
  1116. /* Don't screw with backup files if we're going to stdout, or if
  1117. we are the server. */
  1118. if (!pipeout && !server_active)
  1119. {
  1120. backup = xmalloc (strlen (finfo->file)
  1121. + sizeof (CVSADM)
  1122. + sizeof (CVSPREFIX)
  1123. + 10);
  1124. (void) sprintf (backup, "%s/%s%s", CVSADM, CVSPREFIX, finfo->file);
  1125. if (isfile (finfo->file))
  1126. rename_file (finfo->file, backup);
  1127. else
  1128. {
  1129. /* If -f/-t wrappers are being used to wrap up a directory,
  1130. then backup might be a directory instead of just a file. */
  1131. if (unlink_file_dir (backup) < 0)
  1132. {
  1133. /* Not sure if the existence_error check is needed here. */
  1134. if (!existence_error (errno))
  1135. /* FIXME: should include update_dir in message. */
  1136. error (0, errno, "error removing %s", backup);
  1137. }
  1138. free (backup);
  1139. backup = NULL;
  1140. }
  1141. }
  1142. file_is_dead = RCS_isdead (vers_ts->srcfile, vers_ts->vn_rcs);
  1143. if (!file_is_dead)
  1144. {
  1145. /*
  1146. * if we are checking out to stdout, print a nice message to
  1147. * stderr, and add the -p flag to the command */
  1148. if (pipeout)
  1149. {
  1150. if (!quiet)
  1151. {
  1152. cvs_outerr ("\
  1153. ===================================================================\n\
  1154. Checking out ", 0);
  1155. cvs_outerr (finfo->fullname, 0);
  1156. cvs_outerr ("\n\
  1157. RCS: ", 0);
  1158. cvs_outerr (vers_ts->srcfile->path, 0);
  1159. cvs_outerr ("\n\
  1160. VERS: ", 0);
  1161. cvs_outerr (vers_ts->vn_rcs, 0);
  1162. cvs_outerr ("\n***************\n", 0);
  1163. }
  1164. }
  1165. #ifdef SERVER_SUPPORT
  1166. if (update_server
  1167. && server_active
  1168. && ! pipeout
  1169. && ! file_gzip_level
  1170. && ! joining ()
  1171. && ! wrap_name_has (finfo->file, WRAP_FROMCVS))
  1172. {
  1173. revbuf = buf_nonio_initialize ((BUFMEMERRPROC) NULL);
  1174. status = RCS_checkout (vers_ts->srcfile, (char *) NULL,
  1175. vers_ts->vn_rcs, vers_ts->tag,
  1176. vers_ts->options, RUN_TTY,
  1177. checkout_to_buffer, revbuf);
  1178. }
  1179. else
  1180. #endif
  1181. status = RCS_checkout (vers_ts->srcfile,
  1182. pipeout ? NULL : finfo->file,
  1183. vers_ts->vn_rcs, vers_ts->tag,
  1184. vers_ts->options, RUN_TTY,
  1185. (RCSCHECKOUTPROC) NULL, (void *) NULL);
  1186. }
  1187. if (file_is_dead || status == 0)
  1188. {
  1189. mode_t mode;
  1190. mode = (mode_t) -1;
  1191. if (!pipeout)
  1192. {
  1193. Vers_TS *xvers_ts;
  1194. if (revbuf != NULL && !noexec)
  1195. {
  1196. struct stat sb;
  1197. /* FIXME: We should have RCS_checkout return the mode.
  1198. That would also fix the kludge with noexec, above, which
  1199. is here only because noexec doesn't write srcfile->path
  1200. for us to stat. */
  1201. if (stat (vers_ts->srcfile->path, &sb) < 0)
  1202. {
  1203. #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
  1204. buf_free (revbuf);
  1205. #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
  1206. error (1, errno, "cannot stat %s",
  1207. vers_ts->srcfile->path);
  1208. }
  1209. mode = sb.st_mode &~ (S_IWRITE | S_IWGRP | S_IWOTH);
  1210. }
  1211. if (cvswrite
  1212. && !file_is_dead
  1213. && !fileattr_get (finfo->file, "_watched"))
  1214. {
  1215. if (revbuf == NULL)
  1216. xchmod (finfo->file, 1);
  1217. else
  1218. {
  1219. /* We know that we are the server here, so
  1220. although xchmod checks umask, we don't bother. */
  1221. mode |= (((mode & S_IRUSR) ? S_IWUSR : 0)
  1222. | ((mode & S_IRGRP) ? S_IWGRP : 0)
  1223. | ((mode & S_IROTH) ? S_IWOTH : 0));
  1224. }
  1225. }
  1226. {
  1227. /* A newly checked out file is never under the spell
  1228. of "cvs edit". If we think we were editing it
  1229. from a previous life, clean up. Would be better to
  1230. check for same the working directory instead of
  1231. same user, but that is hairy. */
  1232. struct addremove_args args;
  1233. editor_set (finfo->file, getcaller (), NULL);
  1234. memset (&args, 0, sizeof args);
  1235. args.remove_temp = 1;
  1236. watch_modify_watchers (finfo->file, &args);
  1237. }
  1238. /* set the time from the RCS file iff it was unknown before */
  1239. set_time =
  1240. (!noexec
  1241. && (vers_ts->vn_user == NULL ||
  1242. strncmp (vers_ts->ts_rcs, "Initial", 7) == 0)
  1243. && !file_is_dead);
  1244. wrap_fromcvs_process_file (finfo->file);
  1245. xvers_ts = Version_TS (finfo, options, tag, date,
  1246. force_tag_match, set_time);
  1247. if (strcmp (xvers_ts->options, "-V4") == 0)
  1248. xvers_ts->options[0] = '\0';
  1249. if (revbuf != NULL)
  1250. {
  1251. /* If we stored the file data into a buffer, then we
  1252. didn't create a file at all, so xvers_ts->ts_user
  1253. is wrong. The correct value is to have it be the
  1254. same as xvers_ts->ts_rcs, meaning that the working
  1255. file is unchanged from the RCS file.
  1256. FIXME: We should tell Version_TS not to waste time
  1257. statting the nonexistent file.
  1258. FIXME: Actually, I don't think the ts_user value
  1259. matters at all here. The only use I know of is
  1260. that it is printed in a trace message by
  1261. Server_Register. */
  1262. if (xvers_ts->ts_user != NULL)
  1263. free (xvers_ts->ts_user);
  1264. xvers_ts->ts_user = xstrdup (xvers_ts->ts_rcs);
  1265. }
  1266. (void) time (&last_register_time);
  1267. if (file_is_dead)
  1268. {
  1269. if (xvers_ts->vn_user != NULL)
  1270. {
  1271. error (0, 0,
  1272. "warning: %s is not (any longer) pertinent",
  1273. finfo->fullname);
  1274. }
  1275. Scratch_Entry (finfo->entries, finfo->file);
  1276. #ifdef SERVER_SUPPORT
  1277. if (server_active && xvers_ts->ts_user == NULL)
  1278. server_scratch_entry_only ();
  1279. #endif
  1280. /* FIXME: Rather than always unlink'ing, and ignoring the
  1281. existence_error, we should do the unlink only if
  1282. vers_ts->ts_user is non-NULL. Then there would be no
  1283. need to ignore an existence_error (for example, if the
  1284. user removes the file while we are running). */
  1285. if (unlink_file (finfo->file) < 0 && ! existence_error (errno))
  1286. {
  1287. error (0, errno, "cannot remove %s", finfo->fullname);
  1288. }
  1289. }
  1290. else
  1291. Register (finfo->entries, finfo->file,
  1292. adding ? "0" : xvers_ts->vn_rcs,
  1293. xvers_ts->ts_user, xvers_ts->options,
  1294. xvers_ts->tag, xvers_ts->date,
  1295. (char *)0); /* Clear conflict flag on fresh checkout */
  1296. /* fix up the vers structure, in case it is used by join */
  1297. if (join_rev1)
  1298. {
  1299. /* FIXME: It seems like we should be preserving ts_user
  1300. * & ts_rcs here, but setting them causes problems in
  1301. * join_file().
  1302. */
  1303. if (vers_ts->vn_user != NULL)
  1304. free (vers_ts->vn_user);
  1305. if (vers_ts->vn_rcs != NULL)
  1306. free (vers_ts->vn_rcs);
  1307. vers_ts->vn_user = xstrdup (xvers_ts->vn_rcs);
  1308. vers_ts->vn_rcs = xstrdup (xvers_ts->vn_rcs);
  1309. }
  1310. /* If this is really Update and not Checkout, recode history */
  1311. if (strcmp (cvs_cmd_name, "update") == 0)
  1312. history_write ('U', finfo->update_dir, xvers_ts->vn_rcs, finfo->file,
  1313. finfo->repository);
  1314. freevers_ts (&xvers_ts);
  1315. if (!really_quiet && !file_is_dead)
  1316. {
  1317. write_letter (finfo, 'U');
  1318. }
  1319. }
  1320. #ifdef SERVER_SUPPORT
  1321. if (update_server && server_active)
  1322. server_updated (finfo, vers_ts,
  1323. merging ? SERVER_MERGED : SERVER_UPDATED,
  1324. mode, (unsigned char *) NULL, revbuf);
  1325. #endif
  1326. }
  1327. else
  1328. {
  1329. if (backup != NULL)
  1330. {
  1331. rename_file (backup, finfo->file);
  1332. free (backup);
  1333. backup = NULL;
  1334. }
  1335. error (0, 0, "could not check out %s", finfo->fullname);
  1336. retval = status;
  1337. }
  1338. if (backup != NULL)
  1339. {
  1340. /* If -f/-t wrappers are being used to wrap up a directory,
  1341. then backup might be a directory instead of just a file. */
  1342. if (unlink_file_dir (backup) < 0)
  1343. {
  1344. /* Not sure if the existence_error check is needed here. */
  1345. if (!existence_error (errno))
  1346. /* FIXME: should include update_dir in message. */
  1347. error (0, errno, "error removing %s", backup);
  1348. }
  1349. free (backup);
  1350. }
  1351. #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
  1352. if (revbuf != NULL)
  1353. buf_free (revbuf);
  1354. #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
  1355. return retval;
  1356. }
  1357. #ifdef SERVER_SUPPORT
  1358. /* This function is used to write data from a file being checked out
  1359. into a buffer. */
  1360. static void
  1361. checkout_to_buffer (callerdat, data, len)
  1362. void *callerdat;
  1363. const char *data;
  1364. size_t len;
  1365. {
  1366. struct buffer *buf = (struct buffer *) callerdat;
  1367. buf_output (buf, data, len);
  1368. }
  1369. #endif /* SERVER_SUPPORT */
  1370. #ifdef SERVER_SUPPORT
  1371. /* This structure is used to pass information between patch_file and
  1372. patch_file_write. */
  1373. struct patch_file_data
  1374. {
  1375. /* File name, for error messages. */
  1376. const char *filename;
  1377. /* File to which to write. */
  1378. FILE *fp;
  1379. /* Whether to compute the MD5 checksum. */
  1380. int compute_checksum;
  1381. /* Data structure for computing the MD5 checksum. */
  1382. struct cvs_MD5Context context;
  1383. /* Set if the file has a final newline. */
  1384. int final_nl;
  1385. };
  1386. /* Patch a file. Runs diff. This is only done when running as the
  1387. * server. The hope is that the diff will be smaller than the file
  1388. * itself.
  1389. */
  1390. static int
  1391. patch_file (finfo, vers_ts, docheckout, file_info, checksum)
  1392. struct file_info *finfo;
  1393. Vers_TS *vers_ts;
  1394. int *docheckout;
  1395. struct stat *file_info;
  1396. unsigned char *checksum;
  1397. {
  1398. char *backup;
  1399. char *file1;
  1400. char *file2;
  1401. int retval = 0;
  1402. int retcode = 0;
  1403. int fail;
  1404. FILE *e;
  1405. struct patch_file_data data;
  1406. *docheckout = 0;
  1407. if (noexec || pipeout || joining ())
  1408. {
  1409. *docheckout = 1;
  1410. return 0;
  1411. }
  1412. /* If this file has been marked as being binary, then never send a
  1413. patch. */
  1414. if (strcmp (vers_ts->options, "-kb") == 0)
  1415. {
  1416. *docheckout = 1;
  1417. return 0;
  1418. }
  1419. /* First check that the first revision exists. If it has been nuked
  1420. by cvs admin -o, then just fall back to checking out entire
  1421. revisions. In some sense maybe we don't have to do this; after
  1422. all cvs.texinfo says "Make sure that no-one has checked out a
  1423. copy of the revision you outdate" but then again, that advice
  1424. doesn't really make complete sense, because "cvs admin" operates
  1425. on a working directory and so _someone_ will almost always have
  1426. _some_ revision checked out. */
  1427. {
  1428. char *rev;
  1429. rev = RCS_gettag (finfo->rcs, vers_ts->vn_user, 1, NULL);
  1430. if (rev == NULL)
  1431. {
  1432. *docheckout = 1;
  1433. return 0;
  1434. }
  1435. else
  1436. free (rev);
  1437. }
  1438. /* If the revision is dead, let checkout_file handle it rather
  1439. than duplicating the processing here. */
  1440. if (RCS_isdead (vers_ts->srcfile, vers_ts->vn_rcs))
  1441. {
  1442. *docheckout = 1;
  1443. return 0;
  1444. }
  1445. backup = xmalloc (strlen (finfo->file)
  1446. + sizeof (CVSADM)
  1447. + sizeof (CVSPREFIX)
  1448. + 10);
  1449. (void) sprintf (backup, "%s/%s%s", CVSADM, CVSPREFIX, finfo->file);
  1450. if (isfile (finfo->file))
  1451. rename_file (finfo->file, backup);
  1452. else
  1453. {
  1454. if (unlink_file (backup) < 0
  1455. && !existence_error (errno))
  1456. error (0, errno, "cannot remove %s", backup);
  1457. }
  1458. file1 = xmalloc (strlen (finfo->file)
  1459. + sizeof (CVSADM)
  1460. + sizeof (CVSPREFIX)
  1461. + 10);
  1462. (void) sprintf (file1, "%s/%s%s-1", CVSADM, CVSPREFIX, finfo->file);
  1463. file2 = xmalloc (strlen (finfo->file)
  1464. + sizeof (CVSADM)
  1465. + sizeof (CVSPREFIX)
  1466. + 10);
  1467. (void) sprintf (file2, "%s/%s%s-2", CVSADM, CVSPREFIX, finfo->file);
  1468. fail = 0;
  1469. /* We need to check out both revisions first, to see if either one
  1470. has a trailing newline. Because of this, we don't use rcsdiff,
  1471. but just use diff. */
  1472. e = CVS_FOPEN (file1, "w");
  1473. if (e == NULL)
  1474. error (1, errno, "cannot open %s", file1);
  1475. data.filename = file1;
  1476. data.fp = e;
  1477. data.final_nl = 0;
  1478. data.compute_checksum = 0;
  1479. /* Duplicating the client working file, so use the original sticky options.
  1480. */
  1481. retcode = RCS_checkout (vers_ts->srcfile, (char *) NULL,
  1482. vers_ts->vn_user, vers_ts->entdata->tag,
  1483. vers_ts->entdata->options, RUN_TTY,
  1484. patch_file_write, (void *) &data);
  1485. if (fclose (e) < 0)
  1486. error (1, errno, "cannot close %s", file1);
  1487. if (retcode != 0 || ! data.final_nl)
  1488. fail = 1;
  1489. if (! fail)
  1490. {
  1491. e = CVS_FOPEN (file2, "w");
  1492. if (e == NULL)
  1493. error (1, errno, "cannot open %s", file2);
  1494. data.filename = file2;
  1495. data.fp = e;
  1496. data.final_nl = 0;
  1497. data.compute_checksum = 1;
  1498. cvs_MD5Init (&data.context);
  1499. retcode = RCS_checkout (vers_ts->srcfile, (char *) NULL,
  1500. vers_ts->vn_rcs, vers_ts->tag,
  1501. vers_ts->options, RUN_TTY,
  1502. patch_file_write, (void *) &data);
  1503. if (fclose (e) < 0)
  1504. error (1, errno, "cannot close %s", file2);
  1505. if (retcode != 0 || ! data.final_nl)
  1506. fail = 1;
  1507. else
  1508. cvs_MD5Final (checksum, &data.context);
  1509. }
  1510. retcode = 0;
  1511. if (! fail)
  1512. {
  1513. int dargc = 0;
  1514. size_t darg_allocated = 0;
  1515. char **dargv = NULL;
  1516. /* If the client does not support the Rcs-diff command, we
  1517. send a context diff, and the client must invoke patch.
  1518. That approach was problematical for various reasons. The
  1519. new approach only requires running diff in the server; the
  1520. client can handle everything without invoking an external
  1521. program. */
  1522. if (!rcs_diff_patches)
  1523. /* We use -c, not -u, because that is what CVS has
  1524. traditionally used. Kind of a moot point, now that
  1525. Rcs-diff is preferred, so there is no point in making
  1526. the compatibility issues worse. */
  1527. run_add_arg_p (&dargc, &darg_allocated, &dargv, "-c");
  1528. else
  1529. /* Now that diff is librarified, we could be passing -a if
  1530. we wanted to. However, it is unclear to me whether we
  1531. would want to. Does diff -a, in any significant
  1532. percentage of cases, produce patches which are smaller
  1533. than the files it is patching? I guess maybe text
  1534. files with character sets which diff regards as
  1535. 'binary'. Conversely, do they tend to be much larger
  1536. in the bad cases? This needs some more
  1537. thought/investigation, I suspect. */
  1538. run_add_arg_p (&dargc, &darg_allocated, &dargv, "-n");
  1539. retcode = diff_exec (file1, file2, NULL, NULL, dargc, dargv,
  1540. finfo->file);
  1541. run_arg_free_p (dargc, dargv);
  1542. free (dargv);
  1543. /* A retcode of 0 means no differences. 1 means some differences. */
  1544. if (retcode != 0
  1545. && retcode != 1)
  1546. {
  1547. fail = 1;
  1548. }
  1549. }
  1550. if (! fail)
  1551. {
  1552. struct stat file2_info;
  1553. /* Check to make sure the patch is really shorter */
  1554. if (CVS_STAT (file2, &file2_info) < 0)
  1555. error (1, errno, "could not stat %s", file2);
  1556. if (CVS_STAT (finfo->file, file_info) < 0)
  1557. error (1, errno, "could not stat %s", finfo->file);
  1558. if (file2_info.st_size <= file_info->st_size)
  1559. fail = 1;
  1560. }
  1561. if (! fail)
  1562. {
  1563. # define BINARY "Binary"
  1564. char buf[sizeof BINARY];
  1565. unsigned int c;
  1566. /* Check the diff output to make sure patch will be handle it. */
  1567. e = CVS_FOPEN (finfo->file, "r");
  1568. if (e == NULL)
  1569. error (1, errno, "could not open diff output file %s",
  1570. finfo->fullname);
  1571. c = fread (buf, 1, sizeof BINARY - 1, e);
  1572. buf[c] = '\0';
  1573. if (strcmp (buf, BINARY) == 0)
  1574. {
  1575. /* These are binary files. We could use…