PrequelD.c: A series of small clean-up fixes.
authorChristopher R. Hertel <crh@ubiqx.org>
Mon, 22 Sep 2014 23:49:59 +0000 (18:49 -0500)
committerChristopher R. Hertel <crh@ubiqx.org>
Mon, 22 Sep 2014 23:49:59 +0000 (18:49 -0500)
These are changes that were being kept in a separate branch and really
did need to be merged (just to get them out of the way).

src/daemon/PrequelD.c

index ec28e53b71c12c61921bdd6c465568daff65b713..320432b40e33b6f3524a8dba9be9a394ef9991d9 100644 (file)
@@ -2,11 +2,11 @@
  *                                 PrequelD.c
  *
  * Copyright:
- *  Copyright (C) 2012 by Christopher R. Hertel
+ *  Copyright (C) 2012-2014 by Christopher R. Hertel
  *
  * Email: crh@ubiqx.mn.org
  *
- * $Id: PrequelD.c; 2014-09-22 17:14:11 -0500; Christopher R. Hertel$
+ * $Id: PrequelD.c; 2014-09-22 18:49:59 -0500; Christopher R. Hertel$
  *
  * -------------------------------------------------------------------------- **
  *
  *    PeerDist  - The protocol and/or encoding format used by Microsoft's
  *                BranchCacheTM distributed caching system.
  *
- *    Prequel   - The name of an Open Source project aimed at implementing
- *                PeerDist for use with Linux, *BSD, and other Unix-like
- *                OSes.
+ *    Prequel   - The name of this Open Source project, which is aimed at
+ *                implementing PeerDist for use with Linux, *BSD, and other
+ *                Unix-like OSes.
  *
  *  References:
- *    [BCOVIEW]:    MS W2K8R2 BranchCache Overview
- *                  http://technet.microsoft.com/en-us/library/dd637832.aspx
+ *    [BCOVIEW]:  MS W2K8R2 BranchCache Overview
+ *                http://technet.microsoft.com/en-us/library/dd637832.aspx
  *
- *    [MS-CCRSOD]:  Content Caching and Retrieval System Overview
- *                  http://msdn.microsoft.com/en-us/library/ff632262.aspx
+ *    [MS-CCROD]: Content Caching and Retrieval Protocols Overview
+ *                http://msdn.microsoft.com/en-us/library/hh441913.aspx
  *
- *    [MS-PCCRC]:   Peer Content Caching and Retrieval: Content Identification
- *                  http://msdn.microsoft.com/en-us/library/dd303704.aspx
+ *    [MS-PCCRC]: Peer Content Caching and Retrieval: Content Identification
+ *                http://msdn.microsoft.com/en-us/library/dd303704.aspx
  *
  *  To Compile:
  *    cc -I.. -D_FILE_OFFSET_BITS=64 -o PrequelD PrequelD.c \
  *      ../ubi_sLinkList.c -lcrypto -lpthread
  *
  *  ToDo:
- *    + Add support for Unicode pathnames.
+ *    + Combine all mutex-protected global values into a single structure,
+ *      with the mutex included.  That structure should be viewed as an
+ *      object that accessed via methods (that leverage the mutex).
+ *
+ *    + Verify support for Unicode pathnames.
+ *
+ *    + Get rid of bSIZE.
  *
  *    + The background thread should traverse source directories and generate
  *      hash caches for any file that meets criteria and does not have an
 #define RECV_TIMEOUT { 2, 0 }     /*  2 seconds.  */
 #define POLL_TIMEOUT 30000        /* 30 seconds.  */
 
-#define bSIZE 1024
+#define bSIZE 1024                /* 1K bytes.    */
 
 
 /* -------------------------------------------------------------------------- **
  *              is true, then the client did provide a value and that value
  *              is stored in the <val> field.
  *
- *  WorkItem  - A queue entry and a string pointer pointing to the pathname
- *              of a source file that is in need of hashing.
- *
+ *  WorkItem  - A queue entry node and a string pointer pointing to the
+ *              pathname of a source file that is in need of hashing.
  */
 
 typedef struct
@@ -249,14 +254,13 @@ typedef struct
  */
 
 static const char *Copyright =
-                    "Copyright (c) 2012 by Christopher R. Hertel";
+        "Copyright (c) 2012, 2013 by Christopher R. Hertel";
 static const char *License =
-                    "License: GNU General Public License version 3 (GPLv3)\n"
-                    "         http://www.gnu.org/licenses/gpl-3.0.html";
-static const char *Revision =
-                    "$Revision$";
+        "License: GNU General Public License version 3 (GPLv3)\n"
+        "         http://www.gnu.org/licenses/gpl-3.0.html";
+static const char *Revision = "$Revision$";
 static const char *Id =
