ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

cache.c
Go to the documentation of this file.
00001 /*
00002  * ReactOS log2lines
00003  * Written by Jan Roeloffzen
00004  *
00005  * - Image directory caching
00006  */
00007 
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <stdlib.h>
00011 
00012 #include "util.h"
00013 #include "version.h"
00014 #include "compat.h"
00015 #include "options.h"
00016 #include "help.h"
00017 #include "image.h"
00018 
00019 #include "log2lines.h"
00020 
00021 static char *cache_name;
00022 static char *tmp_name;
00023 
00024 static int
00025 unpack_iso(char *dir, char *iso)
00026 {
00027     char Line[LINESIZE];
00028     int res = 0;
00029     char iso_tmp[MAX_PATH];
00030     int iso_copied = 0;
00031     FILE *fiso;
00032 
00033     strcpy(iso_tmp, iso);
00034     if ((fiso = fopen(iso, "a")) == NULL)
00035     {
00036         l2l_dbg(1, "Open of %s failed (locked for writing?), trying to copy first\n", iso);
00037 
00038         strcat(iso_tmp, "~");
00039         if (copy_file(iso, iso_tmp))
00040             return 3;
00041         iso_copied = 1;
00042     }
00043     else
00044         fclose(fiso);
00045 
00046     sprintf(Line, UNZIP_FMT, opt_7z, iso_tmp, dir);
00047     if (system(Line) < 0)
00048     {
00049         l2l_dbg(0, "\nCannot unpack %s (check 7z path!)\n", iso_tmp);
00050         l2l_dbg(1, "Failed to execute: '%s'\n", Line);
00051         res = 1;
00052     }
00053     else
00054     {
00055         l2l_dbg(2, "\nUnpacking reactos.cab in %s\n", dir);
00056         sprintf(Line, UNZIP_FMT_CAB, opt_7z, dir, dir);
00057         if (system(Line) < 0)
00058         {
00059             l2l_dbg(0, "\nCannot unpack reactos.cab in %s\n", dir);
00060             l2l_dbg(1, "Failed to execute: '%s'\n", Line);
00061             res = 2;
00062         }
00063     }
00064     if (iso_copied)
00065         remove(iso_tmp);
00066     return res;
00067 }
00068 
00069 int
00070 cleanable(char *path)
00071 {
00072     if (strcmp(basename(path),DEF_OPT_DIR) == 0)
00073         return 1;
00074     return 0;
00075 }
00076 
00077 int
00078 check_directory(int force)
00079 {
00080     char Line[LINESIZE];
00081     char freeldr_path[MAX_PATH];
00082     char iso_path[MAX_PATH];
00083     char compressed_7z_path[MAX_PATH];
00084     char *check_iso;
00085     char *check_dir;
00086 
00087     check_iso = strrchr(opt_dir, '.');
00088     l2l_dbg(1, "Checking directory: %s\n", opt_dir);
00089     if (check_iso && PATHCMP(check_iso, ".7z") == 0)
00090     {
00091         l2l_dbg(1, "Checking 7z image: %s\n", opt_dir);
00092 
00093         // First attempt to decompress to an .iso image
00094         strcpy(compressed_7z_path, opt_dir);
00095         if ((check_dir = strrchr(compressed_7z_path, PATH_CHAR)))
00096             *check_dir = '\0';
00097         else
00098             strcpy(compressed_7z_path, "."); // default to current dir
00099 
00100         sprintf(Line, UNZIP_FMT_7Z, opt_7z, opt_dir, compressed_7z_path);
00101 
00102         /* This of course only works if the .7z and .iso basenames are identical
00103          * which is normally true for ReactOS trunk builds:
00104          */
00105         strcpy(check_iso, ".iso");
00106         if (!file_exists(opt_dir) || force)
00107         {
00108             l2l_dbg(1, "Decompressing 7z image: %s\n", opt_dir);
00109             if (system(Line) < 0)
00110             {
00111                 l2l_dbg(0, "\nCannot decompress to iso image %s\n", opt_dir);
00112                 l2l_dbg(1, "Failed to execute: '%s'\n", Line);
00113                 return 2;
00114             }
00115         }
00116         else
00117             l2l_dbg(2, "%s already decompressed\n", opt_dir);
00118     }
00119 
00120     if (check_iso && PATHCMP(check_iso, ".iso") == 0)
00121     {
00122         l2l_dbg(1, "Checking ISO image: %s\n", opt_dir);
00123         if (file_exists(opt_dir))
00124         {
00125             l2l_dbg(2, "ISO image exists: %s\n", opt_dir);
00126             strcpy(iso_path, opt_dir);
00127             *check_iso = '\0';
00128             sprintf(freeldr_path, "%s" PATH_STR "freeldr.ini", opt_dir);
00129             if (!file_exists(freeldr_path) || force)
00130             {
00131                 l2l_dbg(0, "Unpacking %s to: %s ...", iso_path, opt_dir);
00132                 unpack_iso(opt_dir, iso_path);
00133                 l2l_dbg(0, "... done\n");
00134             }
00135             else
00136                 l2l_dbg(2, "%s already unpacked in: %s\n", iso_path, opt_dir);
00137         }
00138         else
00139         {
00140             l2l_dbg(0, "ISO image not found: %s\n", opt_dir);
00141             return 1;
00142         }
00143     }
00144     cache_name = malloc(MAX_PATH);
00145     tmp_name = malloc(MAX_PATH);
00146     strcpy(cache_name, opt_dir);
00147     if (cleanable(opt_dir))
00148         strcat(cache_name, ALT_PATH_STR CACHEFILE);
00149     else
00150         strcat(cache_name, PATH_STR CACHEFILE);
00151     strcpy(tmp_name, cache_name);
00152     strcat(tmp_name, "~");
00153     return 0;
00154 }
00155 
00156 int
00157 read_cache(void)
00158 {
00159     FILE *fr;
00160     LIST_MEMBER *pentry;
00161     char *Line = NULL;
00162     int result = 0;
00163 
00164     Line = malloc(LINESIZE + 1);
00165     if (!Line)
00166     {
00167         l2l_dbg(1, "Alloc Line failed\n");
00168         return 1;
00169     }
00170     Line[LINESIZE] = '\0';
00171 
00172     fr = fopen(cache_name, "r");
00173     if (!fr)
00174     {
00175         l2l_dbg(1, "Open %s failed\n", cache_name);
00176         free(Line);
00177         return 2;
00178     }
00179     cache.phead = cache.ptail = NULL;
00180 
00181     while (fgets(Line, LINESIZE, fr) != NULL)
00182     {
00183         pentry = cache_entry_create(Line);
00184         if (!pentry)
00185         {
00186             l2l_dbg(2, "** Create entry failed of: %s\n", Line);
00187         }
00188         else
00189             entry_insert(&cache, pentry);
00190     }
00191 
00192     fclose(fr);
00193     free(Line);
00194     return result;
00195 }
00196 
00197 int
00198 create_cache(int force, int skipImageBase)
00199 {
00200     FILE *fr, *fw;
00201     char *Line = NULL, *Fname = NULL;
00202     int len, err;
00203     size_t ImageBase;
00204 
00205     if ((fw = fopen(tmp_name, "w")) == NULL)
00206     {
00207         l2l_dbg(1, "Apparently %s is not writable (mounted ISO?), using current dir\n", tmp_name);
00208         cache_name = basename(cache_name);
00209         tmp_name = basename(tmp_name);
00210     }
00211     else
00212     {
00213         l2l_dbg(3, "%s is writable\n", tmp_name);
00214         fclose(fw);
00215         remove(tmp_name);
00216     }
00217 
00218     if (force)
00219     {
00220         l2l_dbg(3, "Removing %s ...\n", cache_name);
00221         remove(cache_name);
00222     }
00223     else
00224     {
00225         if (file_exists(cache_name))
00226         {
00227             l2l_dbg(3, "Cache %s already exists\n", cache_name);
00228             return 0;
00229         }
00230     }
00231 
00232     Line = malloc(LINESIZE + 1);
00233     if (!Line)
00234         return 1;
00235     Line[LINESIZE] = '\0';
00236 
00237     remove(tmp_name);
00238     l2l_dbg(0, "Scanning %s ...\n", opt_dir);
00239     snprintf(Line, LINESIZE, DIR_FMT, opt_dir, tmp_name);
00240     l2l_dbg(1, "Executing: %s\n", Line);
00241     if (system(Line) != 0)
00242     {
00243         l2l_dbg(0, "Cannot list directory %s\n", opt_dir);
00244         l2l_dbg(1, "Failed to execute: '%s'\n", Line);
00245         remove(tmp_name);
00246         free(Line);
00247         return 2;
00248     }
00249     l2l_dbg(0, "Creating cache ...");
00250 
00251     if ((fr = fopen(tmp_name, "r")) != NULL)
00252     {
00253         if ((fw = fopen(cache_name, "w")) != NULL)
00254         {
00255             while (fgets(Line, LINESIZE, fr) != NULL)
00256             {
00257                 len = strlen(Line);
00258                 if (!len)
00259                     continue;
00260 
00261                 Fname = Line + len - 1;
00262                 if (*Fname == '\n')
00263                     *Fname = '\0';
00264 
00265                 while (Fname > Line && *Fname != PATH_CHAR)
00266                     Fname--;
00267                 if (*Fname == PATH_CHAR)
00268                     Fname++;
00269                 if (*Fname && !skipImageBase)
00270                 {
00271                     if ((err = get_ImageBase(Line, &ImageBase)) == 0)
00272                         fprintf(fw, "%s|%s|%0x\n", Fname, Line, (unsigned int)ImageBase);
00273                     else
00274                         l2l_dbg(3, "%s|%s|%0x, ERR=%d\n", Fname, Line, (unsigned int)ImageBase, err);
00275                 }
00276             }
00277             fclose(fw);
00278         }
00279         l2l_dbg(0, "... done\n");
00280         fclose(fr);
00281     }
00282     remove(tmp_name);
00283     free(Line);
00284     return 0;
00285 }
00286 
00287 /* EOF */

Generated on Fri May 25 2012 04:15:27 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.