14 vector<string> g_listFiles;
15 vector<string> g_inputBases;
16 map<string, string> g_variables;
19 bool g_useHardLinks = false;
23 "Usage: atree OPTIONS\n"
26 " -f FILELIST Specify one or more files containing the\n"
27 " list of files to copy.\n"
28 " -I INPUTDIR Specify one or more base directories in\n"
29 " which to look for the files\n"
30 " -o OUTPUTDIR Specify the directory to copy all of the\n"
32 " -l Use hard links instead of copying the files.\n"
33 " -m DEPENDENCY Output a make-formatted file containing the list.\n"
34 " of files included. It sets the variable ATREE_FILES.\n"
35 " -v VAR=VAL Replaces ${VAR} by VAL when reading input files.\n"
37 "FILELIST file format:\n"
38 " The FILELIST files contain the list of files that will end up\n"
39 " in the final OUTPUTDIR. Atree will look for files in the INPUTDIR\n"
40 " directories in the order they are specified.\n"
42 " In a FILELIST file, comment lines start with a #. Other lines\n"
43 " are of the format:\n"
49 " DEST should be path relative to the output directory.\n"
50 " If SRC is supplied, the file names can be different.\n"
51 " SRCPATTERN is a pattern for the filenames.\n"
56 fwrite(USAGE, strlen(USAGE), 1, stderr);
61 add_variable(const char* arg) {
63 while (*p && *p != '=') p++;
65 if (*p == 0 || p == arg || p[1] == 0) {
70 var << "${" << string(arg, p-arg) << "}";
71 g_variables[var.str()] = string(p+1);
76 main(int argc, char* const* argv)
81 int opt = getopt(argc, argv, "f:I:o:hlm:v:");
88 g_listFiles.push_back(string(optarg));
91 g_inputBases.push_back(string(optarg));
94 if (g_outputBase.length() != 0) {
95 fprintf(stderr, "%s: -o may only be supplied once -- "
96 "-o %s\n", argv[0], optarg);
99 g_outputBase = optarg;
102 g_useHardLinks = true;
105 if (g_dependency.length() != 0) {
106 fprintf(stderr, "%s: -m may only be supplied once -- "
107 "-m %s\n", argv[0], optarg);
110 g_dependency = optarg;
113 if (!add_variable(optarg)) {
114 fprintf(stderr, "%s Invalid expression in '-v %s': "
115 "expected format is '-v VAR=VALUE'.\n",
126 if (optind != argc) {
127 fprintf(stderr, "%s: invalid argument -- %s\n", argv[0], argv[optind]);
131 if (g_listFiles.size() == 0) {
132 fprintf(stderr, "%s: At least one -f option must be supplied.\n",
137 if (g_inputBases.size() == 0) {
138 fprintf(stderr, "%s: At least one -I option must be supplied.\n",
143 if (g_outputBase.length() == 0) {
144 fprintf(stderr, "%s: -o option must be supplied.\n", argv[0]);
150 for (vector<string>::iterator it=g_listFiles.begin();
151 it!=g_listFiles.end(); it++) {
152 printf("-f \"%s\"\n", it->c_str());
154 for (vector<string>::iterator it=g_inputBases.begin();
155 it!=g_inputBases.end(); it++) {
156 printf("-I \"%s\"\n", it->c_str());
158 printf("-o \"%s\"\n", g_outputBase.c_str());
159 if (g_useHardLinks) {
164 vector<FileRecord> files;
165 vector<FileRecord> more;
166 vector<string> excludes;
167 set<string> directories;
171 for (vector<string>::iterator it=g_listFiles.begin();
172 it!=g_listFiles.end(); it++) {
173 err = read_list_file(*it, g_variables, &files, &excludes);
179 // look for input files
181 for (vector<FileRecord>::iterator it=files.begin();
182 it!=files.end(); it++) {
183 err |= locate(&(*it), g_inputBases);
187 // expand the directories that we should copy into a list of files
188 for (vector<FileRecord>::iterator it=files.begin();
189 it!=files.end(); it++) {
190 if (it->sourceIsDir) {
191 err |= list_dir(*it, excludes, &more);
194 for (vector<FileRecord>::iterator it=more.begin();
195 it!=more.end(); it++) {
196 files.push_back(*it);
199 // get the name and modtime of the output files
200 for (vector<FileRecord>::iterator it=files.begin();
201 it!=files.end(); it++) {
202 stat_out(g_outputBase, &(*it));
209 // gather directories
210 for (vector<FileRecord>::iterator it=files.begin();
211 it!=files.end(); it++) {
212 if (it->sourceIsDir) {
213 directories.insert(it->outPath);
215 string s = dir_part(it->outPath);
217 directories.insert(s);
222 // gather files that should become directores and directories that should
224 for (vector<FileRecord>::iterator it=files.begin();
225 it!=files.end(); it++) {
226 if (it->outMod != 0 && it->sourceIsDir != it->outIsDir) {
227 deleted.insert(it->outPath);
232 for (set<string>::iterator it=deleted.begin();
233 it!=deleted.end(); it++) {
235 printf("deleting %s\n", it->c_str());
237 err = remove_recursively(*it);
244 for (set<string>::iterator it=directories.begin();
245 it!=directories.end(); it++) {
247 printf("mkdir %s\n", it->c_str());
249 err = mkdir_recursively(*it);
255 // copy (or link) files
256 for (vector<FileRecord>::iterator it=files.begin();
257 it!=files.end(); it++) {
258 if (!it->sourceIsDir) {
260 printf("copy %s(%ld) ==> %s(%ld)", it->sourcePath.c_str(),
261 it->sourceMod, it->outPath.c_str(), it->outMod);
265 if (it->outMod < it->sourceMod) {
266 err = copy_file(it->sourcePath, it->outPath);
275 printf(" skipping.\n");
281 // output the dependency file
282 if (g_dependency.length() != 0) {
283 FILE *f = fopen(g_dependency.c_str(), "w");
285 fprintf(f, "ATREE_FILES := $(ATREE_FILES) \\\n");
286 for (vector<FileRecord>::iterator it=files.begin();
287 it!=files.end(); it++) {
288 if (!it->sourceIsDir) {
289 fprintf(f, "%s \\\n", it->sourcePath.c_str());
295 fprintf(stderr, "error opening manifest file for write: %s\n",
296 g_dependency.c_str());