Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions
fileurl.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define LDAPTOOL_FILEURL_SUCCESS   0
#define LDAPTOOL_FILEURL_NOTAFILEURL   1
#define LDAPTOOL_FILEURL_MISSINGPATH   2
#define LDAPTOOL_FILEURL_NONLOCAL   3
#define LDAPTOOL_FILEURL_NOMEMORY   4
#define LDAPTOOL_FILEURL_FILEIOERROR   5

Functions

int ldaptool_fileurl2path (const char *fileurl, char **localpathp)
int ldaptool_path2fileurl (char *path, char **urlp)
int ldaptool_berval_from_ldif_value (const char *value, int vlen, struct berval *bvp, int recognize_url_syntax, int always_try_file, int reporterrs)
int ldaptool_fileurlerr2ldaperr (int lderr)

Define Documentation

Definition at line 99 of file fileurl.h.

Definition at line 96 of file fileurl.h.

Definition at line 98 of file fileurl.h.

Definition at line 97 of file fileurl.h.

Definition at line 95 of file fileurl.h.

Definition at line 94 of file fileurl.h.


Function Documentation

int ldaptool_berval_from_ldif_value ( const char *  value,
int  vlen,
struct berval bvp,
int  recognize_url_syntax,
int  always_try_file,
int  reporterrs 
)

Definition at line 233 of file fileurl.c.

{
    int       rc = LDAPTOOL_FILEURL_SUCCESS;     /* optimistic */
    struct stat      fstats;

    /* recognize "attr :< url" syntax if LDIF version is >= 1 */

    if ( recognize_url_syntax && *value == '<' ) {
        const char   *url;
        char         *path;

       for ( url = value + 1; isspace( *url ); ++url ) {
           ;  /* NULL */
       }

       if (strlen(url) > 7 && strncasecmp(url, "file://", 7) == 0) {
         /*
          * We only support file:// URLs for now.
          */
         rc = ldaptool_fileurl2path( url, &path );
         switch( rc ) {
         case LDAPTOOL_FILEURL_NOTAFILEURL:
             if ( reporterrs ) fprintf( stderr, "%s: unsupported URL \"%s\";"
                " use a file:// URL instead.\n", ldaptool_progname, url );
             break;

         case LDAPTOOL_FILEURL_MISSINGPATH:
             if ( reporterrs ) fprintf( stderr,
                  "%s: unable to process URL \"%s\" --"
                  " missing path.\n", ldaptool_progname, url );
             break;

         case LDAPTOOL_FILEURL_NONLOCAL:
             if ( reporterrs ) fprintf( stderr,
                  "%s: unable to process URL \"%s\" -- only"
                  " local file:// URLs are supported.\n",
                  ldaptool_progname, url );
             break;

         case LDAPTOOL_FILEURL_NOMEMORY:
             if ( reporterrs ) perror( "ldaptool_fileurl2path" );
             break;

         case LDAPTOOL_FILEURL_SUCCESS:
             if ( stat( path, &fstats ) != 0 ) {
                if ( reporterrs ) perror( path );
             } else if ( fstats.st_mode & S_IFDIR ) {   
                if ( reporterrs ) fprintf( stderr,
                     "%s: %s is a directory, not a file\n",
                     ldaptool_progname, path );
                rc = LDAPTOOL_FILEURL_FILEIOERROR;
             } else {
                rc = berval_from_file( path, bvp, reporterrs );
             }
             free( path );
             break;

         default:
             if ( reporterrs ) fprintf( stderr,
                  "%s: unable to process URL \"%s\""
                  " -- unknown error\n", ldaptool_progname, url );
         }
       }
    }
    if ( always_try_file && (stat( value, &fstats ) == 0) &&
            !(fstats.st_mode & S_IFDIR)) {       /* get value from file */
       rc = berval_from_file( value, bvp, reporterrs );
    } else {
       bvp->bv_len = vlen;
       if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
           if ( reporterrs ) perror( "malloc" );
           rc = LDAPTOOL_FILEURL_NOMEMORY;
       } else {
           SAFEMEMCPY( bvp->bv_val, value, vlen );
           bvp->bv_val[ vlen ] = '\0';
       }
    }

    return( rc );
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ldaptool_fileurl2path ( const char *  fileurl,
char **  localpathp 
)

