1 /* Implementation for "cvs edit", "cvs watch on", and related commands
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details. */
20 static int watch_onoff (int, char **);
22 static bool check_edited = false;
23 static int setting_default;
24 static int turning_on;
26 static bool setting_tedit;
27 static bool setting_tunedit;
28 static bool setting_tcommit;
33 onoff_fileproc (void *callerdat, struct file_info *finfo)
35 fileattr_set (finfo->file, "_watched", turning_on ? "" : NULL);
42 onoff_filesdoneproc (void *callerdat, int err, const char *repository,
43 const char *update_dir, List *entries)
46 fileattr_set (NULL, "_watched", turning_on ? "" : NULL);
53 watch_onoff (int argc, char **argv)
60 while ((c = getopt (argc, argv, "+lR")) != -1)
80 if (current_parsed_root->isremote)
89 send_files (argc, argv, local, 0, SEND_NO_CONTENTS);
90 send_file_names (argc, argv, SEND_EXPAND_WILD);
91 send_to_server (turning_on ? "watch-on\012" : "watch-off\012", 0);
92 return get_responses_and_close ();
94 #endif /* CLIENT_SUPPORT */
96 setting_default = (argc <= 0);
98 lock_tree_promotably (argc, argv, local, W_LOCAL, 0);
100 err = start_recursion (onoff_fileproc, onoff_filesdoneproc, NULL, NULL,
101 NULL, argc, argv, local, W_LOCAL, 0, CVS_LOCK_WRITE,
109 watch_on (int argc, char **argv)
112 return watch_onoff (argc, argv);
116 watch_off (int argc, char **argv)
119 return watch_onoff (argc, argv);
125 dummy_fileproc (void *callerdat, struct file_info *finfo)
127 /* This is a pretty hideous hack, but the gist of it is that recurse.c
128 won't call notify_check unless there is a fileproc, so we can't just
129 pass NULL for fileproc. */
135 /* Check for and process notifications. Local only. I think that doing
136 this as a fileproc is the only way to catch all the
137 cases (e.g. foo/bar.c), even though that means checking over and over
138 for the same CVSADM_NOTIFY file which we removed the first time we
139 processed the directory. */
141 ncheck_fileproc (void *callerdat, struct file_info *finfo)
153 /* We send notifications even if noexec. I'm not sure which behavior
156 fp = CVS_FOPEN (CVSADM_NOTIFY, "r");
159 if (!existence_error (errno))
160 error (0, errno, "cannot open %s", CVSADM_NOTIFY);
164 while (getline (&line, &line_len, fp) > 0)
166 notif_type = line[0];
167 if (notif_type == '\0')
170 cp = strchr (filename, '\t');
175 cp = strchr (val, '\t');
179 cp = strchr (cp, '\t');
183 cp = strchr (cp, '\t');
188 cp = strchr (cp, '\n');
193 notify_do (notif_type, filename, finfo->update_dir, getcaller (), val,
194 watches, finfo->repository);
199 error (0, errno, "cannot read %s", CVSADM_NOTIFY);
201 error (0, errno, "cannot close %s", CVSADM_NOTIFY);
203 if ( CVS_UNLINK (CVSADM_NOTIFY) < 0)
204 error (0, errno, "cannot remove %s", CVSADM_NOTIFY);
211 /* Look through the CVSADM_NOTIFY file and process each item there
214 send_notifications (int argc, char **argv, int local)
218 #ifdef CLIENT_SUPPORT
219 /* OK, we've done everything which needs to happen on the client side.
220 Now we can try to contact the server; if we fail, then the
221 notifications stay in CVSADM_NOTIFY to be sent next time. */
222 if (current_parsed_root->isremote)
224 if (strcmp (cvs_cmd_name, "release") != 0)
230 err += start_recursion (dummy_fileproc, NULL, NULL, NULL, NULL, argc,
231 argv, local, W_LOCAL, 0, 0, NULL, 0, NULL);
233 send_to_server ("noop\012", 0);
234 if (strcmp (cvs_cmd_name, "release") == 0)
235 err += get_server_responses ();
237 err += get_responses_and_close ();
244 err += start_recursion (ncheck_fileproc, NULL, NULL, NULL, NULL, argc,
245 argv, local, W_LOCAL, 0, CVS_LOCK_WRITE, NULL,
254 void editors_output (const char *fullname, const char *p)
256 cvs_output (fullname, 0);
260 cvs_output ("\t", 1);
261 while (*p != '>' && *p != '\0')
265 /* Only happens if attribute is misformed. */
266 cvs_output ("\n", 1);
270 cvs_output ("\t", 1);
273 while (*p != '+' && *p != ',' && *p != '\0')
277 cvs_output ("\n", 1);
286 cvs_output ("\t", 1);
288 cvs_output ("\n", 1);
294 static int find_editors_and_output (struct file_info *finfo)
298 them = fileattr_get0 (finfo->file, "_editors");
302 editors_output (finfo->fullname, them);
309 /* Handle the client-side details of editing a file.
311 * These args could be const but this needs to fit the call_in_directory API.
314 edit_file (void *data, List *ent_list, const char *short_pathname,
315 const char *filename)
318 struct file_info finfo;
321 xchmod (filename, 1);
323 mkdir_if_needed (CVSADM_BASE);
324 basefilename = Xasprintf ("%s/%s", CVSADM_BASE, filename);
325 copy_file (filename, basefilename);
328 node = findnode_fn (ent_list, filename);
331 finfo.file = filename;
332 finfo.fullname = short_pathname;
333 finfo.update_dir = dir_name (short_pathname);
334 base_register (&finfo, ((Entnode *) node->data)->version);
335 free ((char *)finfo.update_dir);
342 edit_fileproc (void *callerdat, struct file_info *finfo)
349 #if defined (CLIENT_SUPPORT)
350 assert (!(current_parsed_root->isremote && check_edited));
351 #else /* !CLIENT_SUPPORT */
352 assert (!check_edited);
353 #endif /* CLIENT_SUPPORT */
358 vers = Version_TS (finfo, NULL, NULL, NULL, 1, 0);
362 error (0, 0, "no such file %s; ignored", finfo->fullname);
366 #ifdef CLIENT_SUPPORT
367 if (!current_parsed_root->isremote)
368 #endif /* CLIENT_SUPPORT */
370 char *editors = fileattr_get0 (finfo->file, "_editors");
375 /* In the !CHECK_EDIT case, this message is printed by
379 editors_output (finfo->fullname, editors);
380 /* Now warn the user if we skip the file, then return. */
382 error (0, 0, "Skipping file `%s' due to existing editors.",
390 fp = xfopen (CVSADM_NOTIFY, "a");
393 ascnow = asctime (gmtime (&now));
395 /* Fix non-standard format. */
396 if (ascnow[8] == '0') ascnow[8] = ' ';
397 fprintf (fp, "E%s\t%s -0000\t%s\t%s\t", finfo->file,
398 ascnow, hostname, CurDir);
409 if (finfo->update_dir[0] == '\0')
410 error (0, errno, "cannot close %s", CVSADM_NOTIFY);
412 error (0, errno, "cannot close %s/%s", finfo->update_dir,
416 /* Now stash the file away in CVSADM so that unedit can revert even if
417 it can't communicate with the server. We stash away a writable
418 copy so that if the user removes the working file, then restores it
419 with "cvs update" (which clears _editors but does not update
420 CVSADM_BASE), then a future "cvs edit" can still win. */
421 /* Could save a system call by only calling mkdir_if_needed if
422 trying to create the output file fails. But copy_file isn't
423 set up to facilitate that. */
424 #ifdef SERVER_SUPPORT
426 server_edit_file (finfo);
428 #endif /* SERVER_SUPPORT */
429 edit_file (NULL, finfo->entries, finfo->fullname, finfo->file);
434 static const char *const edit_usage[] =
436 "Usage: %s %s [-cflR] [files...]\n",
437 "-c: Check that working files are unedited\n",
438 "-f: Force edit if working files are edited (default)\n",
439 "-l: Local directory only, not recursive\n",
440 "-R: Process directories recursively\n",
441 "-a: Specify what actions for temporary watch, one of\n",
442 " edit,unedit,commit,all,none\n",
443 "(Specify the --help global option for a list of other help options)\n",
448 edit (int argc, char **argv)
453 bool a_omitted, a_all, a_none;
461 setting_tedit = false;
462 setting_tunedit = false;
463 setting_tcommit = false;
465 while ((c = getopt (argc, argv, "+cflRa:")) != -1)
473 check_edited = false;
483 if (strcmp (optarg, "edit") == 0)
484 setting_tedit = true;
485 else if (strcmp (optarg, "unedit") == 0)
486 setting_tunedit = true;
487 else if (strcmp (optarg, "commit") == 0)
488 setting_tcommit = true;
489 else if (strcmp (optarg, "all") == 0)
493 setting_tedit = true;
494 setting_tunedit = true;
495 setting_tcommit = true;
497 else if (strcmp (optarg, "none") == 0)
501 setting_tedit = false;
502 setting_tunedit = false;
503 setting_tcommit = false;
517 if (strpbrk (hostname, "+,>;=\t\n") != NULL)
519 "host name (%s) contains an invalid character (+,>;=\\t\\n)",
521 if (strpbrk (CurDir, "+,>;=\t\n") != NULL)
523 "current directory (%s) contains an invalid character (+,>;=\\t\\n)",
526 #ifdef CLIENT_SUPPORT
527 if (check_edited && current_parsed_root->isremote)
529 /* When CHECK_EDITED, we might as well contact the server and let it do
530 * the work since we don't want an edit unless we know it is safe.
532 * When !CHECK_EDITED, we set up notifications and then attempt to
533 * contact the server in order to allow disconnected edits.
537 if (!supported_request ("edit"))
538 error (1, 0, "Server does not support enforced advisory locks.");
542 send_to_server ("Hostname ", 0);
543 send_to_server (hostname, 0);
544 send_to_server ("\012", 1);
545 send_to_server ("LocalDir ", 0);
546 send_to_server (CurDir, 0);
547 send_to_server ("\012", 1);
555 option_with_arg ("-a", "all");
557 option_with_arg ("-a", "none");
561 option_with_arg ("-a", "edit");
563 option_with_arg ("-a", "unedit");
565 option_with_arg ("-a", "commit");
569 send_files (argc, argv, local, 0, SEND_NO_CONTENTS);
570 send_file_names (argc, argv, SEND_EXPAND_WILD);
571 send_to_server ("edit\012", 0);
572 return get_responses_and_close ();
574 #endif /* CLIENT_SUPPORT */
576 /* Now, either SERVER_ACTIVE, local mode, or !CHECK_EDITED. */
580 setting_tedit = true;
581 setting_tunedit = true;
582 setting_tcommit = true;
585 TRACE (TRACE_DATA, "edit(): EUC: %d%d%d edit-check: %d",
586 setting_tedit, setting_tunedit, setting_tcommit, check_edited);
588 err = start_recursion (edit_fileproc, NULL, NULL, NULL, NULL, argc, argv,
589 local, W_LOCAL, 0, 0, NULL, 0, NULL);
591 err += send_notifications (argc, argv, local);
596 static int unedit_fileproc (void *callerdat, struct file_info *finfo);
599 unedit_fileproc (void *callerdat, struct file_info *finfo)
604 char *basefilename = NULL;
609 basefilename = Xasprintf ("%s/%s", CVSADM_BASE, finfo->file);
610 if (!isfile (basefilename))
612 /* This file apparently was never cvs edit'd (e.g. we are uneditting
613 a directory where only some of the files were cvs edit'd. */
618 if (xcmp (finfo->file, basefilename) != 0)
620 printf ("%s has been modified; revert changes? ", finfo->fullname);
630 rename_file (basefilename, finfo->file);
633 fp = xfopen (CVSADM_NOTIFY, "a");
636 ascnow = asctime (gmtime (&now));
638 /* Fix non-standard format. */
639 if (ascnow[8] == '0') ascnow[8] = ' ';
640 fprintf (fp, "U%s\t%s -0000\t%s\t%s\t\n", finfo->file,
641 ascnow, hostname, CurDir);
645 if (finfo->update_dir[0] == '\0')
646 error (0, errno, "cannot close %s", CVSADM_NOTIFY);
648 error (0, errno, "cannot close %s/%s", finfo->update_dir,
652 /* Now update the revision number in CVS/Entries from CVS/Baserev.
653 The basic idea here is that we are reverting to the revision
654 that the user edited. If we wanted "cvs update" to update
655 CVS/Base as we go along (so that an unedit could revert to the
656 current repository revision), we would need:
658 update (or all send_files?) (client) needs to send revision in
659 new Entry-base request. update (server/local) needs to check
660 revision against repository and send new Update-base response
661 (like Update-existing in that the file already exists. While
662 we are at it, might try to clean up the syntax by having the
663 mode only in a "Mode" response, not in the Update-base itself). */
669 baserev = base_get (finfo);
670 node = findnode_fn (finfo->entries, finfo->file);
671 /* The case where node is NULL probably should be an error or
672 something, but I don't want to think about it too hard right
676 entdata = node->data;
679 /* This can only happen if the CVS/Baserev file got
680 corrupted. We suspect it might be possible if the
681 user interrupts CVS, although I haven't verified
683 error (0, 0, "%s not mentioned in %s", finfo->fullname,
686 /* Since we don't know what revision the file derives from,
687 keeping it around would be asking for trouble. */
688 if (unlink_file (finfo->file) < 0)
689 error (0, errno, "cannot remove %s", finfo->fullname);
691 /* This is cheesy, in a sense; why shouldn't we do the
692 update for the user? However, doing that would require
693 contacting the server, so maybe this is OK. */
694 error (0, 0, "run update to complete the unedit");
697 Register (finfo->entries, finfo->file, baserev, entdata->timestamp,
698 entdata->options, entdata->tag, entdata->date,
702 base_deregister (finfo);
705 xchmod (finfo->file, 0);
709 static const char *const unedit_usage[] =
711 "Usage: %s %s [-lR] [files...]\n",
712 "-l: Local directory only, not recursive\n",
713 "-R: Process directories recursively\n",
714 "(Specify the --help global option for a list of other help options)\n",
719 unedit (int argc, char **argv)
726 usage (unedit_usage);
729 while ((c = getopt (argc, argv, "+lR")) != -1)
741 usage (unedit_usage);
748 /* No need to readlock since we aren't doing anything to the
750 err = start_recursion (unedit_fileproc, NULL, NULL, NULL, NULL, argc, argv,
751 local, W_LOCAL, 0, 0, NULL, 0, NULL);
753 err += send_notifications (argc, argv, local);
761 mark_up_to_date (const char *file)
765 /* The file is up to date, so we better get rid of an out of
766 date file in CVSADM_BASE. */
767 base = Xasprintf ("%s/%s", CVSADM_BASE, file);
768 if (unlink_file (base) < 0 && ! existence_error (errno))
769 error (0, errno, "cannot remove %s", file);
776 editor_set (const char *filename, const char *editor, const char *val)
781 edlist = fileattr_get0 (filename, "_editors");
782 newlist = fileattr_modify (edlist, editor, val, '>', ',');
783 /* If the attributes is unchanged, don't rewrite the attribute file. */
784 if (!((edlist == NULL && newlist == NULL)
787 && strcmp (edlist, newlist) == 0)))
788 fileattr_set (filename, "_editors", newlist);
795 struct notify_proc_args {
796 /* What kind of notification, "edit", "tedit", etc. */
798 /* User who is running the command which causes notification. */
800 /* User to be notified. */
801 const char *notifyee;
809 notify_proc (const char *repository, const char *filter, void *closure)
813 const char *srepos = Short_Repository (repository);
814 struct notify_proc_args *args = closure;
817 * Cast any NULL arguments as appropriate pointers as this is an
818 * stdarg function and we need to be certain the caller gets what
821 cmdline = format_cmdline (
822 #ifdef SUPPORT_OLD_INFO_FMT_STRINGS
824 #endif /* SUPPORT_OLD_INFO_FMT_STRINGS */
826 "c", "s", cvs_cmd_name,
827 #ifdef SERVER_SUPPORT
828 "R", "s", referrer ? referrer->original : "NONE",
829 #endif /* SERVER_SUPPORT */
831 "r", "s", current_parsed_root->directory,
832 "s", "s", args->notifyee,
834 if (!cmdline || !strlen (cmdline))
836 if (cmdline) free (cmdline);
837 error (0, 0, "pretag proc resolved to the empty string!");
841 pipefp = run_popen (cmdline, "w");
844 error (0, errno, "cannot write entry to notify filter: %s", cmdline);
849 fprintf (pipefp, "%s %s\n---\n", srepos, args->file);
850 fprintf (pipefp, "Triggered %s watch on %s\n", args->type, repository);
851 fprintf (pipefp, "By %s\n", args->who);
853 /* Lots more potentially useful information we could add here; see
854 logfile_write for inspiration. */
857 return pclose (pipefp);
862 /* FIXME: this function should have a way to report whether there was
863 an error so that server.c can know whether to report Notified back
866 notify_do (int type, const char *filename, const char *update_dir,
867 const char *who, const char *val, const char *watches,
868 const char *repository)
870 static struct addremove_args blank;
871 struct addremove_args args;
877 /* Print out information on current editors if we were called during an
880 if (type == 'E' && !check_edited && !quiet)
882 char *editors = fileattr_get0 (filename, "_editors");
885 /* In the CHECK_EDIT case, this message is printed by
886 * edit_check. It needs to be done here too since files
887 * which are found to be edited when CHECK_EDIT are not
888 * added to the notify list.
891 if (update_dir && *update_dir)
892 tmp = Xasprintf ("%s/%s", update_dir, filename);
896 editors_output (tmp, editors);
898 if (update_dir && *update_dir) free ((char *)tmp);
903 /* Initialize fields to 0, NULL, or 0.0. */
908 if (strpbrk (val, ",>;=\n") != NULL)
910 error (0, 0, "invalid character in editor value");
913 editor_set (filename, who, val);
917 editor_set (filename, who, NULL);
923 watchers = fileattr_get0 (filename, "_watchers");
932 endp = strchr (p, '>');
935 nextp = strchr (p, ',');
937 if ((size_t)(endp - p) == strlen (who) && strncmp (who, p, endp - p) == 0)
939 /* Don't notify user of their own changes. Would perhaps
940 be better to check whether it is the same working
941 directory, not the same user, but that is hairy. */
942 p = nextp == NULL ? nextp : nextp + 1;
946 /* Now we point q at a string which looks like
947 "edit+unedit+commit,"... and walk down it. */
952 endq = strchr (q, '+');
953 if (endq == NULL || (nextp != NULL && endq > nextp))
956 endq = q + strlen (q);
964 /* If there is a temporary and a regular watch, send a single
965 notification, for the regular watch. */
966 if (type == 'E' && endq - q == 4 && strncmp ("edit", q, 4) == 0)
971 && endq - q == 6 && strncmp ("unedit", q, 6) == 0)
976 && endq - q == 6 && strncmp ("commit", q, 6) == 0)
981 && endq - q == 5 && strncmp ("tedit", q, 5) == 0)
984 notif = "temporary edit";
987 && endq - q == 7 && strncmp ("tunedit", q, 7) == 0)
990 notif = "temporary unedit";
993 && endq - q == 7 && strncmp ("tcommit", q, 7) == 0)
996 notif = "temporary commit";
1005 struct notify_proc_args args;
1006 size_t len = endp - p;
1010 size_t line_len = 0;
1012 args.notifyee = NULL;
1013 usersname = Xasprintf ("%s/%s/%s", current_parsed_root->directory,
1014 CVSROOTADM, CVSROOTADM_USERS);
1015 fp = CVS_FOPEN (usersname, "r");
1016 if (fp == NULL && !existence_error (errno))
1017 error (0, errno, "cannot read %s", usersname);
1020 while (getline (&line, &line_len, fp) >= 0)
1022 if (strncmp (line, p, len) == 0
1023 && line[len] == ':')
1026 args.notifyee = xstrdup (line + len + 1);
1028 /* There may or may not be more
1029 colon-separated fields added to this in the
1030 future; in any case, we ignore them right
1031 now, and if there are none we make sure to
1032 chop off the final newline, if any. */
1033 cp = strpbrk (args.notifyee, ":\n");
1041 error (0, errno, "cannot read %s", usersname);
1042 if (fclose (fp) < 0)
1043 error (0, errno, "cannot close %s", usersname);
1049 if (args.notifyee == NULL)
1052 tmp = xmalloc (endp - p + 1);
1053 strncpy (tmp, p, endp - p);
1054 tmp[endp - p] = '\0';
1055 args.notifyee = tmp;
1060 args.file = filename;
1062 (void) Parse_Info (CVSROOTADM_NOTIFY, repository, notify_proc,
1064 /* It's okay to cast out the const for the free() below since we
1065 * just allocated this a few lines above. The const was for
1068 free ((char *)args.notifyee);
1073 if (watchers != NULL)
1079 if (*watches == 'E')
1084 if (*watches == 'U')
1086 args.add_tunedit = 1;
1089 if (*watches == 'C')
1091 args.add_tcommit = 1;
1093 watch_modify_watchers (filename, &args);
1097 args.remove_temp = 1;
1098 watch_modify_watchers (filename, &args);
1105 #ifdef CLIENT_SUPPORT
1106 /* Check and send notifications. This is only for the client. */
1108 notify_check (const char *repository, const char *update_dir)
1112 size_t line_len = 0;
1114 if (!server_started)
1115 /* We are in the midst of a command which is not to talk to
1116 the server (e.g. the first phase of a cvs edit). Just chill
1117 out, we'll catch the notifications on the flip side. */
1120 /* We send notifications even if noexec. I'm not sure which behavior
1121 is most sensible. */
1123 fp = CVS_FOPEN (CVSADM_NOTIFY, "r");
1126 if (!existence_error (errno))
1127 error (0, errno, "cannot open %s", CVSADM_NOTIFY);
1130 while (getline (&line, &line_len, fp) > 0)
1137 notif_type = line[0];
1138 if (notif_type == '\0')
1140 filename = line + 1;
1141 cp = strchr (filename, '\t');
1147 client_notify (repository, update_dir, filename, notif_type, val);
1152 error (0, errno, "cannot read %s", CVSADM_NOTIFY);
1153 if (fclose (fp) < 0)
1154 error (0, errno, "cannot close %s", CVSADM_NOTIFY);
1156 /* Leave the CVSADM_NOTIFY file there, until the server tells us it
1157 has dealt with it. */
1159 #endif /* CLIENT_SUPPORT */
1162 static const char *const editors_usage[] =
1164 "Usage: %s %s [-lR] [files...]\n",
1165 "\t-l\tProcess this directory only (not recursive).\n",
1166 "\t-R\tProcess directories recursively.\n",
1167 "(Specify the --help global option for a list of other help options)\n",
1174 editors_fileproc (void *callerdat, struct file_info *finfo)
1176 return find_editors_and_output (finfo);
1182 editors (int argc, char **argv)
1188 usage (editors_usage);
1191 while ((c = getopt (argc, argv, "+lR")) != -1)
1203 usage (editors_usage);
1210 #ifdef CLIENT_SUPPORT
1211 if (current_parsed_root->isremote)
1219 send_files (argc, argv, local, 0, SEND_NO_CONTENTS);
1220 send_file_names (argc, argv, SEND_EXPAND_WILD);
1221 send_to_server ("editors\012", 0);
1222 return get_responses_and_close ();
1224 #endif /* CLIENT_SUPPORT */
1226 return start_recursion (editors_fileproc, NULL, NULL, NULL, NULL,
1227 argc, argv, local, W_LOCAL, 0, CVS_LOCK_READ, NULL,