2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
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.
18 static void time_stamp_server (const char *, Vers_TS *, Entnode *);
21 /* Fill in and return a Vers_TS structure for the file FINFO.
24 * finfo struct file_info data about the file to be examined.
25 * options Keyword expansion options, I think generally from the
26 * command line. Can be either NULL or "" to indicate
27 * none are specified here.
28 * tag Tag specified by user on the command line (via -r).
29 * date Date specified by user on the command line (via -D).
30 * force_tag_match If set and TAG is specified, will only set RET->vn_rcs
31 * based on TAG. Otherwise, if TAG is specified and does
32 * not exist in the file, RET->vn_rcs will be set to the
34 * set_time If set, set the last modification time of the user file
35 * specified by FINFO to the checkin time of RET->vn_rcs.
38 * Vers_TS structure for FINFO.
41 Version_TS (struct file_info *finfo, char *options, char *tag, char *date,
42 int force_tag_match, int set_time)
47 struct stickydirtag *sdtp;
49 char *rcsexpand = NULL;
51 /* get a new Vers_TS struct */
53 vers_ts = xmalloc (sizeof (Vers_TS));
54 memset (vers_ts, 0, sizeof (*vers_ts));
57 * look up the entries file entry and fill in the version and timestamp
58 * if entries is NULL, there is no entries file so don't bother trying to
59 * look it up (used by checkout -P)
61 if (finfo->entries == NULL)
68 p = findnode_fn (finfo->entries, finfo->file);
69 sdtp = finfo->entries->list->data; /* list-private */
80 if (entdata->type == ENT_SUBDIR)
82 /* According to cvs.texinfo, the various fields in the Entries
83 file for a directory (other than the name) do not have a
84 defined meaning. We need to pass them along without getting
85 confused based on what is in them. Therefore we make sure
86 not to set vn_user and the like from Entries, add.c and
87 perhaps other code will expect these fields to be NULL for
89 vers_ts->entdata = entdata;
93 /* An entries line with "D" in the timestamp indicates that the
94 client sent Is-modified without sending Entry. So we want to
95 use the entries line for the sole purpose of telling
96 time_stamp_server what is up; we don't want the rest of CVS
97 to think there is an entries line. */
98 if (strcmp (entdata->timestamp, "D") != 0)
101 vers_ts->vn_user = xstrdup (entdata->version);
102 vers_ts->ts_rcs = xstrdup (entdata->timestamp);
103 vers_ts->ts_conflict = xstrdup (entdata->conflict);
104 if (!(tag || date) && !(sdtp && sdtp->aflag))
106 vers_ts->tag = xstrdup (entdata->tag);
107 vers_ts->date = xstrdup (entdata->date);
109 vers_ts->entdata = entdata;
111 /* Even if we don't have an "entries line" as such
112 (vers_ts->entdata), we want to pick up options which could
113 have been from a Kopt protocol request. */
114 if (!options || *options == '\0')
116 if (!(sdtp && sdtp->aflag))
117 vers_ts->options = xstrdup (entdata->options);
121 /* Always look up the RCS keyword mode when we have an RCS archive. It
122 * will either be needed as a default or to avoid allowing the -k options
123 * specified on the command line from overriding binary mode (-kb).
125 if (finfo->rcs != NULL)
126 rcsexpand = RCS_getexpand (finfo->rcs);
129 * -k options specified on the command line override (and overwrite)
130 * options stored in the entries file and default options from the RCS
131 * archive, except for binary mode (-kb).
133 if (options && *options != '\0')
135 if (vers_ts->options != NULL)
136 free (vers_ts->options);
137 if (rcsexpand != NULL && strcmp (rcsexpand, "b") == 0)
138 vers_ts->options = xstrdup ("-kb");
140 vers_ts->options = xstrdup (options);
142 else if ((!vers_ts->options || *vers_ts->options == '\0')
143 && rcsexpand != NULL)
145 /* If no keyword expansion was specified on command line,
146 use whatever was in the rcs file (if there is one). This
147 is how we, if we are the server, tell the client whether
149 if (vers_ts->options != NULL)
150 free (vers_ts->options);
151 vers_ts->options = xmalloc (strlen (rcsexpand) + 3);
152 strcpy (vers_ts->options, "-k");
153 strcat (vers_ts->options, rcsexpand);
155 if (!vers_ts->options)
156 vers_ts->options = xstrdup ("");
159 * if tags were specified on the command line, they override what is in
164 vers_ts->tag = xstrdup (tag);
165 vers_ts->date = xstrdup (date);
167 else if (!vers_ts->entdata && (sdtp && sdtp->aflag == 0))
171 vers_ts->tag = xstrdup (sdtp->tag);
172 vers_ts->nonbranch = sdtp->nonbranch;
175 vers_ts->date = xstrdup (sdtp->date);
178 /* Now look up the info on the source controlled file */
179 if (finfo->rcs != NULL)
181 rcsdata = finfo->rcs;
184 else if (finfo->repository != NULL)
185 rcsdata = RCS_parse (finfo->file, finfo->repository);
191 /* squirrel away the rcsdata pointer for others */
192 vers_ts->srcfile = rcsdata;
194 if (vers_ts->tag && strcmp (vers_ts->tag, TAG_BASE) == 0)
196 vers_ts->vn_rcs = xstrdup (vers_ts->vn_user);
197 vers_ts->vn_tag = xstrdup (vers_ts->vn_user);
203 vers_ts->vn_rcs = RCS_getversion (rcsdata, vers_ts->tag,
204 vers_ts->date, force_tag_match,
206 if (vers_ts->vn_rcs == NULL)
207 vers_ts->vn_tag = NULL;
209 vers_ts->vn_tag = xstrdup (vers_ts->tag);
211 vers_ts->vn_tag = xstrdup (vers_ts->vn_rcs);
215 * If the source control file exists and has the requested revision,
216 * get the Date the revision was checked in. If "user" exists, set
219 if (set_time && vers_ts->vn_rcs != NULL)
221 #ifdef SERVER_SUPPORT
223 server_modtime (finfo, vers_ts);
229 memset (&t, 0, sizeof (t));
230 t.modtime = RCS_getrevtime (rcsdata, vers_ts->vn_rcs, 0, 0);
231 if (t.modtime != (time_t) -1)
233 #ifdef UTIME_EXPECTS_WRITABLE
234 int change_it_back = 0;
237 (void) time (&t.actime);
239 #ifdef UTIME_EXPECTS_WRITABLE
240 if (!iswritable (finfo->file))
242 xchmod (finfo->file, 1);
245 #endif /* UTIME_EXPECTS_WRITABLE */
247 /* This used to need to ignore existence_errors
248 (for cases like where update.c now clears
249 set_time if noexec, but didn't used to). I
250 think maybe now it doesn't (server_modtime does
251 not like those kinds of cases). */
252 (void) utime (finfo->file, &t);
254 #ifdef UTIME_EXPECTS_WRITABLE
256 xchmod (finfo->file, 0);
257 #endif /* UTIME_EXPECTS_WRITABLE */
263 /* get user file time-stamp in ts_user */
264 if (finfo->entries != NULL)
266 #ifdef SERVER_SUPPORT
268 time_stamp_server (finfo->file, vers_ts, entdata);
272 vers_ts->ts_user = time_stamp (finfo->file);
273 vers_ts->ts_user_ists = 1;
282 #ifdef SERVER_SUPPORT
284 /* Set VERS_TS->TS_USER to time stamp for FILE. */
286 /* Separate these out to keep the logic below clearer. */
287 #define mark_lost(V) ((V)->ts_user = 0)
288 #define mark_unchanged(V) ((V)->ts_user = xstrdup ((V)->ts_rcs))
291 time_stamp_server (const char *file, Vers_TS *vers_ts, Entnode *entdata)
296 TRACE (TRACE_FUNCTION, "time_stamp_server (%s, %s, %s, %s)",
298 entdata && entdata->version ? entdata->version : "(null)",
299 entdata && entdata->timestamp ? entdata->timestamp : "(null)",
300 entdata && entdata->conflict ? entdata->conflict : "(null)");
302 if (lstat (file, &sb) < 0)
304 if (! existence_error (errno))
305 error (1, errno, "cannot stat temp file");
307 /* Missing file means lost or unmodified; check entries
310 XXX FIXME - If there's no entries file line, we
311 wouldn't be getting the file at all, so consider it
312 lost. I don't know that that's right, but it's not
313 clear to me that either choice is. Besides, would we
314 have an RCS string in that case anyways? */
317 else if (entdata->timestamp
318 && entdata->timestamp[0] == '='
319 && entdata->timestamp[1] == '\0')
320 mark_unchanged (vers_ts);
321 else if (entdata->conflict
322 && entdata->conflict[0] == '=')
324 /* These just need matching content. Might as well minimize it. */
325 vers_ts->ts_user = xstrdup ("");
326 vers_ts->ts_conflict = xstrdup ("");
328 else if (entdata->timestamp
329 && (entdata->timestamp[0] == 'M'
330 || entdata->timestamp[0] == 'D')
331 && entdata->timestamp[1] == '\0')
332 vers_ts->ts_user = xstrdup ("Is-modified");
336 else if (sb.st_mtime == 0)
338 /* We shouldn't reach this case any more! */
345 vers_ts->ts_user_ists = 1;
346 vers_ts->ts_user = xmalloc (25);
347 /* We want to use the same timestamp format as is stored in the
348 st_mtime. For unix (and NT I think) this *must* be universal
349 time (UT), so that files don't appear to be modified merely
350 because the timezone has changed. For VMS, or hopefully other
351 systems where gmtime returns NULL, the modification time is
352 stored in local time, and therefore it is not possible to cause
353 st_mtime to be out of sync by changing the timezone. */
354 tm_p = gmtime (&sb.st_mtime);
355 cp = tm_p ? asctime (tm_p) : ctime (&sb.st_mtime);
357 /* Fix non-standard format. */
358 if (cp[8] == '0') cp[8] = ' ';
359 (void) strcpy (vers_ts->ts_user, cp);
363 #endif /* SERVER_SUPPORT */
367 /* Given a UNIX seconds since the epoch, return a string in the format used by
372 * UNIXTIME The timestamp to be formatted.
375 * A freshly allocated string the caller is responsible for disposing of.
378 entries_time (time_t unixtime)
383 /* We want to use the same timestamp format as is stored in the
384 st_mtime. For unix (and NT I think) this *must* be universal
385 time (UT), so that files don't appear to be modified merely
386 because the timezone has changed. For VMS, or hopefully other
387 systems where gmtime returns NULL, the modification time is
388 stored in local time, and therefore it is not possible to cause
389 st_mtime to be out of sync by changing the timezone. */
390 tm_p = gmtime (&unixtime);
391 cp = tm_p ? asctime (tm_p) : ctime (&unixtime);
392 /* Get rid of the EOL */
394 /* Fix non-standard format. */
395 if (cp[8] == '0') cp[8] = ' ';
397 return Xasprintf ("%s", cp);
403 unix_time_stamp (const char *file)
408 if (!lstat (file, &sb))
413 /* If it's a symlink, return whichever is the newest mtime of
414 the link and its target, for safety.
416 if (!stat (file, &sb))
418 if (mtime < sb.st_mtime)
428 * Gets the time-stamp for the file "file" and returns it in space it
432 time_stamp (const char *file)
434 time_t mtime = unix_time_stamp (file);
435 return mtime ? entries_time (mtime) : NULL;
441 * free up a Vers_TS struct
444 freevers_ts (Vers_TS **versp)
446 if ((*versp)->srcfile)
447 freercsnode (&((*versp)->srcfile));
448 if ((*versp)->vn_user)
449 free ((*versp)->vn_user);
450 if ((*versp)->vn_rcs)
451 free ((*versp)->vn_rcs);
452 if ((*versp)->vn_tag)
453 free ((*versp)->vn_tag);
454 if ((*versp)->ts_user)
455 free ((*versp)->ts_user);
456 if ((*versp)->ts_rcs)
457 free ((*versp)->ts_rcs);
458 if ((*versp)->options)
459 free ((*versp)->options);
461 free ((*versp)->tag);
463 free ((*versp)->date);
464 if ((*versp)->ts_conflict)
465 free ((*versp)->ts_conflict);
466 free ((char *) *versp);