X-Git-Url: http://git.openpandora.org/cgi-bin/gitweb.cgi?p=pandora-libraries.git;a=blobdiff_plain;f=lib%2Fpnd_utility.c;h=17fb322a65c4e0390a616e3e875d05d315fc90ed;hp=3733ad09d62d027514a9e580247e9d701114cd5e;hb=HEAD;hpb=c414a3f418f191163d5dd8813ba5d1df599d3c42 diff --git a/lib/pnd_utility.c b/lib/pnd_utility.c index 3733ad0..17fb322 100644 --- a/lib/pnd_utility.c +++ b/lib/pnd_utility.c @@ -8,6 +8,10 @@ #include /* for expand-tilde below; see commentary for this about-turn */ #include /* ditto */ #include /* ditto */ +#include /* for fstat */ +#include /* for stat->ENOENT */ +#include /* for opendir */ +#include /* for creat */ #include "pnd_pxml.h" #include "pnd_container.h" @@ -65,8 +69,8 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { char *s = freeable_buffer; char *home = getenv ( "HOME" ); - printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer ); - printf ( "DEBUG: $HOME was %s\n", home ); + //printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer ); + //printf ( "DEBUG: $HOME was %s\n", home ); // well, as pndnotifyd (etc) may be running as _root_, while the user is logged in // as 'pandora' or god knows what, this could be problematic. Other parts of the lib @@ -104,7 +108,7 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { florp = strdup ( pw -> pw_dir ); home = florp; - printf ( " DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type ); + //printf ( " DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type ); } // passwd entry matches the utmp entry @@ -124,10 +128,10 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { return ( s ); // can't succeed } - printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home ); + //printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home ); while ( ( p = strchr ( s, '~' ) ) ) { - printf ( "DEBUG: within while (%s)\n", s ); + //printf ( "DEBUG: within while (%s)\n", s ); char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 ); memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 ); // copy in stuff prior to ~ @@ -141,7 +145,7 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { s = temp; } // while finding matches - printf ( "DEBUG: expand tilde OUT: '%s'\n", s ); + //printf ( "DEBUG: expand tilde OUT: '%s'\n", s ); return ( s ); } @@ -165,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! @@ -190,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; @@ -212,7 +219,7 @@ pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) { } // by now, we have .. , try to parse.. - pxmlh = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf ); + pxmlapps = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf ); // done with file fclose ( f ); @@ -221,5 +228,206 @@ 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 int mountpoint_len ) { + + // just cheap it, and call df like an idiot. + + // Filesystem 1K-blocks Used Available Use% Mounted on + + char cmd [ PATH_MAX ]; + FILE *p; + char inbuf [ PATH_MAX ]; + + sprintf ( cmd, "/bin/df %s 2>/dev/null", fullpath ); + + if ( ( p = popen ( cmd, "r" ) ) ) { + + // ignore title line; we really shoudl analyze it to figure out which column, but we make assumptions.. + fgets ( inbuf, PATH_MAX, p ); + + if ( ! fgets ( inbuf, PATH_MAX, p ) ) { + pclose ( p ); + return ( 0 ); + } + + pclose ( p ); + + // by now, good + char mount [ PATH_MAX ]; + if ( sscanf ( inbuf, "%*s %*s %*s %*s %*s %s", mount ) != 1 ) { + return ( 0 ); + } + + if ( strlen ( mount ) < mountpoint_len ) { + strcpy ( r_mountpoint, mount ); + return ( 1 ); + } + + } // if popen + + return ( 0 ); + +#if 0 + struct stat fooby; + + // can we even stat this file? + if ( stat ( fullpath, &fooby ) == 0 ) { + //dev_t st_dev; /* ID of device containing file */ + //dev_t st_rdev; /* device ID (if special file) */ + + dev_t mount = fooby.st_dev; + + DIR *d = opendir ( "/dev" ); + + if ( d ) { + struct dirent *de; + char path [ FILENAME_MAX ]; + + while ( de = readdir ( d ) ) { + sprintf ( path, "/dev/%s", de -> d_name ); + + if ( stat ( path, &fooby ) == 0 ) { + + // finally, if we find the same major/minor pair in /dev, as we found for the target file, it means we found the right device + if ( fooby.st_rdev == mount ) { + printf ( "Device: %s\n", path ); + } + + } // if + + } // while + + } // opened /dev? + + } // stat +#endif + + 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 ); }