ReactOS 0.4.16-dev-199-g898cc56
cache.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "version.h"
#include "compat.h"
#include "options.h"
#include "help.h"
#include "image.h"
#include "log2lines.h"
Include dependency graph for cache.c:

Go to the source code of this file.

Functions

static int unpack_iso (char *dir, char *iso)
 
int cleanable (char *path)
 
int check_directory (int force)
 
int read_cache (void)
 
int create_cache (int force, int skipImageBase)
 

Variables

static char CacheName [PATH_MAX]
 
static charcache_name = CacheName
 
static char TmpName [PATH_MAX]
 
static chartmp_name = TmpName
 

Function Documentation

◆ check_directory()

int check_directory ( int  force)

Definition at line 80 of file cache.c.

81{
82 char Line[LINESIZE];
83 char freeldr_path[PATH_MAX];
84 char iso_path[PATH_MAX];
85 char compressed_7z_path[PATH_MAX];
86 char *check_iso;
87 char *check_dir;
88
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)
92 {
93 l2l_dbg(1, "Checking 7z image: %s\n", opt_dir);
94
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)))
98 *check_dir = '\0';
99 else
100 strcpy(compressed_7z_path, "."); // default to current dir
101
102 sprintf(Line, UNZIP_FMT_7Z, opt_7z, opt_dir, compressed_7z_path);
103
104 /* This of course only works if the .7z and .iso basenames are identical
105 * which is normally true for ReactOS trunk builds:
106 */
107 strcpy(check_iso, ".iso");
108 if (!file_exists(opt_dir) || force)
109 {
110 l2l_dbg(1, "Decompressing 7z image: %s\n", opt_dir);
111 if (system(Line) < 0)
112 {
113 l2l_dbg(0, "\nCannot decompress to iso image %s\n", opt_dir);
114 l2l_dbg(1, "Failed to execute: '%s'\n", Line);
115 return 2;
116 }
117 }
118 else
119 l2l_dbg(2, "%s already decompressed\n", opt_dir);
120 }
121
122 if (check_iso && PATHCMP(check_iso, ".iso") == 0)
123 {
124 l2l_dbg(1, "Checking ISO image: %s\n", opt_dir);
125 if (file_exists(opt_dir))
126 {
127 l2l_dbg(2, "ISO image exists: %s\n", opt_dir);
128 strcpy(iso_path, opt_dir);
129 *check_iso = '\0';
130 sprintf(freeldr_path, "%s" PATH_STR "freeldr.ini", opt_dir);
131 if (!file_exists(freeldr_path) || force)
132 {
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");
136 }
137 else
138 l2l_dbg(2, "%s already unpacked in: %s\n", iso_path, opt_dir);
139 }
140 else
141 {
142 l2l_dbg(0, "ISO image not found: %s\n", opt_dir);
143 return 1;
144 }
145 }
147 if (cleanable(opt_dir))
149 else
152 strcat(tmp_name, "~");
153 return 0;
154}
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
static BOOL file_exists(const WCHAR *file_name)
Definition: axinstall.c:70
#define PATH_MAX
Definition: types.h:280
#define LINESIZE
Definition: chargen.c:18
static int check_dir(DOS_FS *fs, DOS_FILE **root, int dots)
Definition: check.c:826
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define PATH_CHAR
Definition: rmkdir.c:17
int __cdecl system(_In_opt_z_ const char *_Command)
_CRT_RESTORE_GCC_WARNINGS _CRT_DISABLE_GCC_WARNINGS _Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
int cleanable(char *path)
Definition: cache.c:72
static int unpack_iso(char *dir, char *iso)
Definition: cache.c:27
static char * tmp_name
Definition: cache.c:24
static char * cache_name
Definition: cache.c:22
#define PATHCMP
Definition: compat.h:32
#define PATH_STR
Definition: compat.h:31
#define ALT_PATH_STR
Definition: config.h:28
#define UNZIP_FMT_7Z
Definition: config.h:19
#define CACHEFILE
Definition: config.h:10
char opt_dir[PATH_MAX]
Definition: options.c:40
char opt_7z[PATH_MAX]
Definition: options.c:43
#define l2l_dbg(level,...)
Definition: util.h:35
Definition: ncftp.h:79

◆ cleanable()

int cleanable ( char path)

Definition at line 72 of file cache.c.

73{
75 return 1;
76 return 0;
77}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
static void basename(LPCWSTR path, LPWSTR name)
Definition: profile.c:38
#define DEF_OPT_DIR
Definition: config.h:8

Referenced by check_directory().

◆ create_cache()

int create_cache ( int  force,
int  skipImageBase 
)

Definition at line 190 of file cache.c.

