1 /* cmd-uncacherev.c
2  *
3  ****************************************************************
4  * Copyright (C) 2003 Tom Lord
5  *
6  * See the file "COPYING" for further information about
7  * the copyright and warranty status of this work.
8  */
9 
10 
11 #include "config-options.h"
12 #include "hackerlab/cmd/main.h"
13 #include "hackerlab/fs/file-names.h"
14 #include "hackerlab/fs/cwd.h"
15 #include "tla/libfsutils/tmp-files.h"
16 #include "tla/libfsutils/rmrf.h"
17 #include "tla/libarch/namespace.h"
18 #include "tla/libarch/project-tree.h"
19 #include "tla/libarch/my.h"
20 #include "tla/libarch/archive.h"
21 #include "tla/libarch/pristines.h"
22 #include "tla/libarch/build-revision.h"
23 #include "tla/libarch/cmd-uncacherev.h"
24 
25 
26 
27 static t_uchar * usage = "[options] revision [dir]";
28 static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
29                                    "\n"
30                                    "Copyright 2003 Tom Lord\n"
31                                    "\n"
32                                    "This is free software; see the source for copying conditions.\n"
33                                    "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
34                                    "PARTICULAR PURPOSE.\n"
35                                    "\n"
36                                    "Report bugs to " cfg__tla_bug_mail ".\n"
37                                    "\n"
38                                    cfg__std__release_id_string
39                                    "\n");
40 
41 #define OPTS(OP) \
42   OP (opt_help_msg, "h", "help", 0, \
43       "Display a help message and exit.") \
44   OP (opt_long_help, "H", 0, 0, \
45       "Display a verbose help message and exit.") \
46   OP (opt_version, "V", "version", 0, \
47       "Display a release identifier string\n" \
48       "and exit.") \
49   OP (opt_archive, "A", "archive", 1, \
50       "Override `my-default-archive'")
51 
52 
53 t_uchar arch_cmd_uncacherev_help[] = ("remove a cached full source tree from an archive\n"
54                                       "Remove the cached form of REVISION from its archive.\n"
55                                       "\n"
56                                       "If REVISION is not specified, but the command is run from within\n"
57                                       "a project tree, uncache the latest revision in the default\n"
58                                       "version of that tree.\n"
59                                       "\n"
60                                       "Also see \"tla cacherev -H\".\n");
61 
62 enum options
63 {
64   OPTS (OPT_ENUM)
65 };
66 
67 static struct opt_desc opts[] =
68 {
69   OPTS (OPT_DESC)
70     {-1, 0, 0, 0, 0}
71 };
72 
73 
74 
75 int
arch_cmd_uncacherev(t_uchar * program_name,int argc,char * argv[])76 arch_cmd_uncacherev (t_uchar * program_name, int argc, char * argv[])
77 {
78   int o;
79   struct opt_parsed * option;
80   t_uchar * default_archive = 0;
81 
82   safe_buffer_fd (1, 0, O_WRONLY, 0);
83 
84   option = 0;
85 
86   while (1)
87     {
88       o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_uncacherev_help, opt_help_msg, opt_long_help, opt_version);
89       if (o == opt_none)
90         break;
91       switch (o)
92         {
93         default:
94           safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
95           panic ("internal error parsing arguments");
96 
97         usage_error:
98           opt_usage (2, argv[0], program_name, usage, 1);
99           exit (1);
100 
101           /* bogus_arg: */
102           safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
103           goto usage_error;
104 
105         case opt_archive:
106           {
107             lim_free (0, default_archive);
108             default_archive = str_save (0, option->arg_string);
109             break;
110           }
111         }
112     }
113 
114   if (argc != 2)
115     goto usage_error;
116 
117   default_archive = arch_my_default_archive (default_archive);
118 
119   {
120     t_uchar * revision_spec;
121     t_uchar * archive = 0;
122     t_uchar * revision = 0;
123     struct arch_archive * arch = 0;
124 
125     revision_spec = argv[1];
126 
127     if (!arch_valid_package_name (revision_spec, arch_maybe_archive, arch_req_patch_level, 1))
128       {
129         safe_printfmt (2, "%s: invalid revision spec (%s)\n",
130                        argv[0], revision_spec);
131         exit (2);
132       }
133 
134     archive = arch_parse_package_name (arch_ret_archive, default_archive, revision_spec);
135     revision = arch_parse_package_name (arch_ret_non_archive, 0, revision_spec);
136     arch = arch_archive_connect (archive, 0);
137 
138     {
139       t_uchar * errstr;
140 
141       if (arch_archive_delete_cached (&errstr, arch, revision))
142         {
143           safe_printfmt (2, "%s: unable to delete cached revision of %s/%s (%s)\n",
144                          argv[0], archive, revision, errstr);
145           exit (1);
146         }
147     }
148 
149     arch_archive_close (arch);
150 
151     lim_free (0, archive);
152     lim_free (0, revision);
153   }
154 
155   lim_free (0, default_archive);
156   return 0;
157 }
158 
159 
160 
161 
162 /* tag: Tom Lord Thu May 29 23:16:23 2003 (uncacherev.c)
163  */
164