PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/build/tools/atree/files.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 482 lines | 421 code | 51 blank | 10 comment | 120 complexity | 0327340c9c5576cc271f6773cd93e28e MD5 | raw file
  1. #include "files.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <dirent.h>
  9. #include <fnmatch.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. static bool
  13. is_comment_line(const char* p)
  14. {
  15. while (*p && isspace(*p)) {
  16. p++;
  17. }
  18. return *p == '#';
  19. }
  20. static string
  21. path_append(const string& base, const string& leaf)
  22. {
  23. string full = base;
  24. if (base.length() > 0 && leaf.length() > 0) {
  25. full += '/';
  26. }
  27. full += leaf;
  28. return full;
  29. }
  30. static bool
  31. is_whitespace_line(const char* p)
  32. {
  33. while (*p) {
  34. if (!isspace(*p)) {
  35. return false;
  36. }
  37. p++;
  38. }
  39. return true;
  40. }
  41. static bool
  42. is_exclude_line(const char* p) {
  43. while (*p) {
  44. if (*p == '-') {
  45. return true;
  46. }
  47. else if (isspace(*p)) {
  48. p++;
  49. }
  50. else {
  51. return false;
  52. }
  53. }
  54. return false;
  55. }
  56. void
  57. split_line(const char* p, vector<string>* out)
  58. {
  59. const char* q = p;
  60. enum { WHITE, TEXT, IN_QUOTE } state = WHITE;
  61. while (*p) {
  62. if (*p == '#') {
  63. break;
  64. }
  65. switch (state)
  66. {
  67. case WHITE:
  68. if (!isspace(*p)) {
  69. q = p;
  70. state = (*p == '"') ? IN_QUOTE : TEXT;
  71. }
  72. break;
  73. case IN_QUOTE:
  74. if (*p == '"') {
  75. state = TEXT;
  76. break;
  77. }
  78. // otherwise fall-through to TEXT case
  79. case TEXT:
  80. if (state != IN_QUOTE && isspace(*p)) {
  81. if (q != p) {
  82. const char* start = q;
  83. size_t len = p-q;
  84. if (len > 2 && *start == '"' && start[len - 1] == '"') {
  85. start++;
  86. len -= 2;
  87. }
  88. out->push_back(string(start, len));
  89. }
  90. state = WHITE;
  91. }
  92. break;
  93. }
  94. p++;
  95. }
  96. if (state == TEXT) {
  97. const char* start = q;
  98. size_t len = p-q;
  99. if (len > 2 && *start == '"' && start[len - 1] == '"') {
  100. start++;
  101. len -= 2;
  102. }
  103. out->push_back(string(start, len));
  104. }
  105. }
  106. static void
  107. add_file(vector<FileRecord>* files, const FileOpType fileOp,
  108. const string& listFile, int listLine,
  109. const string& sourceName, const string& outName)
  110. {
  111. FileRecord rec;
  112. rec.listFile = listFile;
  113. rec.listLine = listLine;
  114. rec.fileOp = fileOp;
  115. rec.sourceName = sourceName;
  116. rec.outName = outName;
  117. files->push_back(rec);
  118. }
  119. static string
  120. replace_variables(const string& input,
  121. const map<string, string>& variables,
  122. bool* error) {
  123. if (variables.empty()) {
  124. return input;
  125. }
  126. // Abort if the variable prefix is not found
  127. if (input.find("${") == string::npos) {
  128. return input;
  129. }
  130. string result = input;
  131. // Note: rather than be fancy to detect recursive replacements,
  132. // we simply iterate till a given threshold is met.
  133. int retries = 1000;
  134. bool did_replace;
  135. do {
  136. did_replace = false;
  137. for (map<string, string>::const_iterator it = variables.begin();
  138. it != variables.end(); ++it) {
  139. string::size_type pos = 0;
  140. while((pos = result.find(it->first, pos)) != string::npos) {
  141. result = result.replace(pos, it->first.length(), it->second);
  142. pos += it->second.length();
  143. did_replace = true;
  144. }
  145. }
  146. if (did_replace && --retries == 0) {
  147. *error = true;
  148. fprintf(stderr, "Recursive replacement detected during variables "
  149. "substitution. Full list of variables is: ");
  150. for (map<string, string>::const_iterator it = variables.begin();
  151. it != variables.end(); ++it) {
  152. fprintf(stderr, " %s=%s\n",
  153. it->first.c_str(), it->second.c_str());
  154. }
  155. return result;
  156. }
  157. } while (did_replace);
  158. return result;
  159. }
  160. int
  161. read_list_file(const string& filename,
  162. const map<string, string>& variables,
  163. vector<FileRecord>* files,
  164. vector<string>* excludes)
  165. {
  166. int err = 0;
  167. FILE* f = NULL;
  168. long size;
  169. char* buf = NULL;
  170. char *p, *q;
  171. int i, lineCount;
  172. f = fopen(filename.c_str(), "r");
  173. if (f == NULL) {
  174. fprintf(stderr, "Could not open list file (%s): %s\n",
  175. filename.c_str(), strerror(errno));
  176. err = errno;
  177. goto cleanup;
  178. }
  179. err = fseek(f, 0, SEEK_END);
  180. if (err != 0) {
  181. fprintf(stderr, "Could not seek to the end of file %s. (%s)\n",
  182. filename.c_str(), strerror(errno));
  183. err = errno;
  184. goto cleanup;
  185. }
  186. size = ftell(f);
  187. err = fseek(f, 0, SEEK_SET);
  188. if (err != 0) {
  189. fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n",
  190. filename.c_str(), strerror(errno));
  191. err = errno;
  192. goto cleanup;
  193. }
  194. buf = (char*)malloc(size+1);
  195. if (buf == NULL) {
  196. // (potentially large)
  197. fprintf(stderr, "out of memory (%ld)\n", size);
  198. err = ENOMEM;
  199. goto cleanup;
  200. }
  201. if (1 != fread(buf, size, 1, f)) {
  202. fprintf(stderr, "error reading file %s. (%s)\n",
  203. filename.c_str(), strerror(errno));
  204. err = errno;
  205. goto cleanup;
  206. }
  207. // split on lines
  208. p = buf;
  209. q = buf+size;
  210. lineCount = 0;
  211. while (p<q) {
  212. if (*p == '\r' || *p == '\n') {
  213. *p = '\0';
  214. lineCount++;
  215. }
  216. p++;
  217. }
  218. // read lines
  219. p = buf;
  220. for (i=0; i<lineCount; i++) {
  221. int len = strlen(p);
  222. q = p + len + 1;
  223. if (is_whitespace_line(p) || is_comment_line(p)) {
  224. ;
  225. }
  226. else if (is_exclude_line(p)) {
  227. while (*p != '-') p++;
  228. p++;
  229. excludes->push_back(string(p));
  230. }
  231. else {
  232. vector<string> words;
  233. split_line(p, &words);
  234. #if 0
  235. printf("[ ");
  236. for (size_t k=0; k<words.size(); k++) {
  237. printf("'%s' ", words[k].c_str());
  238. }
  239. printf("]\n");
  240. #endif
  241. FileOpType op = FILE_OP_COPY;
  242. string paths[2];
  243. int pcount = 0;
  244. string errstr;
  245. for (vector<string>::iterator it = words.begin(); it != words.end(); ++it) {
  246. const string& word = *it;
  247. if (word == "rm") {
  248. if (op != FILE_OP_COPY) {
  249. errstr = "Error: you can only specifiy 'rm' or 'strip' once per line.";
  250. break;
  251. }
  252. op = FILE_OP_REMOVE;
  253. } else if (word == "strip") {
  254. if (op != FILE_OP_COPY) {
  255. errstr = "Error: you can only specifiy 'rm' or 'strip' once per line.";
  256. break;
  257. }
  258. op = FILE_OP_STRIP;
  259. } else if (pcount < 2) {
  260. bool error = false;
  261. paths[pcount++] = replace_variables(word, variables, &error);
  262. if (error) {
  263. err = 1;
  264. goto cleanup;
  265. }
  266. } else {
  267. errstr = "Error: More than 2 paths per line.";
  268. break;
  269. }
  270. }
  271. if (pcount == 0 && !errstr.empty()) {
  272. errstr = "Error: No path found on line.";
  273. }
  274. if (!errstr.empty()) {
  275. fprintf(stderr, "%s:%d: bad format: %s\n%s\nExpected: [SRC] [rm|strip] DEST\n",
  276. filename.c_str(), i+1, p, errstr.c_str());
  277. err = 1;
  278. } else {
  279. if (pcount == 1) {
  280. // pattern: [rm|strip] DEST
  281. paths[1] = paths[0];
  282. }
  283. add_file(files, op, filename, i+1, paths[0], paths[1]);
  284. }
  285. }
  286. p = q;
  287. }
  288. cleanup:
  289. if (buf != NULL) {
  290. free(buf);
  291. }
  292. if (f != NULL) {
  293. fclose(f);
  294. }
  295. return err;
  296. }
  297. int
  298. locate(FileRecord* rec, const vector<string>& search)
  299. {
  300. if (rec->fileOp == FILE_OP_REMOVE) {
  301. // Don't touch source files when removing a destination.
  302. rec->sourceMod = 0;
  303. rec->sourceSize = 0;
  304. rec->sourceIsDir = false;
  305. return 0;
  306. }
  307. int err;
  308. for (vector<string>::const_iterator it=search.begin();
  309. it!=search.end(); it++) {
  310. string full = path_append(*it, rec->sourceName);
  311. struct stat st;
  312. err = stat(full.c_str(), &st);
  313. if (err == 0) {
  314. rec->sourceBase = *it;
  315. rec->sourcePath = full;
  316. rec->sourceMod = st.st_mtime;
  317. rec->sourceSize = st.st_size;
  318. rec->sourceIsDir = S_ISDIR(st.st_mode);
  319. return 0;
  320. }
  321. }
  322. fprintf(stderr, "%s:%d: couldn't locate source file: %s\n",
  323. rec->listFile.c_str(), rec->listLine, rec->sourceName.c_str());
  324. return 1;
  325. }
  326. void
  327. stat_out(const string& base, FileRecord* rec)
  328. {
  329. rec->outPath = path_append(base, rec->outName);
  330. int err;
  331. struct stat st;
  332. err = stat(rec->outPath.c_str(), &st);
  333. if (err == 0) {
  334. rec->outMod = st.st_mtime;
  335. rec->outSize = st.st_size;
  336. rec->outIsDir = S_ISDIR(st.st_mode);
  337. } else {
  338. rec->outMod = 0;
  339. rec->outSize = 0;
  340. rec->outIsDir = false;
  341. }
  342. }
  343. string
  344. dir_part(const string& filename)
  345. {
  346. int pos = filename.rfind('/');
  347. if (pos <= 0) {
  348. return ".";
  349. }
  350. return filename.substr(0, pos);
  351. }
  352. static void
  353. add_more(const string& entry, bool isDir,
  354. const FileRecord& rec, vector<FileRecord>*more)
  355. {
  356. FileRecord r;
  357. r.listFile = rec.listFile;
  358. r.listLine = rec.listLine;
  359. r.sourceName = path_append(rec.sourceName, entry);
  360. r.sourcePath = path_append(rec.sourceBase, r.sourceName);
  361. struct stat st;
  362. int err = stat(r.sourcePath.c_str(), &st);
  363. if (err == 0) {
  364. r.sourceMod = st.st_mtime;
  365. }
  366. r.sourceIsDir = isDir;
  367. r.outName = path_append(rec.outName, entry);
  368. more->push_back(r);
  369. }
  370. static bool
  371. matches_excludes(const char* file, const vector<string>& excludes)
  372. {
  373. for (vector<string>::const_iterator it=excludes.begin();
  374. it!=excludes.end(); it++) {
  375. if (0 == fnmatch(it->c_str(), file, FNM_PERIOD)) {
  376. return true;
  377. }
  378. }
  379. return false;
  380. }
  381. static int
  382. list_dir(const string& path, const FileRecord& rec,
  383. const vector<string>& excludes,
  384. vector<FileRecord>* more)
  385. {
  386. int err;
  387. string full = path_append(rec.sourceBase, rec.sourceName);
  388. full = path_append(full, path);
  389. DIR *d = opendir(full.c_str());
  390. if (d == NULL) {
  391. return errno;
  392. }
  393. vector<string> dirs;
  394. struct dirent *ent;
  395. while (NULL != (ent = readdir(d))) {
  396. if (0 == strcmp(".", ent->d_name)
  397. || 0 == strcmp("..", ent->d_name)) {
  398. continue;
  399. }
  400. if (matches_excludes(ent->d_name, excludes)) {
  401. continue;
  402. }
  403. string entry = path_append(path, ent->d_name);
  404. #ifdef HAVE_DIRENT_D_TYPE
  405. bool is_directory = (ent->d_type == DT_DIR);
  406. #else
  407. // If dirent.d_type is missing, then use stat instead
  408. struct stat stat_buf;
  409. stat(entry.c_str(), &stat_buf);
  410. bool is_directory = S_ISDIR(stat_buf.st_mode);
  411. #endif
  412. add_more(entry, is_directory, rec, more);
  413. if (is_directory) {
  414. dirs.push_back(entry);
  415. }
  416. }
  417. closedir(d);
  418. for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
  419. list_dir(*it, rec, excludes, more);
  420. }
  421. return 0;
  422. }
  423. int
  424. list_dir(const FileRecord& rec, const vector<string>& excludes,
  425. vector<FileRecord>* files)
  426. {
  427. return list_dir("", rec, excludes, files);
  428. }
  429. FileRecord::FileRecord() {
  430. fileOp = FILE_OP_COPY;
  431. }