Exemple #1
0
int main(void) {
	LIST *list;
#ifdef SYSTEM_OS_TYPE_WINDOWS
	list = directory_list("C:\\WINDOWS", TRUE);
#else
	list = directory_list("/etc", TRUE);
#endif

	list_show(list);

	list_free(list);

	return 0;
}
Exemple #2
0
int directory_list(const char *path, bool recursive,
                   directory_list_callback_t callback, void *user_data)
{
    DIR *dir;
    struct dirent *entry;
    struct stat info;
    int error = 0;
    char entry_path[PATH_MAX];
    bool prune = false;

    if (!(dir = opendir(path))) {
        error = errno;
        return error;
    }

    for (entry = readdir(dir); entry; entry = readdir(dir)) {

        if (strcmp(entry->d_name, ".") == 0 ||
            strcmp(entry->d_name, "..") == 0) {
            continue;
        }

        error = path_concat(entry_path, sizeof(entry_path),
                            path, entry->d_name);
        if (error != SUCCESS) {
            closedir(dir);
            /* Don't bother checking the return here.
             * The path_concat error is more important
             */
            return error;
        }

        if (lstat(entry_path, &info) < 0) {
            continue;
        }

        prune = !callback(path, entry->d_name, entry_path, &info, user_data);
        if (S_ISDIR(info.st_mode)) {
            if (recursive && !prune) {
                error = directory_list(entry_path, recursive,
                                       callback, user_data);
                if (error != SUCCESS) {
                    closedir(dir);
                    /* Don't bother checking the return here.
                     * The directory_list error is more important
                     */
                    return error;
                }
            }
        }
    }
    error = closedir(dir);
    if (error) {
        return error;
    }
    return SUCCESS;
}
Exemple #3
0
static int serve_client_out(CLIENT *client, int sock, const char *dir, 
    const char *hostname)
{
    FILE *fp = NULL;
    FILESTATS fstats;
    int  i, connected, file_readcnt, file_sendcnt, buflen, nbytes;

    const char *path;
    char out_dir[PATH_LEN+1];
    unsigned char buf[CH_MSG_SIZE];

    DIRECTORY directory;

    /* create incoming file path */
    sprintf(out_dir, "%s/%s/%s", dir, client->hostname, CHOUSESYS_OUTLOC);

    if ( !directory_exists(out_dir) )
    {
        fprintf(stderr, "%s: %s: error: non-existant out directory: %s\n",
                timestamp(), ProgName, out_dir); 
        return 1;
    }

    directory_init(&directory, out_dir);
    directory_open(&directory);

    /* Set file send count to 0 */
    errno = 0;
    file_readcnt = 0;
    file_sendcnt = 0;
    connected = TRUE;

    while ( (path = directory_list(&directory)) )
    {
        /* Set file statistics to 0 */
        memset(&fstats, 0, sizeof(fstats));

        if ( (fstats.name = (char *)strrchr(path, '/')) == NULL )
            fstats.name = path; /* no path in path */
        else
            fstats.name++; /* skip leading '/' */

#ifdef DEBUG
        if ( Debug > 2 )
        {
            PRINT_DBGLOC;
            fprintf(stderr, "sending %s\n", fstats.name);
        }
#endif
        if ( (fp = fopen(path, "r")) == NULL )
        {
            /* soft error */
            fprintf(stderr, "%s: %s: error: couldn't open %s: %s\n",
                timestamp(), ProgName, basename(path), strerror(ferror(fp))); 
            continue;
        }
        file_readcnt++;

        /* send current file */
        while ( fstats.filesize == fstats.socksize )
        {
            if ( (buflen = fread(buf, sizeof(*buf), sizeof(buf), fp)) == 0 )
            {
                /* finished reading file */
                break;
            }
#ifdef DEBUG
            if ( Debug > 7 )
            {
                PRINT_DBGLOC;
                print_sockseg(buf, buflen);
            }
#endif
            fstats.filesize += buflen;
            fstats.socksize += send(sock, buf, buflen, 0);
        }

        if ( fstats.filesize != fstats.socksize )
        {
                /* error */
                fstats.transfered = FALSE;
        }
        else
        {
            /* finished sending file data - now send eod string */
            nbytes = send(sock, client->eod.hex_buf, client->eod.hex_buflen, 0);
            if ( nbytes != client->eod.hex_buflen ) 
            {
                /* error */
                fprintf(stderr, 
                    "%s: %s: warning: send end-of-data to %s failed\n", 
                    timestamp(), ProgName, hostname);
                fstats.transfered = FALSE; 
            }
            else
            {
                fstats.transfered = TRUE; 
#ifdef DEBUG
                if ( Debug > 3 )
                {
                    PRINT_DBGLOC;
                    print_sockseg(client->eod.hex_buf, client->eod.hex_buflen);
                }
#endif
            }
        }

        /*  finished with current file */
        fclose(fp);

        /* log statistics for this file */
        if ( fstats.transfered )
        {
            file_sendcnt++;
            fprintf(stderr, "%s: %s: info: ", timestamp(), ProgName);
            fprintf(stderr, "send \"%s (%d bytes)\" succeeded to %s\n",
                fstats.name, fstats.filesize, hostname);
        }
        else
        {
            fprintf(stderr, "%s: %s: error: ", timestamp(), ProgName);
            fprintf(stderr, "send \"%s (%d bytes) failed to %s: ", 
                fstats.name, fstats.filesize, hostname);
            fprintf(stderr, "%d/%d bytes read/sent\n",
                    fstats.filesize, fstats.socksize);
        }

    } /* while ( (path = directory_list(&directory)) ) */

    /* close the directory */
    directory_close(&directory);

    /* The port number must be converted first to host byte
     * order before printing.  On most hosts, this is not
     * necessary, but the ntohs() call is included here so
     * that this program could easily be ported to a host
     * that does require it.
     */
    fprintf(stderr, "%s: %s: info: completed %s: %d of %d files sent\n",
        timestamp(), ProgName, hostname, file_readcnt, file_sendcnt);

    return 0;

} /* serve_client_out() */