Definition at line 78 of file fileurl.c.

{
    const char       *path;
    char      *pathcopy;

    /*
     * Make sure this is a file URL we can handle.
     */
    if ( !str_starts_with( fileurl, "file:" )) {
       return( LDAPTOOL_FILEURL_NOTAFILEURL );
    }

    path = fileurl + 5;            /* skip past "file:" scheme prefix */

    if ( *path != '/' ) {
       return( LDAPTOOL_FILEURL_MISSINGPATH );
    }

    ++path;                 /* skip past '/' at end of "file:/" */

    if ( *path == '/' ) {
       ++path;                     /* remainder is now host/path or /path */
       if ( *path != '/' ) {
           /*
            * Make sure it is for the local host.
            */
           if ( str_starts_with( path, "localhost/" )) {
              path += 9;
           } else {
              return( LDAPTOOL_FILEURL_NONLOCAL );
           }
       }
    } else {         /* URL is of the form file:/path */
       --path;
    }

    /*
     * The remainder is now of the form /path.  On Windows, skip past the
     * leading slash if a drive letter is present.
     */
#ifdef _WINDOWS
    if ( isalpha( path[1] ) && ( path[2] == '|' || path[2] == ':' )) {
       ++path;
    }
#endif /* _WINDOWS */

    /*
     * Duplicate the path so we can safely alter it.
     * Unescape any %HH sequences.
     */
    if (( pathcopy = strdup( path )) == NULL ) {
       return( LDAPTOOL_FILEURL_NOMEMORY );
    }
    hex_unescape( pathcopy );

#ifdef _WINDOWS
    /*
     * Convert forward slashes to backslashes for Windows.  Also,
     * if we see a drive letter / vertical bar combination (e.g., c|)
     * at the beginning of the path, replace the '|' with a ':'.
     */
    {
       char   *p;

       for ( p = pathcopy; *p != '\0'; ++p ) {
           if ( *p == '/' ) {
              *p = '\\';
           }
       }
    }

    if ( isalpha( pathcopy[0] ) && pathcopy[1] == '|' ) {
       pathcopy[1] = ':';
    }
#endif /* _WINDOWS */

    *localpathp = pathcopy;
    return( LDAPTOOL_FILEURL_SUCCESS );
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 321 of file fileurl.c.

{
    int              rc;

    switch( lderr ) {
    case LDAPTOOL_FILEURL_SUCCESS:
       rc = LDAP_SUCCESS;
       break;
    case LDAPTOOL_FILEURL_NOMEMORY:
       rc = LDAP_NO_MEMORY;
       break;
    default:
       rc = LDAP_PARAM_ERROR;
    }

    return( rc );
} 

Here is the caller graph for this function:

int ldaptool_path2fileurl ( char *  path,
char **  urlp 
)

Definition at line 173 of file fileurl.c.

{
    char      *p, *url, *prefix ="file:";

    if ( NULL == path ) {
       path = "/";
    }

    /*
     * Allocate space for the URL, taking into account that path may
     * expand during the hex escaping process.
     */
    if (( url = malloc( strlen( prefix ) + 3 * strlen( path ) + 1 )) == NULL ) {
       return( LDAPTOOL_FILEURL_NOMEMORY );
    }

    strcpy( url, prefix );
    p = url + strlen( prefix );

#ifdef _WINDOWS
    /*
     * On Windows, convert leading drive letters (e.g., C:) to the correct URL
     * syntax (e.g., C|).
     */
    if ( isalpha( path[0] ) && path[1] == ':' ) {
       *p++ = path[0];
       *p++ = '|';
       path += 2;
       *p = '\0';
    }
#endif /* _WINDOWS */

    /*
     * Append the path, encoding any URL-special characters using the %HH
     * convention.
     * On Windows, convert backwards slashes in the path to forward ones.
     */
    strcpy_escaped_and_convert( p, path );

    *urlp = url;
    return( LDAPTOOL_FILEURL_SUCCESS );
}

Here is the call graph for this function:

Here is the caller graph for this function: