14
is_comment_line(const char* p)
16
while (*p && isspace(*p)) {
23
path_append(const string& base, const string& leaf)
26
if (base.length() > 0 && leaf.length() > 0) {
34
is_whitespace_line(const char* p)
46
is_exclude_line(const char* p) {
51
else if (isspace(*p)) {
62
split_line(const char* p, vector<string>* out)
65
enum { WHITE, TEXT, IN_QUOTE } state = WHITE;
76
state = (*p == '"') ? IN_QUOTE : TEXT;
84
// otherwise fall-through to TEXT case
86
if (state != IN_QUOTE && isspace(*p)) {
88
const char* start = q;
90
if (len > 2 && *start == '"' && start[len - 1] == '"') {
94
out->push_back(string(start, len));
103
const char* start = q;
105
if (len > 2 && *start == '"' && start[len - 1] == '"') {
109
out->push_back(string(start, len));
114
add_file(vector<FileRecord>* files, const FileOpType fileOp,
115
const string& listFile, int listLine,
116
const string& sourceName, const string& outName)
119
rec.listFile = listFile;
120
rec.listLine = listLine;
122
rec.sourceName = sourceName;
123
rec.outName = outName;
124
files->push_back(rec);
128
replace_variables(const string& input,
129
const map<string, string>& variables,
131
if (variables.empty()) {
135
// Abort if the variable prefix is not found
136
if (input.find("${") == string::npos) {
140
string result = input;
142
// Note: rather than be fancy to detect recursive replacements,
143
// we simply iterate till a given threshold is met.
150
for (map<string, string>::const_iterator it = variables.begin();
151
it != variables.end(); ++it) {
152
string::size_type pos = 0;
153
while((pos = result.find(it->first, pos)) != string::npos) {
154
result = result.replace(pos, it->first.length(), it->second);
155
pos += it->second.length();
159
if (did_replace && --retries == 0) {
161
fprintf(stderr, "Recursive replacement detected during variables "
162
"substitution. Full list of variables is: ");
164
for (map<string, string>::const_iterator it = variables.begin();
165
it != variables.end(); ++it) {
166
fprintf(stderr, " %s=%s\n",
167
it->first.c_str(), it->second.c_str());
172
} while (did_replace);
178
read_list_file(const string& filename,
179
const map<string, string>& variables,
180
vector<FileRecord>* files,
181
vector<string>* excludes)
190
f = fopen(filename.c_str(), "r");
192
fprintf(stderr, "Could not open list file (%s): %s\n",
193
filename.c_str(), strerror(errno));
198
err = fseek(f, 0, SEEK_END);
200
fprintf(stderr, "Could not seek to the end of file %s. (%s)\n",
201
filename.c_str(), strerror(errno));
208
err = fseek(f, 0, SEEK_SET);
210
fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n",
211
filename.c_str(), strerror(errno));
216
buf = (char*)malloc(size+1);
218
// (potentially large)
219
fprintf(stderr, "out of memory (%ld)\n", size);
224
if (1 != fread(buf, size, 1, f)) {
225
fprintf(stderr, "error reading file %s. (%s)\n",
226
filename.c_str(), strerror(errno));
236
if (*p == '\r' || *p == '\n') {
245
for (i=0; i<lineCount; i++) {
248
if (is_whitespace_line(p) || is_comment_line(p)) {
251
else if (is_exclude_line(p)) {
252
while (*p != '-') p++;
254
excludes->push_back(string(p));
257
vector<string> words;
259
split_line(p, &words);
263
for (size_t k=0; k<words.size(); k++) {
264
printf("'%s' ", words[k].c_str());
268
FileOpType op = FILE_OP_COPY;
272
for (vector<string>::iterator it = words.begin(); it != words.end(); ++it) {
273
const string& word = *it;
275
if (op != FILE_OP_COPY) {
276
errstr = "Error: you can only specifiy 'rm' or 'strip' once per line.";
280
} else if (word == "strip") {
281
if (op != FILE_OP_COPY) {
282
errstr = "Error: you can only specifiy 'rm' or 'strip' once per line.";
286
} else if (pcount < 2) {
288
paths[pcount++] = replace_variables(word, variables, &error);
294
errstr = "Error: More than 2 paths per line.";
299
if (pcount == 0 && !errstr.empty()) {
300
errstr = "Error: No path found on line.";
303
if (!errstr.empty()) {
304
fprintf(stderr, "%s:%d: bad format: %s\n%s\nExpected: [SRC] [rm|strip] DEST\n",
305
filename.c_str(), i+1, p, errstr.c_str());
309
// pattern: [rm|strip] DEST
313
add_file(files, op, filename, i+1, paths[0], paths[1]);
331
locate(FileRecord* rec, const vector<string>& search)
333
if (rec->fileOp == FILE_OP_REMOVE) {
334
// Don't touch source files when removing a destination.
337
rec->sourceIsDir = false;
343
for (vector<string>::const_iterator it=search.begin();
344
it!=search.end(); it++) {
345
string full = path_append(*it, rec->sourceName);
347
err = stat(full.c_str(), &st);
349
rec->sourceBase = *it;
350
rec->sourcePath = full;
351
rec->sourceMod = st.st_mtime;
352
rec->sourceSize = st.st_size;
353
rec->sourceIsDir = S_ISDIR(st.st_mode);
358
fprintf(stderr, "%s:%d: couldn't locate source file: %s\n",
359
rec->listFile.c_str(), rec->listLine, rec->sourceName.c_str());
364
stat_out(const string& base, FileRecord* rec)
366
rec->outPath = path_append(base, rec->outName);
370
err = stat(rec->outPath.c_str(), &st);
372
rec->outMod = st.st_mtime;
373
rec->outSize = st.st_size;
374
rec->outIsDir = S_ISDIR(st.st_mode);
378
rec->outIsDir = false;
383
dir_part(const string& filename)
385
int pos = filename.rfind('/');
389
return filename.substr(0, pos);
393
add_more(const string& entry, bool isDir,
394
const FileRecord& rec, vector<FileRecord>*more)
397
r.listFile = rec.listFile;
398
r.listLine = rec.listLine;
399
r.sourceName = path_append(rec.sourceName, entry);
400
r.sourcePath = path_append(rec.sourceBase, r.sourceName);
402
int err = stat(r.sourcePath.c_str(), &st);
404
r.sourceMod = st.st_mtime;
406
r.sourceIsDir = isDir;
407
r.outName = path_append(rec.outName, entry);
412
matches_excludes(const char* file, const vector<string>& excludes)
414
for (vector<string>::const_iterator it=excludes.begin();
415
it!=excludes.end(); it++) {
416
if (0 == fnmatch(it->c_str(), file, FNM_PERIOD)) {
424
list_dir(const string& path, const FileRecord& rec,
425
const vector<string>& excludes,
426
vector<FileRecord>* more)
430
string full = path_append(rec.sourceBase, rec.sourceName);
431
full = path_append(full, path);
433
DIR *d = opendir(full.c_str());
441
while (NULL != (ent = readdir(d))) {
442
if (0 == strcmp(".", ent->d_name)
443
|| 0 == strcmp("..", ent->d_name)) {
446
if (matches_excludes(ent->d_name, excludes)) {
449
string entry = path_append(path, ent->d_name);
450
#ifdef HAVE_DIRENT_D_TYPE
451
bool is_directory = (ent->d_type == DT_DIR);
453
// If dirent.d_type is missing, then use stat instead
454
struct stat stat_buf;
455
stat(entry.c_str(), &stat_buf);
456
bool is_directory = S_ISDIR(stat_buf.st_mode);
458
add_more(entry, is_directory, rec, more);
460
dirs.push_back(entry);
465
for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
466
list_dir(*it, rec, excludes, more);
473
list_dir(const FileRecord& rec, const vector<string>& excludes,
474
vector<FileRecord>* files)
476
return list_dir("", rec, excludes, files);
479
FileRecord::FileRecord() {
480
fileOp = FILE_OP_COPY;