"Fossies" - the Fresh Open Source Software Archive

Member "rpm2html-1.11.2/cleanup.c" (5 Oct 2010, 13432 Bytes) of package /linux/privat/rpm2html-1.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cleanup.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * cleanup.c : Cleanup the RDF and HTML trees to remove files related to
    3  *             older packages.
    4  *
    5  *  The RDF output tree looks like this:
    6  *
    7  *  root ----- distrib1 ---- package1.rdf
    8  *                         - package2.rdf
    9  *             - ...
   10  *           - distrib2 ---- packagen.rdf
   11  *                     - ...
   12  *           - distribm ...
   13  *       - resources --- resource1.rdf
   14  *                     - resource2.rdf
   15  *             - ...
   16  *             - distribs ------ distrib1.rdf
   17  *                             - distrib2.rdf
   18  *                             - distribn.rdf
   19  *
   20  * See Copyright for the status of this software.
   21  *
   22  * $Id: cleanup.c,v 1.10 2010/10/05 14:36:54 hany Exp $
   23  */
   24 
   25 #include <config.h>
   26 #include <sys/types.h>
   27 #include <sys/stat.h>
   28 #ifdef HAVE_FCNTL_H
   29 #include <fcntl.h>
   30 #endif
   31 #include <stdio.h>
   32 #include <stdlib.h>
   33 #include <string.h>
   34 #ifdef HAVE_UNISTD_H
   35 #include <unistd.h>
   36 #endif
   37 #include <dirent.h>
   38 #include <errno.h>
   39 #include <time.h>
   40 
   41 #include <rpm/rpmlib.h>
   42 
   43 #include "rpm2html.h"
   44 #include "rpmdata.h"
   45 #include "html.h"
   46 #include "rdf.h"
   47 #include "rdf_api.h"
   48 #include "language.h"
   49 
   50 #ifndef HAVE_SNPRINTF
   51 #error You really need snprintf ...
   52 #endif
   53 static int nb_removed_files = 0;
   54 
   55 /*
   56  * Cleanup the global varibales of this module
   57  */
   58 void cleanupCleanup(void) {
   59     nb_removed_files = 0;
   60 }
   61 
   62 /*
   63  * Check an RPM RDF file for obsolescence.
   64  */
   65 
   66 static void rpmOneFileCleanup(char *file) {
   67     rdfSchema rdf;
   68     rdfNamespace rpmNs;
   69     rdfNamespace rdfNs;
   70     rdfDescription desc;
   71     char *name;
   72     char *version;
   73     char *release;
   74     char *arch;
   75     char *URL;
   76     int   aboutRPM = 0;
   77     int   isOk = 0;
   78     rpmDataPtr found;
   79 
   80     rdf = rdfRead(file);
   81     if (rdf == NULL) {
   82     unlink(file);
   83     nb_removed_files++;
   84         return;
   85     }
   86 
   87     /*
   88      * Start the analyze, check that's an RDF for RPM packages.
   89      */
   90     rdfNs = rdfGetNamespace(rdf, "http://www.w3.org/TR/WD-rdf-syntax#");
   91     if (rdfNs == NULL) {
   92     if (rpm2htmlVerbose)
   93         printf("%s is not an RDF schema\n", file);
   94     rdfDestroySchema(rdf);
   95     unlink(file);
   96     nb_removed_files++;
   97     return;
   98     }
   99     rpmNs = rdfGetNamespace(rdf, "http://www.rpm.org/");
  100     if (rdfNs == NULL) {
  101     if (rpm2htmlVerbose)
  102         printf("%s is not an RPM specific RDF schema\n", file);
  103     rdfDestroySchema(rdf);
  104     unlink(file);
  105     nb_removed_files++;
  106     return;
  107     }
  108     desc = rdfFirstDescription(rdf);
  109     if (rdfNs == NULL) {
  110     if (rpm2htmlVerbose)
  111         printf("%s RDF schema seems empty\n", file);
  112     rdfDestroySchema(rdf);
  113     unlink(file);
  114     nb_removed_files++;
  115     return;
  116     }
  117 
  118     /*
  119      * We are pretty sure that it will be a valid schema,
  120      * Walk the tree and collect the packages descriptions.
  121      */
  122     if (desc != NULL) {
  123         /*
  124      * Get the resource URL ...
  125      */
  126     URL = rdfGetDescriptionHref(rdf, desc);
  127     if (URL == NULL) {
  128         if (rpm2htmlVerbose)
  129         printf("%s RDF schema invalid : Desc without href\n",
  130                 file);
  131         rdfDestroySchema(rdf);
  132         unlink(file);
  133         nb_removed_files++;
  134         return;
  135     }
  136 
  137     /*
  138      * Now extract all the metadata information from the RDF tree
  139      */
  140     rdfGetValue(desc, "Name", rpmNs, &name, NULL);
  141     if (name == NULL) {
  142         if (rpm2htmlVerbose)
  143         printf("%s RDF schema invalid : no Name\n", file);
  144         rdfDestroySchema(rdf);
  145         unlink(file);
  146         nb_removed_files++;
  147         return;
  148     }
  149     rdfGetValue(desc, "Version", rpmNs, &version, NULL);
  150     if (version == NULL) {
  151         if (rpm2htmlVerbose)
  152         printf("%s RDF schema invalid : no Version\n", file);
  153         rdfDestroySchema(rdf);
  154         unlink(file);
  155         nb_removed_files++;
  156         return;
  157     }
  158     rdfGetValue(desc, "Release", rpmNs, &release, NULL);
  159     if (release == NULL) {
  160         if (rpm2htmlVerbose)
  161         printf("%s RDF schema invalid : no Release\n", file);
  162         rdfDestroySchema(rdf);
  163         unlink(file);
  164         nb_removed_files++;
  165         return;
  166     }
  167     rdfGetValue(desc, "Arch", rpmNs, &arch, NULL);
  168     if (arch == NULL) {
  169         if (rpm2htmlVerbose)
  170         printf("%s RDF schema invalid : no Arch\n", file);
  171         rdfDestroySchema(rdf);
  172         unlink(file);
  173         nb_removed_files++;
  174         return;
  175     }
  176 
  177         /*********************
  178     rdfGetValue(desc, "Os", rpmNs, &os, NULL);
  179     if (os == NULL) {
  180         if (rpm2htmlVerbose)
  181         printf("%s RDF schema invalid : no Os\n", file);
  182         rdfDestroySchema(rdf);
  183         unlink(file);
  184         nb_removed_files++;
  185         return;
  186     }
  187      **********************/
  188     aboutRPM = 1;
  189 
  190         /*
  191      * Now that we have gathered name,version,release,arch and os
  192      * Check that this is in the package list.
  193      */
  194         found = rpmSearchSoftware(name, version, release, arch);
  195     if (found) {
  196         isOk = 1;
  197     }
  198     if (URL != NULL) free(URL);
  199 
  200         /*
  201      * Skip to next Description in the RDf file.
  202      */
  203     desc = rdfNextDescription(desc); 
  204     }
  205 
  206     if ((aboutRPM) && (!isOk)) {
  207     /* if (rpm2htmlVerbose) */
  208         printf("Removing outdated %s RDF description\n", file);
  209         unlink(file);
  210     nb_removed_files++;
  211     }
  212 
  213     /*
  214      * Cleanup.
  215      */
  216     rdfDestroySchema(rdf);
  217 }
  218 
  219 /*
  220  * Scan a distribution directory for RDF files
  221  */
  222 static void rpmOneDirCleanup(char *dir) {
  223     char *filename;
  224     char path[2000];
  225     struct stat buf;
  226     int len;
  227     DIR *d;
  228     struct dirent *file;
  229 
  230     d = opendir(dir);
  231     if (d == NULL) {
  232         fprintf(stderr, "rpmOneDirCleanup: Listing of %s failed: %s\n", dir,
  233             strerror(errno));
  234     return;
  235     } else {
  236         while ((file = readdir(d)) != NULL) {
  237         filename = file->d_name;
  238         len = strlen(filename);
  239 
  240         /*
  241          * Compute the full path
  242          */
  243         snprintf(path, sizeof(path), "%s/%s", dir, filename);
  244 
  245         /*
  246          * Stat() the file to detect directory and symlimks
  247          */
  248         if (lstat(path, &buf) != 0) {
  249             fprintf(stderr, "rpmOneDirCleanup: Couldn't stat(%s)\n", path);
  250         continue;
  251         }
  252 
  253         /*
  254          * Don't follow of analyze symlinks, remove them
  255          */
  256             if (S_ISLNK(buf.st_mode)) {
  257         if (rpm2htmlVerbose)
  258             fprintf(stderr, "Cleaning: removing symlink %s\n", path);
  259         unlink(path);
  260         nb_removed_files++;
  261             continue;
  262         }
  263 
  264         /*
  265          * Check for RDF files by looking at the suffix
  266          */
  267         else if ((len >= 5) && (!strcasecmp(&filename[len - 4], ".rdf"))) {
  268             rpmOneFileCleanup(path);
  269         }
  270 
  271         /*
  272          * Else if this is a directory, recurse !
  273          */
  274         else if (S_ISDIR(buf.st_mode)) {
  275         if (filename[0] != '.') {
  276             rpmOneDirCleanup(path);
  277         }
  278         }
  279     }
  280     }
  281     closedir(d);
  282 }
  283 
  284 /*
  285  * Check a resource RDF file for obsolescence.
  286  */
  287 
  288 static void rpmOneResourceCleanup(char *file) {
  289     rdfSchema rdf;
  290     rdfNamespace rpmNs;
  291     rdfNamespace rdfNs;
  292     rdfDescription desc;
  293     char *name;
  294     char *version;
  295     char *release;
  296     char *arch;
  297     char *URL;
  298     int   aboutRPM = 0;
  299     int   isOk = 0;
  300     rpmDataPtr found;
  301 
  302     rdf = rdfRead(file);
  303     if (rdf == NULL) {
  304     unlink(file);
  305     nb_removed_files++;
  306         return;
  307     }
  308 
  309     /*
  310      * Start the analyze, check that's an RDF for RPM resource.
  311      */
  312     rdfNs = rdfGetNamespace(rdf, "http://www.w3.org/TR/WD-rdf-syntax#");
  313     if (rdfNs == NULL) {
  314     if (rpm2htmlVerbose)
  315         printf("%s is not an RDF schema\n", file);
  316     rdfDestroySchema(rdf);
  317     unlink(file);
  318     nb_removed_files++;
  319     return;
  320     }
  321     rpmNs = rdfGetNamespace(rdf, "http://www.rpm.org/");
  322     if (rdfNs == NULL) {
  323     if (rpm2htmlVerbose)
  324         printf("%s is not an RPM specific RDF schema\n", file);
  325     rdfDestroySchema(rdf);
  326     unlink(file);
  327     nb_removed_files++;
  328     return;
  329     }
  330     desc = rdfFirstDescription(rdf);
  331     if (rdfNs == NULL) {
  332     if (rpm2htmlVerbose)
  333         printf("%s RDF schema seems empty\n", file);
  334     rdfDestroySchema(rdf);
  335     unlink(file);
  336     nb_removed_files++;
  337     return;
  338     }
  339 
  340     /*
  341      * We are pretty sure that it will be a valid schema,
  342      * Walk the tree and collect the packages descriptions.
  343      */
  344     if (desc != NULL) {
  345         /*
  346      * Get the resource URL ...
  347      */
  348     URL = rdfGetDescriptionHref(rdf, desc);
  349     if (URL == NULL) {
  350         if (rpm2htmlVerbose)
  351         printf("%s RDF schema invalid : Desc without href\n",
  352                 file);
  353         rdfDestroySchema(rdf);
  354         unlink(file);
  355         nb_removed_files++;
  356         return;
  357     }
  358 
  359     /*
  360      * Now extract all the metadata information from the RDF tree
  361      */
  362     rdfGetValue(desc, "Name", rpmNs, &name, NULL);
  363     if (name == NULL) {
  364         if (rpm2htmlVerbose)
  365         printf("%s RDF schema invalid : no Name\n", file);
  366         rdfDestroySchema(rdf);
  367         return;
  368     }
  369     rdfGetValue(desc, "Version", rpmNs, &version, NULL);
  370     if (version == NULL) {
  371         if (rpm2htmlVerbose)
  372         printf("%s RDF schema invalid : no Version\n", file);
  373         rdfDestroySchema(rdf);
  374         return;
  375     }
  376     rdfGetValue(desc, "Release", rpmNs, &release, NULL);
  377     if (release == NULL) {
  378         if (rpm2htmlVerbose)
  379         printf("%s RDF schema invalid : no Release\n", file);
  380         rdfDestroySchema(rdf);
  381         return;
  382     }
  383     rdfGetValue(desc, "Arch", rpmNs, &arch, NULL);
  384     if (arch == NULL) {
  385         if (rpm2htmlVerbose)
  386         printf("%s RDF schema invalid : no Arch\n", file);
  387         rdfDestroySchema(rdf);
  388         return;
  389     }
  390 
  391         /***********
  392     rdfGetValue(desc, "Os", rpmNs, &os, NULL);
  393     if (os == NULL) {
  394         if (rpm2htmlVerbose)
  395         printf("%s RDF schema invalid : no Os\n", file);
  396         rdfDestroySchema(rdf);
  397         return;
  398     }
  399      *************/
  400 
  401     aboutRPM = 1;
  402 
  403         /*
  404      * Now that we have gathered name,version,release,arch and os
  405      * Check that this is in the package list.
  406      */
  407         found = rpmSearchSoftware(name, version, release, arch);
  408     if (found) {
  409         isOk = 1;
  410     }
  411 
  412         /*
  413      * Skip to next Description in the RDf file.
  414      */
  415     desc = rdfNextDescription(desc); 
  416     }
  417 
  418     if ((aboutRPM) && (!isOk)) {
  419     /* if (rpm2htmlVerbose) */
  420         printf("Removing outdated %s RDF resource\n", file);
  421         unlink(file);
  422     nb_removed_files++;
  423     }
  424     /*
  425      * Cleanup.
  426      */
  427     rdfDestroySchema(rdf);
  428 }
  429 
  430 /*
  431  * Check the resources directory.
  432  */
  433 
  434 static void rpmResourcesCleanup(char *dir) {
  435     char *filename;
  436     char path[2000];
  437     struct stat buf;
  438     int len;
  439     DIR *d;
  440     struct dirent *file;
  441 
  442     d = opendir(dir);
  443     if (d == NULL) {
  444         fprintf(stderr, "rpmResourcesCleanup: Listing of %s failed: %s\n", dir,
  445             strerror(errno));
  446     return;
  447     } else {
  448         while ((file = readdir(d)) != NULL) {
  449         filename = file->d_name;
  450         len = strlen(filename);
  451 
  452         /*
  453          * Compute the full path
  454          */
  455         snprintf(path, sizeof(path), "%s/%s", dir, filename);
  456 
  457         /*
  458          * Stat() the file to detect directory and symlimks
  459          */
  460         if (lstat(path, &buf) != 0) {
  461             fprintf(stderr, "Couldn't stat(%s)\n", path);
  462         unlink(path);
  463         nb_removed_files++;
  464         continue;
  465         }
  466 
  467         /*
  468          * Don't follow of analyze symlinks, remove them
  469          */
  470             if (S_ISLNK(buf.st_mode)) {
  471         if (rpm2htmlVerbose)
  472             fprintf(stderr, "Cleaning: removing symlink %s\n", path);
  473         unlink(path);
  474         nb_removed_files++;
  475             continue;
  476         }
  477 
  478         /*
  479          * Check for RDF files by looking at the suffix
  480          */
  481         else if ((len >= 5) && (!strcasecmp(&filename[len - 4], ".rdf"))) {
  482             rpmOneResourceCleanup(path);
  483         }
  484 
  485         /*
  486          * Else if this is a directory, recurse !
  487          */
  488         else if (S_ISDIR(buf.st_mode)) {
  489         if (filename[0] != '.') {
  490             if (!strcmp(filename, "distribs")) {
  491                 /* TOTO : explore the distrib too ! */
  492             } else {
  493             if (rpm2htmlVerbose)
  494     fprintf(stderr, "Cleaning: directory resources/%s should not exist\n",
  495                     filename);
  496             }
  497         }
  498         }
  499     }
  500     }
  501     closedir(d);
  502 }
  503 
  504 /*
  505  * Cleanup the whole RDF repository
  506  */
  507 
  508 void rpmDirCleanupAll(void) {
  509     char *filename;
  510     char path[2000];
  511     struct stat buf;
  512     int len;
  513     DIR *d;
  514     struct dirent *file;
  515     int start_time;
  516     int end_time;
  517 
  518     if (!rpm2html_dump_rdf) return;
  519     if (!rpm2html_rdf_dir) return;
  520 
  521     nb_removed_files = 0;
  522     start_time = time(NULL);
  523 
  524     d = opendir(rpm2html_rdf_dir);
  525     if (d == NULL) {
  526         fprintf(stderr, "Listing of %s failed: %s\n", rpm2html_rdf_dir,
  527             strerror(errno));
  528     return;
  529     } else {
  530         while ((file = readdir(d)) != NULL) {
  531         filename = file->d_name;
  532         len = strlen(filename);
  533 
  534         /*
  535          * Compute the full path
  536          */
  537         snprintf(path, sizeof(path), "%s/%s", rpm2html_rdf_dir, filename);
  538 
  539         /*
  540          * Stat() the file to detect directory and symlimks
  541          */
  542         if (lstat(path, &buf) != 0) {
  543             fprintf(stderr, "rpmDirCleanupAll: Couldn't stat(%s)\n", path);
  544         continue;
  545         }
  546 
  547         /*
  548          * Don't follow of analyze symlinks,
  549          */
  550             if (S_ISLNK(buf.st_mode)) {
  551         if (rpm2htmlVerbose)
  552             fprintf(stderr, "Cleaning: removing symlink %s\n", path);
  553         unlink(path);
  554         nb_removed_files++;
  555             continue;
  556         }
  557 
  558         /*
  559          * We shouldn't get any non-rdf file here
  560          */
  561         else if ((len >= 5) && (!strcasecmp(&filename[len - 4], ".rdf"))) {
  562             rpmOneFileCleanup(filename);
  563         }
  564 
  565         /*
  566          * Else if this is a directory, handle it
  567          */
  568         else if (S_ISDIR(buf.st_mode)) {
  569         if (!strcmp(filename, "resources")) {
  570             rpmResourcesCleanup(path);
  571         } else if (filename[0] != '.') {
  572             rpmOneDirCleanup(path);
  573         }
  574         }
  575         
  576         /*
  577          * Else, remove that file it shouldn't be here !
  578          */
  579             else {
  580         if (rpm2htmlVerbose)
  581             fprintf(stderr, "Cleaning: removing file %s\n", path);
  582         unlink(path);
  583         nb_removed_files++;
  584             continue;
  585         }
  586 
  587     }
  588     }
  589     closedir(d);
  590     end_time = time(NULL);
  591     /* if (rpm2htmlVerbose) */
  592         printf("Cleanup took %d second, %d files removed\n",
  593            end_time - start_time, nb_removed_files);
  594 }
  595