version 0.31
[FAPG] / fapg.c
diff --git a/fapg.c b/fapg.c
index e75235e..8a6667d 100644 (file)
--- a/fapg.c
+++ b/fapg.c
@@ -1,5 +1,5 @@
 /*
- * FAPG version 0.30
+ * FAPG version 0.31
  *
  * FAPG means Fast Audio Playlist Generator.
  * It is a tool to generate list of audio files (Wav, MP3, Ogg, etc)
@@ -36,6 +36,7 @@
 #include <limits.h>
 #include <unistd.h>
 #include <ctype.h>
+#include <assert.h>
 #include "genres.h"
 
 #define MP3_BASE 1024
@@ -47,6 +48,7 @@ int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */
 char *genrelist=NULL;
 unsigned char *prefix="";
 int recursive=0;
+int avoidhlinked=0;
 int separator='/';
 int skip=0;
 int windows=0;
@@ -65,19 +67,20 @@ unsigned char unix2dos[256]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2
 
 void usage()
 {
-  fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html] [-g|--genre=#:#:...] [-o|--output=/path/to/file.m3u] [-p|--prefix=/the/prefix] [-r|--recursive] [-w|--windows] [-x|--exclude=#:#:...] /path/to/mp3/dir1 [/path/to/mp3/dir2 ...]\n");
+  fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html] [-g|--genre=#:#:...] [-n|--nohardlink] [-o|--output=/path/to/file.m3u] [-p|--prefix=/the/prefix] [-r|--recursive] [-w|--windows] [-x|--exclude=#:#:...] /path/to/mp3/dir1 [/path/to/mp3/dir2 ...]\n");
   exit(1);
 }
 
 void parse_options(int argc,char **argv)
 {
-  static char const short_options[]="bdf:g:o:p:rwx:";
+  static char const short_options[]="bdf:g:lo:np:rwx:";
   static struct option long_options[]=
   {
     {"backslash",no_argument,NULL,'b'},
     {"debug",no_argument,NULL,'d'},
     {"format",required_argument,NULL,'f'},
     {"genre",required_argument,NULL,'g'},
+    {"nohardlink",no_argument,NULL,'n'},
     {"output",required_argument,NULL,'o'},
     {"prefix",required_argument,NULL,'p'},
     {"recursive",no_argument,NULL,'r'},
@@ -116,6 +119,9 @@ void parse_options(int argc,char **argv)
           }
         }
         break;
+      case 'n':
+        avoidhlinked=1;
+        break;
       case 'o':
         close(1);
         if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
@@ -473,6 +479,69 @@ void parse_mpc(unsigned char *file)
   fclose(fic);
 }
 
