Couple optimizations --
authorskeezix <skeezix@flotsam-vm.(none)>
Tue, 24 Feb 2009 05:03:36 +0000 (00:03 -0500)
committerskeezix <skeezix@flotsam-vm.(none)>
Tue, 24 Feb 2009 05:03:36 +0000 (00:03 -0500)
- when about to remove non-existant apps .desktop file, check if it has _Source=libpnd line in it.
  If not, leave it alone.. could be a bundled app, or user created or something
- when about to extract icons, if the file already exists, don't bother extracting it again

apps/pndnotifyd.c
include/pnd_discovery.h
lib/pnd_discovery.c
testdata/pndsample/x86_echo.pnd
testdata/pndsample/x86_ls.pnd

index 665c4f6..45b204f 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdio.h>     // for stdio
 #include <unistd.h>    // for exit()
 #include <stdlib.h>    // for exit()
+#define __USE_GNU /* for strcasestr */
 #include <string.h>
 #include <time.h>      // for time()
 #include <ctype.h>     // for isdigit()
@@ -251,15 +252,42 @@ int main ( int argc, char *argv[] ) {
            printf ( "Found app: %s\n", pnd_box_get_key ( d ) );
          }
 
-         // attempt to create icon files; if successful, alter the disco struct to contain new
-         // path, otherwise leave it alone (since it could be a generic icon reference..)
-         if ( pnd_emit_icon ( dotdesktoppath, d ) ) {
-           // success; fix up icon path to new one..
-           free ( d -> icon );
-           char buffer [ FILENAME_MAX ];
-           sprintf ( buffer, "%s/%s.png", dotdesktoppath, d -> unique_id );
-           d -> icon = strdup ( buffer );
-         }
+         // check if icon already exists (from a previous extraction say); if so, we needn't
+         // do it again
+         char existingpath [ FILENAME_MAX ];
+         sprintf ( existingpath, "%s/%s.png", dotdesktoppath, d -> unique_id );
+
+         struct stat dirs;
+         if ( stat ( existingpath, &dirs ) == 0 ) {
+           // icon seems to exist, so just crib the location into the .desktop
+
+           if ( ! g_daemon_mode ) {
+             printf ( "  Found icon already existed, so reusing it! %s\n", existingpath );
+           }
+
+           if ( d -> icon ) {
+             free ( d -> icon );
+           }
+           d -> icon = strdup ( existingpath );
+
+         } else {
+           // icon seems unreadable or does not exist; lets try to create it..
+
+           if ( ! g_daemon_mode ) {
+             printf ( "  Icon not already present, so trying to write it! %s\n", existingpath );
+           }
+
+           // attempt to create icon files; if successful, alter the disco struct to contain new
+           // path, otherwise leave it alone (since it could be a generic icon reference..)
+           if ( pnd_emit_icon ( dotdesktoppath, d ) ) {
+             // success; fix up icon path to new one..
+             if ( d -> icon ) {
+               free ( d -> icon );
+             }
+             d -> icon = strdup ( existingpath );
+           }
+
+         } // icon already exists?
 
          // create the .desktop file
          if ( pnd_emit_dotdesktop ( dotdesktoppath, pndrun, d ) ) {
@@ -312,14 +340,45 @@ int main ( int argc, char *argv[] ) {
              continue;
            }
 
+           // figure out full path
+           sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name );
+
            // file was previously created by libpnd; check Source= line
-           // TBD
+           // logic: default to 'yes' (in case we can't open the file for some reason)
+           //        if we can open the file, default to no and look for the source flag we added; if
+           //          that matches then we know its libpnd created, otherwise assume not.
+           unsigned char source_libpnd = 1;
+           {
+             char line [ 256 ];
+             FILE *grep = fopen ( buffer, "r" );
+             if ( grep ) {
+               source_libpnd = 0;
+               while ( fgets ( line, 255, grep ) ) {
+                 if ( strcasestr ( line, PND_DOTDESKTOP_SOURCE ) ) {
+                   source_libpnd = 2;
+                 }
+               } // while
+               fclose ( grep );
+             }
+           }
+           if ( source_libpnd ) {
+             if ( ! g_daemon_mode ) {
+               printf ( "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd );
+             }
+           } else {
+             if ( ! g_daemon_mode ) {
+               printf ( "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer );
+             }
+             continue; // skip deleting it
+           }
 
            // file is 'new'?
-           sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name );
            if ( stat ( buffer, &dirs ) == 0 ) {
              if ( dirs.st_mtime >= createtime ) {
-               continue;
+               if ( ! g_daemon_mode ) {
+                 printf ( "File '%s' seems 'new', so leave it alone.\n", buffer );
+               }
+               continue; // skip deleting it
              }
            }
 
index 4019e73..19982fe 100644 (file)
@@ -61,6 +61,7 @@ void pnd_disco_destroy ( pnd_disco_t *p ); // a function name that simply could
 
 // emit_dotdesktop() will determine a filename and create a FILENAME.desktop file in the targetpath
 // TODO: Copy the icon into this directory as well, if its source is a .pnd or info is in the dico struct
+#define PND_DOTDESKTOP_SOURCE "_Source=libpnd"
 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p );
 
 // emit_icon() will attempt to copy the icon from a PXML directory, or from a pnd file if appended,
index 6ca6205..cb09703 100644 (file)
@@ -308,7 +308,7 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t
     fprintf ( f, "%s", buffer );
   }
 
-  fprintf ( f, "_Source=libpnd\n" ); // should we need to know 'who' created the file during trimming
+  fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
 
   fclose ( f );
 
index ad943f6..5437373 100644 (file)
Binary files a/testdata/pndsample/x86_echo.pnd and b/testdata/pndsample/x86_echo.pnd differ
index d412a70..994b6b0 100644 (file)
Binary files a/testdata/pndsample/x86_ls.pnd and b/testdata/pndsample/x86_ls.pnd differ