3 * Written by Jan Roeloffzen
5 * - Image directory caching
19 #include "log2lines.h"
21 static char *cache_name
;
22 static char *tmp_name
;
25 unpack_iso(char *dir
, char *iso
)
29 char iso_tmp
[PATH_MAX
];
34 if ((fiso
= fopen(iso
, "a")) == NULL
)
36 l2l_dbg(1, "Open of %s failed (locked for writing?), trying to copy first\n", iso
);
39 if (copy_file(iso
, iso_tmp
))
46 sprintf(Line
, UNZIP_FMT
, opt_7z
, iso_tmp
, dir
);
49 l2l_dbg(0, "\nCannot unpack %s (check 7z path!)\n", iso_tmp
);
50 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
55 l2l_dbg(2, "\nUnpacking reactos.cab in %s\n", dir
);
56 sprintf(Line
, UNZIP_FMT_CAB
, opt_7z
, dir
, dir
);
59 l2l_dbg(0, "\nCannot unpack reactos.cab in %s\n", dir
);
60 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
72 if (strcmp(basename(path
),DEF_OPT_DIR
) == 0)
78 check_directory(int force
)
81 char freeldr_path
[PATH_MAX
];
82 char iso_path
[PATH_MAX
];
83 char compressed_7z_path
[PATH_MAX
];
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)
91 l2l_dbg(1, "Checking 7z image: %s\n", opt_dir
);
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
)))
98 strcpy(compressed_7z_path
, "."); // default to current dir
100 sprintf(Line
, UNZIP_FMT_7Z
, opt_7z
, opt_dir
, compressed_7z_path
);
102 /* This of course only works if the .7z and .iso basenames are identical
103 * which is normally true for ReactOS trunk builds:
105 strcpy(check_iso
, ".iso");
106 if (!file_exists(opt_dir
) || force
)
108 l2l_dbg(1, "Decompressing 7z image: %s\n", opt_dir
);
109 if (system(Line
) < 0)
111 l2l_dbg(0, "\nCannot decompress to iso image %s\n", opt_dir
);
112 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
117 l2l_dbg(2, "%s already decompressed\n", opt_dir
);
120 if (check_iso
&& PATHCMP(check_iso
, ".iso") == 0)
122 l2l_dbg(1, "Checking ISO image: %s\n", opt_dir
);
123 if (file_exists(opt_dir
))
125 l2l_dbg(2, "ISO image exists: %s\n", opt_dir
);
126 strcpy(iso_path
, opt_dir
);
128 sprintf(freeldr_path
, "%s" PATH_STR
"freeldr.ini", opt_dir
);
129 if (!file_exists(freeldr_path
) || force
)
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");
136 l2l_dbg(2, "%s already unpacked in: %s\n", iso_path
, opt_dir
);
140 l2l_dbg(0, "ISO image not found: %s\n", opt_dir
);
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
);
150 strcat(cache_name
, PATH_STR CACHEFILE
);
151 strcpy(tmp_name
, cache_name
);
152 strcat(tmp_name
, "~");
164 Line
= malloc(LINESIZE
+ 1);
167 l2l_dbg(1, "Alloc Line failed\n");
170 Line
[LINESIZE
] = '\0';
172 fr
= fopen(cache_name
, "r");
175 l2l_dbg(1, "Open %s failed\n", cache_name
);
179 cache
.phead
= cache
.ptail
= NULL
;
181 while (fgets(Line
, LINESIZE
, fr
) != NULL
)
183 pentry
= cache_entry_create(Line
);
186 l2l_dbg(2, "** Create entry failed of: %s\n", Line
);
189 entry_insert(&cache
, pentry
);
198 create_cache(int force
, int skipImageBase
)
201 char *Line
= NULL
, *Fname
= NULL
;
205 if ((fw
= fopen(tmp_name
, "w")) == NULL
)
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
);
213 l2l_dbg(3, "%s is writable\n", tmp_name
);
220 l2l_dbg(3, "Removing %s ...\n", cache_name
);
225 if (file_exists(cache_name
))
227 l2l_dbg(3, "Cache %s already exists\n", cache_name
);
232 Line
= malloc(LINESIZE
+ 1);
235 Line
[LINESIZE
] = '\0';
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)
243 l2l_dbg(0, "Cannot list directory %s\n", opt_dir
);
244 l2l_dbg(1, "Failed to execute: '%s'\n", Line
);
249 l2l_dbg(0, "Creating cache ...");
251 if ((fr
= fopen(tmp_name
, "r")) != NULL
)
253 if ((fw
= fopen(cache_name
, "w")) != NULL
)
255 while (fgets(Line
, LINESIZE
, fr
) != NULL
)
261 Fname
= Line
+ len
- 1;
265 while (Fname
> Line
&& *Fname
!= PATH_CHAR
)
267 if (*Fname
== PATH_CHAR
)
269 if (*Fname
&& !skipImageBase
)
271 if ((err
= get_ImageBase(Line
, &ImageBase
)) == 0)
272 fprintf(fw
, "%s|%s|%0x\n", Fname
, Line
, (unsigned int)ImageBase
);
274 l2l_dbg(3, "%s|%s|%0x, ERR=%d\n", Fname
, Line
, (unsigned int)ImageBase
, err
);
279 l2l_dbg(0, "... done\n");