-                    "$Id: PrequelD.c; 2014-09-22 17:14:11 -0500; Christopher R. Hertel$";
+        "$Id: PrequelD.c; 2014-09-22 18:49:59 -0500; Christopher R. Hertel$";
 
 static const char *HelpMsg[] =
   {
@@ -769,7 +773,7 @@ static void *WorkerMain( void *thingy )
    */
   {
   static enum { wNone, wStop, wQueue, wDir } doState;   /* What shall we do?  */
-  static struct timespec ts = { 0, 500000000 };         /* Half a second.     */
+  static struct timespec ts       = { 0, 500000000 };   /* Half a second.     */
   WorkItem              *srcEntry = NULL;
 
   do
@@ -780,7 +784,6 @@ static void *WorkerMain( void *thingy )
     /* Determine which action to take, and collect any required data.
      *  This requires locking/unlocking the global mutex.
      */
-    doState = wNone;      /* Default action.  */
     if( 0 == pthread_mutex_lock( &GlobalMutex ) )
       {
       /* Note:  See pthread_mutex_lock(3).  We do not check for an error
@@ -788,6 +791,7 @@ static void *WorkerMain( void *thingy )
        *        to occurr and, if an error were to occur, we don't have a
        *        sensible way of handling it (other than to wait and retry).
        */
+      doState = wNone;      /* Default action.  */
       if( Shutdown )
         doState = wStop;
       else
@@ -796,6 +800,7 @@ static void *WorkerMain( void *thingy )
           doState = wDir;
         else
           {
+          /* Do all of this now, while we have the mutex. */
           srcEntry = (WorkItem *)ubi_slDequeue( WorkQueue );
           if( (NULL != srcEntry) && (NULL != srcEntry->srcPath) )
             {
@@ -803,7 +808,7 @@ static void *WorkerMain( void *thingy )
             if( Verbosity > 2 )
               Log( "Dequeued: %s\n", srcEntry->srcPath );
             }
-          else
+          else  /* Should never happen. */
             Log( "Error: NULL entry in priority queue.\n" );
           }
         }
@@ -823,7 +828,7 @@ static void *WorkerMain( void *thingy )
         break;
       case wDir:
         /* Hash an entry from a source directory. */
-        /* FIX: Need directory traversal. */
+        /* PreCalcHash();  FIX:  Write PreCalcHash(); */
         break;
       default:
         break;
@@ -1122,28 +1127,28 @@ static void ReadOpts( int argc, char *argv[] )
     switch( c )
       {
       case 'c':
-        ConfigFile  = optarg;
+        ConfigFile = optarg;
         break;
       case 'f':
-        Daemonize   = false;
+        Daemonize = false;
         break;
       case 'h':
-        SpewHelp    = true;
+        SpewHelp = true;
         break;
       case 'l':
-        LogFile     = optarg;
+        LogFile = optarg;
         break;
       case 'q':
-        Verbosity   = 0;
-        VerbSet     = true;
+        Verbosity = 0;
+        VerbSet   = true;
         break;
       case 't':
-        TestConfig  = true;
+        TestConfig = true;
         break;
       case 'v':
         if( Verbosity < 0xFF )
           Verbosity++;
-        VerbSet     = true;
+        VerbSet = true;
         break;
       case 'V':
         SpewVersion = true;
@@ -1297,6 +1302,12 @@ int main( int argc, char *argv[] )
    *
    *  Output: EXIT_SUCCESS or, on failure, EXIT_FAILURE.
    *
+   *  Notes:  The program has two threads.  The foreground thread listens for
+   *          service requests on a Unix Domain Socket.  The background
+   *          thread (called the "worker") maintains the hash cache files.
+   *
+   *          See the comments at the top of this file for more details.
+   *
    * ------------------------------------------------------------------------ **
    */
   {
@@ -1308,14 +1319,14 @@ int main( int argc, char *argv[] )
   ReadOpts( argc, argv );         /* Get all set up to run. */
   ReadKeys();                     /* Find the signing keys. */
 
-  /* Become a daemon, unless we were told otherwise.  */
+  /* Unless we were told otherwise, let's become a daemon.  */
   if( Daemonize )
     Spawn();
 
   /* Before creating any subthreads, let's try to open the listening socket.  */
   listenSock = OpenListenSock( Cfg->sockFileName );
 
-  /* Initialize the priority queue and start the worker thread. */
+  /* Initialize the priority queue, and start the worker thread.  */
   (void)ubi_slInitList( WorkQueue );
   StartWorker();