pndevmapperd: add f-keys
[pandora-libraries.git] / lib / pnd_utility.c
index 5009b51..b011cdd 100644 (file)
@@ -9,7 +9,9 @@
 #include <sys/types.h> /* ditto */
 #include <pwd.h> /* ditto */
 #include <sys/stat.h> /* for fstat */
+#include <errno.h> /* for stat->ENOENT */
 #include <dirent.h> /* for opendir */
+#include <fcntl.h> /* for creat */
 
 #include "pnd_pxml.h"
 #include "pnd_container.h"
@@ -167,15 +169,18 @@ void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
     execl ( fullpath, fullpath, (char*) NULL );
   }
 
+  // error invoking something, and we're the child process, so just die before all hell breaks lose with us thinking we're the (second!) parent on return!
+  exit ( -1 );
+
   // getting here is an error
   //printf ( "Error attempting to run %s\n", fullpath );
 
   return;
 }
 
-pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
+pnd_pxml_handle *pnd_pxml_get_by_path ( char *fullpath ) {
   unsigned char valid = pnd_object_type_unknown;
-  pnd_pxml_handle pxmlh = 0;
+  pnd_pxml_handle *pxmlapps = 0;
 
   // WARN: this is way too close to callback in pnd_disco .. should be refactored!
 
@@ -192,7 +197,7 @@ pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
 
   // potentially a valid application
   if ( valid == pnd_object_type_directory ) {
-    pxmlh = pnd_pxml_fetch ( (char*) fullpath );
+    pxmlapps = pnd_pxml_fetch ( (char*) fullpath );
 
   } else if ( valid == pnd_object_type_pnd ) {
     FILE *f;
@@ -214,7 +219,7 @@ pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
     }
 
     // by now, we have <PXML> .. </PXML>, try to parse..
-    pxmlh = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );
+    pxmlapps = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );
 
     // done with file
     fclose ( f );
@@ -223,10 +228,10 @@ pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
 
   // ..
 
-  return ( pxmlh );
+  return ( pxmlapps );
 }
 
-unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, unsigned char mountpoint_len ) {
+unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, unsigned int mountpoint_len ) {
 
   // just cheap it, and call df like an idiot.
 
@@ -251,9 +256,8 @@ unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, uns
     pclose ( p );
 
     // by now, good
-    char crap [ PATH_MAX ];
     char mount [ PATH_MAX ];
-    if ( sscanf ( inbuf, "%s %s %s %s %s %s", crap, crap, crap, crap, crap, mount ) != 6 ) {
+    if ( sscanf ( inbuf, "%*s %*s %*s %*s %*s %s", mount ) != 1 ) {
       return ( 0 );
     }
 
@@ -303,3 +307,127 @@ unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, uns
 
   return ( 0 );
 }
+
+unsigned char pnd_filecopy ( char *sourcepath, char *targetpath ) {
+#define BITLEN (64*1024)
+  FILE *pnd, *target; // pnd == from, since I cribbed the code from pnd_desktop.c :/
+  unsigned char bits [ BITLEN ];
+  unsigned int bitlen;
+
+  pnd = fopen ( sourcepath, "rb" );
+
+  if ( ! pnd ) {
+    return ( 0 );
+  }
+
+  unsigned int len;
+
+  target = fopen ( targetpath, "wb" );
+
+  if ( ! target ) {
+    fclose ( pnd );
+    return ( 0 );
+  }
+
+  fseek ( pnd, 0, SEEK_END );
+  len = ftell ( pnd );
+  fseek ( pnd, 0, SEEK_SET );
+
+  while ( len ) {
+
+    if ( len > (BITLEN) ) {
+      bitlen = (BITLEN);
+    } else {
+      bitlen = len;
+    }
+
+    if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
+      fclose ( pnd );
+      fclose ( target );
+      unlink ( targetpath );
+      return ( 0 );
+    }
+
+    if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
+      fclose ( pnd );
+      fclose ( target );
+      unlink ( targetpath );
+      return ( 0 );
+    }
+
+    len -= bitlen;
+  } // while
+
+  fclose ( pnd );
+  fclose ( target );
+
+  return ( 1 );
+}
+
+unsigned char pnd_lock ( char *lockname ) {
+
+  if ( pnd_is_locked ( lockname ) ) {
+    return ( 0 ); // already locked
+  }
+
+  char fullpath [ PATH_MAX ];
+  int fd;
+
+  snprintf ( fullpath, PATH_MAX, "%s/%s", PND_LOCK_PATH, lockname );
+
+  if ( ( fd = creat ( fullpath, 0444 ) < 0 ) ) {
+    return ( 0 ); // error, yeah, I know, no way to know why it failed..
+  }
+
+  close ( fd );
+
+  return ( 1 );
+}
+
+time_t pnd_is_locked ( char *lockname ) {
+  char fullpath [ PATH_MAX ];
+  int rv;
+  snprintf ( fullpath, PATH_MAX, "%s/%s", PND_LOCK_PATH, lockname );
+
+  struct stat statbuf;
+  rv = stat ( fullpath, &statbuf );
+
+  if ( rv == ENOENT ) {
+    return ( 0 ); // file not existk, so no lock
+  } else if ( rv < 0 ) {
+    return ( 0 ); // assume unlocked for error, so app can continue?
+  }
+
+  return ( statbuf.st_mtime );
+}
+
+void pnd_unlock ( char *lockname ) {
+  char fullpath [ PATH_MAX ];
+  snprintf ( fullpath, PATH_MAX, "%s/%s", PND_LOCK_PATH, lockname );
+
+  unlink ( fullpath );
+
+  return;
+}
+
+unsigned char pnd_wait_for_unlock ( char *lockname, unsigned short int max, unsigned int usec_delta ) {
+
+  // check right off the top
+  if ( ! pnd_is_locked ( lockname ) ) {
+    return ( 1 ); // all clear!
+  }
+
+  unsigned short int count = 0;
+  while ( count < max ) {
+
+    if ( ! pnd_is_locked ( lockname ) ) {
+      return ( 1 ); // all clear!
+    }
+
+    usleep ( usec_delta );
+
+    count++;
+  }
+
+  return ( 0 );
+}