191{
192 FILE *fr, *fw;
193 char Line[LINESIZE + 1], *Fname = NULL;
194 int len, err;
195 size_t ImageBase;
196
197 if ((fw = fopen(tmp_name, "w")) == NULL)
198 {
199 l2l_dbg(1, "Apparently %s is not writable (mounted ISO?), using current dir\n", tmp_name);
202 }
203 else
204 {
205 l2l_dbg(3, "%s is writable\n", tmp_name);
206 fclose(fw);
208 }
209
210 if (force)
211 {
212 l2l_dbg(3, "Removing %s ...\n", cache_name);
214 }
215 else
216 {
218 {
219 l2l_dbg(3, "Cache %s already exists\n", cache_name);
220 return 0;
221 }
222 }
223
224 Line[LINESIZE] = '\0';
225
227 l2l_dbg(0, "Scanning %s ...\n", opt_dir);
229 l2l_dbg(1, "Executing: %s\n", Line);
230 if (system(Line) != 0)
231 {
232 l2l_dbg(0, "Cannot list directory %s\n", opt_dir);
233 l2l_dbg(1, "Failed to execute: '%s'\n", Line);
235 return 2;
236 }
237 l2l_dbg(0, "Creating cache ...");
238
239 if ((fr = fopen(tmp_name, "r")) != NULL)
240 {
241 if ((fw = fopen(cache_name, "w")) != NULL)
242 {
243 while (fgets(Line, LINESIZE, fr) != NULL)
244 {
245 len = strlen(Line);
246 if (!len)
247 continue;
248
249 Fname = Line + len - 1;
250 if (*Fname == '\n')
251 *Fname = '\0';
252
253 while (Fname > Line && *Fname != PATH_CHAR)
254 Fname--;
255 if (*Fname == PATH_CHAR)
256 Fname++;
257 if (*Fname && !skipImageBase)
258 {
259 if ((err = get_ImageBase(Line, &ImageBase)) == 0)
260 fprintf(fw, "%s|%s|%0x\n", Fname, Line, (unsigned int)ImageBase);
261 else
262 l2l_dbg(3, "%s|%s|%0x, ERR=%d\n", Fname, Line, (unsigned int)ImageBase, err);
263 }
264 }
265 fclose(fw);
266 }
267 l2l_dbg(0, "... done\n");
268 fclose(fr);
269 }
271 return 0;
272}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define NULL
Definition: types.h:112
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
int remove
Definition: msacm.c:1366
#define err(...)
#define DIR_FMT
Definition: compat.h:34
int get_ImageBase(char *fname, size_t *ImageBase)
Definition: image.c:102
#define snprintf
Definition: wintirpc.h:48

◆ read_cache()

int read_cache ( void  )

Definition at line 157 of file cache.c.

158{
159 FILE *fr;
160 LIST_MEMBER *pentry;
161 char Line[LINESIZE + 1];
162 int result = 0;
163
164 Line[LINESIZE] = '\0';
165
166 fr = fopen(cache_name, "r");
167 if (!fr)
168 {
169 l2l_dbg(1, "Open %s failed\n", cache_name);
170 return 2;
171 }
172 cache.phead = cache.ptail = NULL;
173
174 while (fgets(Line, LINESIZE, fr) != NULL)
175 {
176 pentry = cache_entry_create(Line);
177 if (!pentry)
178 {
179 l2l_dbg(2, "** Create entry failed of: %s\n", Line);
180 }
181 else
182 entry_insert(&cache, pentry);
183 }
184
185 fclose(fr);
186 return result;
187}
GLuint64EXT * result
Definition: glext.h:11304
PLIST_MEMBER entry_insert(PLIST list, PLIST_MEMBER pentry)
Definition: list.c:58
PLIST_MEMBER cache_entry_create(char *Line)
Definition: list.c:119
Definition: cache.c:49

Referenced by main().

◆ unpack_iso()

static int unpack_iso ( char dir,
char iso 
)
static

Definition at line 27 of file cache.c.

28{
29 char Line[LINESIZE];
30 int res = 0;
31 char iso_tmp[PATH_MAX];
32 int iso_copied = 0;
33 FILE *fiso;
34
35 strcpy(iso_tmp, iso);
36 if ((fiso = fopen(iso, "a")) == NULL)
37 {
38 l2l_dbg(1, "Open of %s failed (locked for writing?), trying to copy first\n", iso);
39
40 strcat(iso_tmp, "~");
41 if (copy_file(iso, iso_tmp))
42 return 3;
43 iso_copied = 1;
44 }
45 else
46 fclose(fiso);
47
48 sprintf(Line, UNZIP_FMT, opt_7z, iso_tmp, dir);
49 if (system(Line) < 0)
50 {
51 l2l_dbg(0, "\nCannot unpack %s (check 7z path!)\n", iso_tmp);
52 l2l_dbg(1, "Failed to execute: '%s'\n", Line);
53 res = 1;
54 }
55 else
56 {
57 l2l_dbg(2, "\nUnpacking reactos.cab in %s\n", dir);
59 if (system(Line) < 0)
60 {
61 l2l_dbg(0, "\nCannot unpack reactos.cab in %s\n", dir);
62 l2l_dbg(1, "Failed to execute: '%s'\n", Line);
63 res = 2;
64 }
65 }
66 if (iso_copied)
67 remove(iso_tmp);
68 return res;
69}
unsigned int dir
Definition: maze.c:112
static void copy_file(LPCWSTR source, LPCWSTR destination)
Definition: extrac32.c:105
GLuint res
Definition: glext.h:9613
#define UNZIP_FMT_CAB
Definition: config.h:21
#define UNZIP_FMT
Definition: config.h:20

Referenced by check_directory().

Variable Documentation

◆ cache_name

char* cache_name = CacheName
static

Definition at line 22 of file cache.c.

Referenced by check_directory(), create_cache(), and read_cache().

◆ CacheName

char CacheName[PATH_MAX]
static

Definition at line 21 of file cache.c.

◆ tmp_name

◆ TmpName

char TmpName[PATH_MAX]
static

Definition at line 23 of file cache.c.

Referenced by CCFDATAStorage::Create().