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