/reactos/sdk/tools/log2lines/cache.c

https://gitlab.com/dj-tech/reactos · C · 287 lines · 247 code · 29 blank · 11 comment · 52 complexity · 550d0262ec14b60a8d2dec2590faa73a MD5 · raw file

  1. /*
  2. * ReactOS log2lines
  3. * Written by Jan Roeloffzen
  4. *
  5. * - Image directory caching
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "util.h"
  11. #include "version.h"
  12. #include "compat.h"
  13. #include "options.h"
  14. #include "help.h"
  15. #include "image.h"
  16. #include "log2lines.h"
  17. static char *cache_name;
  18. static char *tmp_name;
  19. static int
  20. unpack_iso(char *dir, char *iso)
  21. {
  22. char Line[LINESIZE];
  23. int res = 0;
  24. char iso_tmp[PATH_MAX];
  25. int iso_copied = 0;
  26. FILE *fiso;
  27. strcpy(iso_tmp, iso);
  28. if ((fiso = fopen(iso, "a")) == NULL)
  29. {
  30. l2l_dbg(1, "Open of %s failed (locked for writing?), trying to copy first\n", iso);
  31. strcat(iso_tmp, "~");
  32. if (copy_file(iso, iso_tmp))
  33. return 3;
  34. iso_copied = 1;
  35. }
  36. else
  37. fclose(fiso);
  38. sprintf(Line, UNZIP_FMT, opt_7z, iso_tmp, dir);
  39. if (system(Line) < 0)
  40. {
  41. l2l_dbg(0, "\nCannot unpack %s (check 7z path!)\n", iso_tmp);
  42. l2l_dbg(1, "Failed to execute: '%s'\n", Line);
  43. res = 1;
  44. }
  45. else
  46. {
  47. l2l_dbg(2, "\nUnpacking reactos.cab in %s\n", dir);
  48. sprintf(Line, UNZIP_FMT_CAB, opt_7z, dir, dir);
  49. if (system(Line) < 0)
  50. {
  51. l2l_dbg(0, "\nCannot unpack reactos.cab in %s\n", dir);
  52. l2l_dbg(1, "Failed to execute: '%s'\n", Line);
  53. res = 2;
  54. }
  55. }
  56. if (iso_copied)
  57. remove(iso_tmp);
  58. return res;
  59. }
  60. int
  61. cleanable(char *path)
  62. {
  63. if (strcmp(basename(path),DEF_OPT_DIR) == 0)
  64. return 1;
  65. return 0;
  66. }
  67. int
  68. check_directory(int force)
  69. {
  70. char Line[LINESIZE];
  71. char freeldr_path[PATH_MAX];
  72. char iso_path[PATH_MAX];
  73. char compressed_7z_path[PATH_MAX];
  74. char *check_iso;
  75. char *check_dir;
  76. check_iso = strrchr(opt_dir, '.');
  77. l2l_dbg(1, "Checking directory: %s\n", opt_dir);
  78. if (check_iso && PATHCMP(check_iso, ".7z") == 0)
  79. {
  80. l2l_dbg(1, "Checking 7z image: %s\n", opt_dir);
  81. // First attempt to decompress to an .iso image
  82. strcpy(compressed_7z_path, opt_dir);
  83. if ((check_dir = strrchr(compressed_7z_path, PATH_CHAR)))
  84. *check_dir = '\0';
  85. else
  86. strcpy(compressed_7z_path, "."); // default to current dir
  87. sprintf(Line, UNZIP_FMT_7Z, opt_7z, opt_dir, compressed_7z_path);
  88. /* This of course only works if the .7z and .iso basenames are identical
  89. * which is normally true for ReactOS trunk builds:
  90. */
  91. strcpy(check_iso, ".iso");
  92. if (!file_exists(opt_dir) || force)
  93. {
  94. l2l_dbg(1, "Decompressing 7z image: %s\n", opt_dir);
  95. if (system(Line) < 0)
  96. {
  97. l2l_dbg(0, "\nCannot decompress to iso image %s\n", opt_dir);
  98. l2l_dbg(1, "Failed to execute: '%s'\n", Line);
  99. return 2;
  100. }
  101. }
  102. else
  103. l2l_dbg(2, "%s already decompressed\n", opt_dir);
  104. }
  105. if (check_iso && PATHCMP(check_iso, ".iso") == 0)
  106. {
  107. l2l_dbg(1, "Checking ISO image: %s\n", opt_dir);
  108. if (file_exists(opt_dir))
  109. {
  110. l2l_dbg(2, "ISO image exists: %s\n", opt_dir);
  111. strcpy(iso_path, opt_dir);
  112. *check_iso = '\0';
  113. sprintf(freeldr_path, "%s" PATH_STR "freeldr.ini", opt_dir);
  114. if (!file_exists(freeldr_path) || force)
  115. {
  116. l2l_dbg(0, "Unpacking %s to: %s ...", iso_path, opt_dir);
  117. unpack_iso(opt_dir, iso_path);
  118. l2l_dbg(0, "... done\n");
  119. }
  120. else
  121. l2l_dbg(2, "%s already unpacked in: %s\n", iso_path, opt_dir);
  122. }
  123. else
  124. {
  125. l2l_dbg(0, "ISO image not found: %s\n", opt_dir);
  126. return 1;
  127. }
  128. }
  129. cache_name = malloc(PATH_MAX);
  130. tmp_name = malloc(PATH_MAX);
  131. strcpy(cache_name, opt_dir);
  132. if (cleanable(opt_dir))
  133. strcat(cache_name, ALT_PATH_STR CACHEFILE);
  134. else
  135. strcat(cache_name, PATH_STR CACHEFILE);
  136. strcpy(tmp_name, cache_name);
  137. strcat(tmp_name, "~");
  138. return 0;
  139. }
  140. int
  141. read_cache(void)
  142. {
  143. FILE *fr;
  144. LIST_MEMBER *pentry;
  145. char *Line = NULL;
  146. int result = 0;
  147. Line = malloc(LINESIZE + 1);
  148. if (!Line)
  149. {
  150. l2l_dbg(1, "Alloc Line failed\n");
  151. return 1;
  152. }
  153. Line[LINESIZE] = '\0';
  154. fr = fopen(cache_name, "r");
  155. if (!fr)
  156. {
  157. l2l_dbg(1, "Open %s failed\n", cache_name);
  158. free(Line);
  159. return 2;
  160. }
  161. cache.phead = cache.ptail = NULL;
  162. while (fgets(Line, LINESIZE, fr) != NULL)
  163. {
  164. pentry = cache_entry_create(Line);
  165. if (!pentry)
  166. {
  167. l2l_dbg(2, "** Create entry failed of: %s\n", Line);
  168. }
  169. else
  170. entry_insert(&cache, pentry);
  171. }
  172. fclose(fr);
  173. free(Line);
  174. return result;
  175. }
  176. int
  177. create_cache(int force, int skipImageBase)
  178. {
  179. FILE *fr, *fw;
  180. char *Line = NULL, *Fname = NULL;
  181. int len, err;
  182. size_t ImageBase;
  183. if ((fw = fopen(tmp_name, "w")) == NULL)
  184. {
  185. l2l_dbg(1, "Apparently %s is not writable (mounted ISO?), using current dir\n", tmp_name);
  186. cache_name = basename(cache_name);
  187. tmp_name = basename(tmp_name);
  188. }
  189. else
  190. {
  191. l2l_dbg(3, "%s is writable\n", tmp_name);
  192. fclose(fw);
  193. remove(tmp_name);
  194. }
  195. if (force)
  196. {
  197. l2l_dbg(3, "Removing %s ...\n", cache_name);
  198. remove(cache_name);
  199. }
  200. else
  201. {
  202. if (file_exists(cache_name))
  203. {
  204. l2l_dbg(3, "Cache %s already exists\n", cache_name);
  205. return 0;
  206. }
  207. }
  208. Line = malloc(LINESIZE + 1);
  209. if (!Line)
  210. return 1;
  211. Line[LINESIZE] = '\0';
  212. remove(tmp_name);
  213. l2l_dbg(0, "Scanning %s ...\n", opt_dir);
  214. snprintf(Line, LINESIZE, DIR_FMT, opt_dir, tmp_name);
  215. l2l_dbg(1, "Executing: %s\n", Line);
  216. if (system(Line) != 0)
  217. {
  218. l2l_dbg(0, "Cannot list directory %s\n", opt_dir);
  219. l2l_dbg(1, "Failed to execute: '%s'\n", Line);
  220. remove(tmp_name);
  221. free(Line);
  222. return 2;
  223. }
  224. l2l_dbg(0, "Creating cache ...");
  225. if ((fr = fopen(tmp_name, "r")) != NULL)
  226. {
  227. if ((fw = fopen(cache_name, "w")) != NULL)
  228. {
  229. while (fgets(Line, LINESIZE, fr) != NULL)
  230. {
  231. len = strlen(Line);
  232. if (!len)
  233. continue;
  234. Fname = Line + len - 1;
  235. if (*Fname == '\n')
  236. *Fname = '\0';
  237. while (Fname > Line && *Fname != PATH_CHAR)
  238. Fname--;
  239. if (*Fname == PATH_CHAR)
  240. Fname++;
  241. if (*Fname && !skipImageBase)
  242. {
  243. if ((err = get_ImageBase(Line, &ImageBase)) == 0)
  244. fprintf(fw, "%s|%s|%0x\n", Fname, Line, (unsigned int)ImageBase);
  245. else
  246. l2l_dbg(3, "%s|%s|%0x, ERR=%d\n", Fname, Line, (unsigned int)ImageBase, err);
  247. }
  248. }
  249. fclose(fw);
  250. }
  251. l2l_dbg(0, "... done\n");
  252. fclose(fr);
  253. }
  254. remove(tmp_name);
  255. free(Line);
  256. return 0;
  257. }
  258. /* EOF */