/contrib/cvs/src/vers_ts.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 429 lines · 284 code · 39 blank · 106 comment · 116 complexity · 130a7f3df80ca92df8c600cebcdc5389 MD5 · raw 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. #include "cvs.h"
  14. #ifdef SERVER_SUPPORT
  15. static void time_stamp_server PROTO((const char *, Vers_TS *, Entnode *));
  16. #endif
  17. /* Fill in and return a Vers_TS structure for the file FINFO. TAG and
  18. DATE are from the command line. */
  19. Vers_TS *
  20. Version_TS (finfo, options, tag, date, force_tag_match, set_time)
  21. struct file_info *finfo;
  22. /* Keyword expansion options, I think generally from the command
  23. line. Can be either NULL or "" to indicate none are specified
  24. here. */
  25. char *options;
  26. char *tag;
  27. char *date;
  28. int force_tag_match;
  29. int set_time;
  30. {
  31. Node *p;
  32. RCSNode *rcsdata;
  33. Vers_TS *vers_ts;
  34. struct stickydirtag *sdtp;
  35. Entnode *entdata;
  36. char *rcsexpand = NULL;
  37. #ifdef UTIME_EXPECTS_WRITABLE
  38. int change_it_back = 0;
  39. #endif
  40. /* get a new Vers_TS struct */
  41. vers_ts = (Vers_TS *) xmalloc (sizeof (Vers_TS));
  42. memset ((char *) vers_ts, 0, sizeof (*vers_ts));
  43. /*
  44. * look up the entries file entry and fill in the version and timestamp
  45. * if entries is NULL, there is no entries file so don't bother trying to
  46. * look it up (used by checkout -P)
  47. */
  48. if (finfo->entries == NULL)
  49. {
  50. sdtp = NULL;
  51. p = NULL;
  52. }
  53. else
  54. {
  55. p = findnode_fn (finfo->entries, finfo->file);
  56. sdtp = finfo->entries->list->data; /* list-private */
  57. }
  58. entdata = NULL;
  59. if (p != NULL)
  60. {
  61. entdata = p->data;
  62. if (entdata->type == ENT_SUBDIR)
  63. {
  64. /* According to cvs.texinfo, the various fields in the Entries
  65. file for a directory (other than the name) do not have a
  66. defined meaning. We need to pass them along without getting
  67. confused based on what is in them. Therefore we make sure
  68. not to set vn_user and the like from Entries, add.c and
  69. perhaps other code will expect these fields to be NULL for
  70. a directory. */
  71. vers_ts->entdata = entdata;
  72. }
  73. else
  74. #ifdef SERVER_SUPPORT
  75. /* An entries line with "D" in the timestamp indicates that the
  76. client sent Is-modified without sending Entry. So we want to
  77. use the entries line for the sole purpose of telling
  78. time_stamp_server what is up; we don't want the rest of CVS
  79. to think there is an entries line. */
  80. if (strcmp (entdata->timestamp, "D") != 0)
  81. #endif
  82. {
  83. vers_ts->vn_user = xstrdup (entdata->version);
  84. vers_ts->ts_rcs = xstrdup (entdata->timestamp);
  85. vers_ts->ts_conflict = xstrdup (entdata->conflict);
  86. if (!(tag || date) && !(sdtp && sdtp->aflag))
  87. {
  88. vers_ts->tag = xstrdup (entdata->tag);
  89. vers_ts->date = xstrdup (entdata->date);
  90. }
  91. vers_ts->entdata = entdata;
  92. }
  93. /* Even if we don't have an "entries line" as such
  94. (vers_ts->entdata), we want to pick up options which could
  95. have been from a Kopt protocol request. */
  96. if (!options || *options == '\0')
  97. {
  98. if (!(sdtp && sdtp->aflag))
  99. vers_ts->options = xstrdup (entdata->options);
  100. }
  101. }
  102. /* Always look up the RCS keyword mode when we have an RCS archive. It
  103. * will either be needed as a default or to avoid allowing the -k options
  104. * specified on the command line from overriding binary mode (-kb).
  105. */
  106. if (finfo->rcs != NULL)
  107. rcsexpand = RCS_getexpand (finfo->rcs);
  108. /*
  109. * -k options specified on the command line override (and overwrite)
  110. * options stored in the entries file and default options from the RCS
  111. * archive, except for binary mode (-kb).
  112. */
  113. if (options && *options != '\0')
  114. {
  115. if (vers_ts->options != NULL)
  116. free (vers_ts->options);
  117. if (rcsexpand != NULL && strcmp (rcsexpand, "b") == 0)
  118. vers_ts->options = xstrdup ("-kb");
  119. else
  120. vers_ts->options = xstrdup (options);
  121. }
  122. else if ((!vers_ts->options || *vers_ts->options == '\0')
  123. && rcsexpand != NULL)
  124. {
  125. /* If no keyword expansion was specified on command line,
  126. use whatever was in the rcs file (if there is one). This
  127. is how we, if we are the server, tell the client whether
  128. a file is binary. */
  129. if (vers_ts->options != NULL)
  130. free (vers_ts->options);
  131. vers_ts->options = xmalloc (strlen (rcsexpand) + 3);
  132. strcpy (vers_ts->options, "-k");
  133. strcat (vers_ts->options, rcsexpand);
  134. }
  135. if (!vers_ts->options)
  136. vers_ts->options = xstrdup ("");
  137. /*
  138. * if tags were specified on the command line, they override what is in
  139. * the Entries file
  140. */
  141. if (tag || date)
  142. {
  143. vers_ts->tag = xstrdup (tag);
  144. vers_ts->date = xstrdup (date);
  145. }
  146. else if (!vers_ts->entdata && (sdtp && sdtp->aflag == 0))
  147. {
  148. if (!vers_ts->tag)
  149. {
  150. vers_ts->tag = xstrdup (sdtp->tag);
  151. vers_ts->nonbranch = sdtp->nonbranch;
  152. }
  153. if (!vers_ts->date)
  154. vers_ts->date = xstrdup (sdtp->date);
  155. }
  156. /* Now look up the info on the source controlled file */
  157. if (finfo->rcs != NULL)
  158. {
  159. rcsdata = finfo->rcs;
  160. rcsdata->refcount++;
  161. }
  162. else if (finfo->repository != NULL)
  163. rcsdata = RCS_parse (finfo->file, finfo->repository);
  164. else
  165. rcsdata = NULL;
  166. if (rcsdata != NULL)
  167. {
  168. /* squirrel away the rcsdata pointer for others */
  169. vers_ts->srcfile = rcsdata;
  170. if (vers_ts->tag && strcmp (vers_ts->tag, TAG_BASE) == 0)
  171. {
  172. vers_ts->vn_rcs = xstrdup (vers_ts->vn_user);
  173. vers_ts->vn_tag = xstrdup (vers_ts->vn_user);
  174. }
  175. else
  176. {
  177. int simple;
  178. vers_ts->vn_rcs = RCS_getversion (rcsdata, vers_ts->tag,
  179. vers_ts->date, force_tag_match,
  180. &simple);
  181. if (vers_ts->vn_rcs == NULL)
  182. vers_ts->vn_tag = NULL;
  183. else if (simple)
  184. vers_ts->vn_tag = xstrdup (vers_ts->tag);
  185. else
  186. vers_ts->vn_tag = xstrdup (vers_ts->vn_rcs);
  187. }
  188. /*
  189. * If the source control file exists and has the requested revision,
  190. * get the Date the revision was checked in. If "user" exists, set
  191. * its mtime.
  192. */
  193. if (set_time && vers_ts->vn_rcs != NULL)
  194. {
  195. #ifdef SERVER_SUPPORT
  196. if (server_active)
  197. server_modtime (finfo, vers_ts);
  198. else
  199. #endif
  200. {
  201. struct utimbuf t;
  202. memset (&t, 0, sizeof (t));
  203. t.modtime = RCS_getrevtime (rcsdata, vers_ts->vn_rcs, 0, 0);
  204. if (t.modtime != (time_t) -1)
  205. {
  206. (void) time (&t.actime);
  207. #ifdef UTIME_EXPECTS_WRITABLE
  208. if (!iswritable (finfo->file))
  209. {
  210. xchmod (finfo->file, 1);
  211. change_it_back = 1;
  212. }
  213. #endif /* UTIME_EXPECTS_WRITABLE */
  214. /* This used to need to ignore existence_errors
  215. (for cases like where update.c now clears
  216. set_time if noexec, but didn't used to). I
  217. think maybe now it doesn't (server_modtime does
  218. not like those kinds of cases). */
  219. (void) utime (finfo->file, &t);
  220. #ifdef UTIME_EXPECTS_WRITABLE
  221. if (change_it_back)
  222. {
  223. xchmod (finfo->file, 0);
  224. change_it_back = 0;
  225. }
  226. #endif /* UTIME_EXPECTS_WRITABLE */
  227. }
  228. }
  229. }
  230. }
  231. /* get user file time-stamp in ts_user */
  232. if (finfo->entries != (List *) NULL)
  233. {
  234. #ifdef SERVER_SUPPORT
  235. if (server_active)
  236. time_stamp_server (finfo->file, vers_ts, entdata);
  237. else
  238. #endif
  239. vers_ts->ts_user = time_stamp (finfo->file);
  240. }
  241. return (vers_ts);
  242. }
  243. #ifdef SERVER_SUPPORT
  244. /* Set VERS_TS->TS_USER to time stamp for FILE. */
  245. /* Separate these out to keep the logic below clearer. */
  246. #define mark_lost(V) ((V)->ts_user = 0)
  247. #define mark_unchanged(V) ((V)->ts_user = xstrdup ((V)->ts_rcs))
  248. static void
  249. time_stamp_server (file, vers_ts, entdata)
  250. const char *file;
  251. Vers_TS *vers_ts;
  252. Entnode *entdata;
  253. {
  254. struct stat sb;
  255. char *cp;
  256. if (CVS_LSTAT (file, &sb) < 0)
  257. {
  258. if (! existence_error (errno))
  259. error (1, errno, "cannot stat temp file");
  260. /* Missing file means lost or unmodified; check entries
  261. file to see which.
  262. XXX FIXME - If there's no entries file line, we
  263. wouldn't be getting the file at all, so consider it
  264. lost. I don't know that that's right, but it's not
  265. clear to me that either choice is. Besides, would we
  266. have an RCS string in that case anyways? */
  267. if (entdata == NULL)
  268. mark_lost (vers_ts);
  269. else if (entdata->timestamp
  270. && entdata->timestamp[0] == '=')
  271. mark_unchanged (vers_ts);
  272. else if (entdata->conflict
  273. && entdata->conflict[0] == '=')
  274. {
  275. /* These just need matching content. Might as well minimize it. */
  276. vers_ts->ts_user = xstrdup ("");
  277. vers_ts->ts_conflict = xstrdup ("");
  278. }
  279. else if (entdata->timestamp
  280. && (entdata->timestamp[0] == 'M'
  281. || entdata->timestamp[0] == 'D')
  282. && entdata->timestamp[1] == '\0')
  283. vers_ts->ts_user = xstrdup ("Is-modified");
  284. else
  285. mark_lost (vers_ts);
  286. }
  287. else if (sb.st_mtime == 0)
  288. {
  289. /* We shouldn't reach this case any more! */
  290. abort ();
  291. }
  292. else
  293. {
  294. struct tm *tm_p;
  295. vers_ts->ts_user = xmalloc (25);
  296. /* We want to use the same timestamp format as is stored in the
  297. st_mtime. For unix (and NT I think) this *must* be universal
  298. time (UT), so that files don't appear to be modified merely
  299. because the timezone has changed. For VMS, or hopefully other
  300. systems where gmtime returns NULL, the modification time is
  301. stored in local time, and therefore it is not possible to cause
  302. st_mtime to be out of sync by changing the timezone. */
  303. tm_p = gmtime (&sb.st_mtime);
  304. cp = tm_p ? asctime (tm_p) : ctime (&sb.st_mtime);
  305. cp[24] = 0;
  306. /* Fix non-standard format. */
  307. if (cp[8] == '0') cp[8] = ' ';
  308. (void) strcpy (vers_ts->ts_user, cp);
  309. }
  310. }
  311. #endif /* SERVER_SUPPORT */
  312. /*
  313. * Gets the time-stamp for the file "file" and returns it in space it
  314. * allocates
  315. */
  316. char *
  317. time_stamp (file)
  318. const char *file;
  319. {
  320. struct stat sb;
  321. char *cp;
  322. char *ts = NULL;
  323. time_t mtime = 0L;
  324. if (!CVS_LSTAT (file, &sb))
  325. {
  326. mtime = sb.st_mtime;
  327. }
  328. else if (! existence_error (errno))
  329. error (0, errno, "cannot lstat %s", file);
  330. /* If it's a symlink, return whichever is the newest mtime of
  331. the link and its target, for safety.
  332. */
  333. if (!CVS_STAT (file, &sb))
  334. {
  335. if (mtime < sb.st_mtime)
  336. mtime = sb.st_mtime;
  337. }
  338. else if (! existence_error (errno))
  339. error (0, errno, "cannot stat %s", file);
  340. if (mtime)
  341. {
  342. struct tm *tm_p;
  343. ts = xmalloc (25);
  344. /* We want to use the same timestamp format as is stored in the
  345. st_mtime. For unix (and NT I think) this *must* be universal
  346. time (UT), so that files don't appear to be modified merely
  347. because the timezone has changed. For VMS, or hopefully other
  348. systems where gmtime returns NULL, the modification time is
  349. stored in local time, and therefore it is not possible to cause
  350. st_mtime to be out of sync by changing the timezone. */
  351. tm_p = gmtime (&sb.st_mtime);
  352. cp = tm_p ? asctime (tm_p) : ctime (&sb.st_mtime);
  353. cp[24] = 0;
  354. /* Fix non-standard format. */
  355. if (cp[8] == '0') cp[8] = ' ';
  356. (void) strcpy (ts, cp);
  357. }
  358. return (ts);
  359. }
  360. /*
  361. * free up a Vers_TS struct
  362. */
  363. void
  364. freevers_ts (versp)
  365. Vers_TS **versp;
  366. {
  367. if ((*versp)->srcfile)
  368. freercsnode (&((*versp)->srcfile));
  369. if ((*versp)->vn_user)
  370. free ((*versp)->vn_user);
  371. if ((*versp)->vn_rcs)
  372. free ((*versp)->vn_rcs);
  373. if ((*versp)->vn_tag)
  374. free ((*versp)->vn_tag);
  375. if ((*versp)->ts_user)
  376. free ((*versp)->ts_user);
  377. if ((*versp)->ts_rcs)
  378. free ((*versp)->ts_rcs);
  379. if ((*versp)->options)
  380. free ((*versp)->options);
  381. if ((*versp)->tag)
  382. free ((*versp)->tag);
  383. if ((*versp)->date)
  384. free ((*versp)->date);
  385. if ((*versp)->ts_conflict)
  386. free ((*versp)->ts_conflict);
  387. free ((char *) *versp);
  388. *versp = (Vers_TS *) NULL;
  389. }