+#define FSN  8
+#define MAXINO  (1<<22)
+#define INOTYP  unsigned long
+#define regbit_qry(x,y)  ( x[( (y) / sizeof(INOTYP) )]  &  1<<( (y) % sizeof(INOTYP) ) )
+#define regbit_set(x,y)  ( x[( (y) / sizeof(INOTYP) )]  |=  1<<( (y) % sizeof(INOTYP) ) )
+
+int hlink_check(struct stat*info)
+{
+  /*
+   * for speed this subroutine should only be called
+   * - if the file has more than one hardlink
+   * - if the file is a resolved softlink
+   */
+  /* the persistent variables */
+  static INOTYP *list[FSN];
+  static dev_t name[FSN];
+  /* some temporary variables */
+  int fsn, is_registered=0;
+
+  /* assertions - in case parameters are lowered for less memory usage */
+  assert(fsn<FSN);
+  assert((info->st_ino)/sizeof(INOTYP)<MAXINO);
+  
+  /* search which internal registration number is used for this filesystem */
+  for( fsn=0; (name[fsn]!=(info->st_dev)) && (name[fsn]!=0) ; fsn++);
+
+  /* if file system is not registered yet, do it and leave */
+  if( name[fsn] == 0 ) 
+  {
+    name[fsn] = (info->st_dev);  
+    /* provide space for the bitmap that maps the inodes of this file system */
+    list[fsn] = (INOTYP*)calloc(MAXINO,sizeof(INOTYP));
+    /* no comparison is needed in empty lists ... return */
+    if(debug) fprintf(stderr, "Debug >> Linked >> Init List %04x @mem %04lx\n", (int)name[fsn], (long)&list[fsn] );
+  } else
+  {
+    /* this looks more complicated than it really is */
+    /* the idea is very simple: 
+     *  provide a bitmap that maps all inodes of a file system
+     *  to mark all files that have already been visited.
+     *  If it is already visited, do not add it to the playlist
+     */
+    /*
+     * The difficulty is as follows:
+     *   struct inode_bitmap { char registered:1; } bitmap[1<<MAXINO];
+     * would be byte-aligned and would allocate at least eight times the needed space.
+     * Feel free to change the definitions that are involved here, if you know better.
+     */
+    if(  regbit_qry( list[fsn], (info->st_ino) ) ) is_registered=1;
+    else regbit_set( list[fsn], (info->st_ino) );
+    /*
+     * the debug expression is more complicated then the working stuff
+     */
+    if(debug)
+      fprintf(stderr, "Debug >> Linked >> DEV %04x INO %06x => "
+    "list[%02x][%04x] = %04x & %04x --> %s registered\n",
+    (int)info->st_dev, (int)info->st_ino, fsn, (int)((info->st_ino)/sizeof(INOTYP)),
+          (int)list[fsn][(info->st_ino)/sizeof(INOTYP)],
+    1<<((info->st_ino)%sizeof(INOTYP)), is_registered?"Already":"Not" );
+  }
+  return is_registered;
+}
+
 void parse_directory(unsigned char *path)
 {
   int i,n;
@@ -505,7 +574,8 @@ void parse_directory(unsigned char *path)
     sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
     if (stat(newpath,&infos)!=0) { fprintf(stderr,"Warning >> can't stat file : %s\n",newpath); continue; }
     if (recursive && S_ISDIR(infos.st_mode) && strcmp(namelist[i]->d_name,".")!=0 && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
-    if (S_ISREG(infos.st_mode))
+    /* hlink_check() might be applied more selective ... avoidhlink is only a simple prereq */
+    if (S_ISREG(infos.st_mode) && ! ( avoidhlinked && hlink_check(&infos) ) )
     {
       unsigned char ext[5];
       int j;
@@ -572,7 +642,7 @@ int main(int argc,char **argv)
       printf("[playlist]%s",eol);
       break;
     case 2:
-      printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">%s%s<html>%s%s<head>%s<title>Playlist generated by FAPG 0.30</title>%s<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />%s<style type=\"text/css\">%s<!--%s%sbody,td,tr {%s font-family: Verdana, Arial, Helvetica, sans-serif;%s  font-size: 12px;%s  color: #000000;%s}%s%sbody {%s  background: #ffffff;%s}%s%sth {%s  text-align: center;%s  background: #ffcccc;%s  padding-left: 15px;%s  padding-right: 15px;%s  border: 1px #dd8888 solid;%s}%s%std {%s  text-align: center;%s  background: #eeeeee;%s  padding-left: 15px;%s  padding-right: 15px;%s  border: 1px #cccccc solid;%s}%s%sh1 {%s  font-size: 25px;%s}%s%sp {%s  font-size: 10px;%s}%s%sa {%s  color: #993333;%s  text-decoration: none;%s}%s%sa:hover {%s text-decoration: underline;%s}%s%s-->%s</style>%s</head>%s%s<body>%s%s<h1>Playlist</h1>%s%s<table>%s<tr><th>Entry</th><th>Artist</th><th>Title</th><th>Length</th></tr>%s",eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol);
+      printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">%s%s<html>%s%s<head>%s<title>Playlist generated by FAPG 0.31</title>%s<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />%s<style type=\"text/css\">%s<!--%s%sbody,td,tr {%s font-family: Verdana, Arial, Helvetica, sans-serif;%s  font-size: 12px;%s  color: #000000;%s}%s%sbody {%s  background: #ffffff;%s}%s%sth {%s  text-align: center;%s  background: #ffcccc;%s  padding-left: 15px;%s  padding-right: 15px;%s  border: 1px #dd8888 solid;%s}%s%std {%s  text-align: center;%s  background: #eeeeee;%s  padding-left: 15px;%s  padding-right: 15px;%s  border: 1px #cccccc solid;%s}%s%sh1 {%s  font-size: 25px;%s}%s%sp {%s  font-size: 10px;%s}%s%sa {%s  color: #993333;%s  text-decoration: none;%s}%s%sa:hover {%s text-decoration: underline;%s}%s%s-->%s</style>%s</head>%s%s<body>%s%s<h1>Playlist</h1>%s%s<table>%s<tr><th>Entry</th><th>Artist</th><th>Title</th><th>Length</th></tr>%s",eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol);
       break;
   }
   for (;optind<argc;optind++)
@@ -586,7 +656,7 @@ int main(int argc,char **argv)
       printf("NumberOfEntries=%d%sVersion=2%s",counter,eol,eol);
       break;
     case 2:
-      printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.30</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
+      printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.31</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
       break;
   }
   if (genrelist) free(genrelist);