3 * Written by Jan Roeloffzen
5 * - Image directory caching
19 #include "log2lines.h"
21 static char CacheName
[PATH_MAX
];
22 static char *cache_name
= CacheName
;
23 static char TmpName
[PATH_MAX
];
24 static char *tmp_name
= TmpName
;
27 unpack_iso(char *dir
, char *iso
)
31 char iso_tmp
[PATH_MAX
];
36 if ((fiso
= fopen(iso
, "a")) == NULL
)
38 l2l_dbg(1, "Open of %s failed (locked for writing?), trying to copy first\n", iso
);
41 if (copy_file(iso
, iso_tmp
))
48 sprintf(Line
, UNZIP_FMT
, opt_7z
, iso_tmp
, dir
);
51 l2l_dbg(0, "\nCannot unpack %s (check 7z path!)\n", iso_tmp
);
52 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
57 l2l_dbg(2, "\nUnpacking reactos.cab in %s\n", dir
);
58 sprintf(Line
, UNZIP_FMT_CAB
, opt_7z
, dir
, dir
);
61 l2l_dbg(0, "\nCannot unpack reactos.cab in %s\n", dir
);
62 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
74 if (strcmp(basename(path
),DEF_OPT_DIR
) == 0)
80 check_directory(int force
)
83 char freeldr_path
[PATH_MAX
];
84 char iso_path
[PATH_MAX
];
85 char compressed_7z_path
[PATH_MAX
];
89 check_iso
= strrchr(opt_dir
, '.');
90 l2l_dbg(1, "Checking directory: %s\n", opt_dir
);
91 if (check_iso
&& PATHCMP(check_iso
, ".7z") == 0)
93 l2l_dbg(1, "Checking 7z image: %s\n", opt_dir
);
95 // First attempt to decompress to an .iso image
96 strcpy(compressed_7z_path
, opt_dir
);
97 if ((check_dir
= strrchr(compressed_7z_path
, PATH_CHAR
)))
100 strcpy(compressed_7z_path
, "."); // default to current dir
102 sprintf(Line
, UNZIP_FMT_7Z
, opt_7z
, opt_dir
, compressed_7z_path
);
104 /* This of course only works if the .7z and .iso basenames are identical
105 * which is normally true for ReactOS trunk builds:
107 strcpy(check_iso
, ".iso");
108 if (!file_exists(opt_dir
) || force
)
110 l2l_dbg(1, "Decompressing 7z image: %s\n", opt_dir
);
111 if (system(Line
) < 0)
113 l2l_dbg(0, "\nCannot decompress to iso image %s\n", opt_dir
);
114 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
119 l2l_dbg(2, "%s already decompressed\n", opt_dir
);
122 if (check_iso
&& PATHCMP(check_iso
, ".iso") == 0)
124 l2l_dbg(1, "Checking ISO image: %s\n", opt_dir
);
125 if (file_exists(opt_dir
))
127 l2l_dbg(2, "ISO image exists: %s\n", opt_dir
);
128 strcpy(iso_path
, opt_dir
);
130 sprintf(freeldr_path
, "%s" PATH_STR
"freeldr.ini", opt_dir
);
131 if (!file_exists(freeldr_path
) || force
)
133 l2l_dbg(0, "Unpacking %s to: %s ...", iso_path
, opt_dir
);
134 unpack_iso(opt_dir
, iso_path
);
135 l2l_dbg(0, "... done\n");
138 l2l_dbg(2, "%s already unpacked in: %s\n", iso_path
, opt_dir
);
142 l2l_dbg(0, "ISO image not found: %s\n", opt_dir
);
146 strcpy(cache_name
, opt_dir
);
147 if (cleanable(opt_dir
))
148 strcat(cache_name
, ALT_PATH_STR CACHEFILE
);
150 strcat(cache_name
, PATH_STR CACHEFILE
);
151 strcpy(tmp_name
, cache_name
);
152 strcat(tmp_name
, "~");
161 char Line
[LINESIZE
+ 1];
164 Line
[LINESIZE
] = '\0';
166 fr
= fopen(cache_name
, "r");
169 l2l_dbg(1, "Open %s failed\n", cache_name
);
172 cache
.phead
= cache
.ptail
= NULL
;
174 while (fgets(Line
, LINESIZE
, fr
) != NULL
)
176 pentry
= cache_entry_create(Line
);
179 l2l_dbg(2, "** Create entry failed of: %s\n", Line
);
182 entry_insert(&cache
, pentry
);
190 create_cache(int force
, int skipImageBase
)
193 char Line
[LINESIZE
+ 1], *Fname
= NULL
;
197 if ((fw
= fopen(tmp_name
, "w")) == NULL
)
199 l2l_dbg(1, "Apparently %s is not writable (mounted ISO?), using current dir\n", tmp_name
);
200 cache_name
= basename(cache_name
);
201 tmp_name
= basename(tmp_name
);
205 l2l_dbg(3, "%s is writable\n", tmp_name
);
212 l2l_dbg(3, "Removing %s ...\n", cache_name
);
217 if (file_exists(cache_name
))
219 l2l_dbg(3, "Cache %s already exists\n", cache_name
);
224 Line
[LINESIZE
] = '\0';
227 l2l_dbg(0, "Scanning %s ...\n", opt_dir
);
228 snprintf(Line
, LINESIZE
, DIR_FMT
, opt_dir
, tmp_name
);
229 l2l_dbg(1, "Executing: %s\n", Line
);
230 if (system(Line
) != 0)
232 l2l_dbg(0, "Cannot list directory %s\n", opt_dir
);
233 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
237 l2l_dbg(0, "Creating cache ...");
239 if ((fr
= fopen(tmp_name
, "r")) != NULL
)
241 if ((fw
= fopen(cache_name
, "w")) != NULL
)
243 while (fgets(Line
, LINESIZE
, fr
) != NULL
)
249 Fname
= Line
+ len
- 1;
253 while (Fname
> Line
&& *Fname
!= PATH_CHAR
)
255 if (*Fname
== PATH_CHAR
)
257 if (*Fname
&& !skipImageBase
)
259 if ((err
= get_ImageBase(Line
, &ImageBase
)) == 0)
260 fprintf(fw
, "%s|%s|%0x\n", Fname
, Line
, (unsigned int)ImageBase
);
262 l2l_dbg(3, "%s|%s|%0x, ERR=%d\n", Fname
, Line
, (unsigned int)ImageBase
, err
);
267 l2l_dbg(0, "... done\n");