Exemplo n.º 1
0
static int cddb_write_cache(cddb_data_t *cddb_data)
{
    // We have the file, save it for cache.
    struct stat file_stat;
    char file_name[100];
    int file_fd, ret;
    int wrote = 0;

    if (cddb_data == NULL || cddb_data->cache_dir == NULL)
        return -1;

    // Check if the CDDB cache dir exist
    ret = stat(cddb_data->cache_dir, &file_stat);
    if (ret < 0) {
        // Directory not present, create it.
        ret = mkdir(cddb_data->cache_dir, 0755);
#ifdef __MINGW32__
        if (ret < 0 && errno != EEXIST) {
#else
        if (ret < 0) {
#endif
            perror("mkdir");
            mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
                    cddb_data->cache_dir);
            return -1;
        }
    }

    sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);

    file_fd = creat(file_name, S_IRUSR | S_IWUSR);
    if (file_fd < 0) {
        perror("create");
        return -1;
    }

    wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
    if (wrote < 0) {
        perror("write");
        close(file_fd);
        return -1;
    }
    if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
        mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
        close(file_fd);
        return -1;
    }

    close(file_fd);

    return 0;
}

static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
{
    unsigned long disc_id;
    char category[100];
    char *ptr = NULL, *ptr2 = NULL;
    int ret, status;

    if (http_hdr == NULL || cddb_data == NULL)
        return -1;

    ret = sscanf(http_hdr->body, "%d ", &status);
    if (ret != 1) {
        mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
        return -1;
    }

    switch (status) {
    case 210:
        ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
                     category, &disc_id);
        if (ret != 3) {
            mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
            return -1;
        }
        // Check if it's a xmcd database file
        ptr = strstr(http_hdr->body, "# xmcd");
        if (ptr == NULL) {
            mp_tmsg(MSGT_DEMUX, MSGL_ERR,
                    "Invalid xmcd database file returned.\n");
            return -1;
        }
        ptr = strdup(ptr);
        // Ok found the beginning of the file
        // look for the end
        ptr2 = strstr(ptr, "\n.\r\n");
        if (!ptr2)
            ptr2 = strstr(ptr, "\n.\n");
        if (ptr2) {
            ptr2++;
        } else {
            mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
            ptr2 = ptr + strlen(ptr); //return -1;
        }
        // Ok found the end
        // do a sanity check
        if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
            mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
            return -1;
        }
        cddb_data->xmcd_file                            = ptr;
        cddb_data->xmcd_file_size                       = ptr2 - ptr;
        cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
        return cddb_write_cache(cddb_data);
    default:
        mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
    }
    return 0;
}
Exemplo n.º 2
0
int
cddb_write_cache(cddb_data_t *cddb_data) {
	// We have the file, save it for cache.
	struct stat file_stat;
	char file_name[100];
	int file_fd, ret;
	int wrote=0;

	if( cddb_data==NULL || cddb_data->cache_dir==NULL ) return -1;

	// Check if the CDDB cache dir exist
	ret = stat( cddb_data->cache_dir, &file_stat );
	if( ret<0 ) {
		// Directory not present, create it.
		ret = mkdir( cddb_data->cache_dir, 0755 );
#ifdef __MINGW32__
		if( ret<0 && errno != EEXIST ) {
#else
		if( ret<0 ) {
#endif
			perror("mkdir");
			mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToCreateDirectory, cddb_data->cache_dir);
			return -1;
		}
	}
	
	sprintf( file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id );
	
	file_fd = creat(file_name, S_IREAD|S_IWRITE);
	if( file_fd<0 ) {
		perror("create");
		return -1;
	}
	
	wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
	if( wrote<0 ) {
		perror("write");
		close(file_fd);
		return -1;
	}
	if( (unsigned int)wrote!=cddb_data->xmcd_file_size ) {
		mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenWritten);
		close(file_fd);
		return -1;
	}
	
	close(file_fd);

	return 0;
}

int
cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
	unsigned long disc_id;
	char category[100];
	char *ptr=NULL, *ptr2=NULL;
	int ret, status;

	if( http_hdr==NULL || cddb_data==NULL ) return -1;
	
	ret = sscanf( http_hdr->body, "%d ", &status);
	if( ret!=1 ) {
		mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
		return -1;
	}

	switch(status) {
		case 210:
			ret = sscanf( http_hdr->body, "%d %99s %08lx", &status, category, &disc_id);
			if( ret!=3 ) {
				mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
				return -1;
			}
			// Check if it's a xmcd database file
			ptr = strstr(http_hdr->body, "# xmcd");
			if( ptr==NULL ) {
				mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_InvalidXMCDDatabaseReturned);
				return -1;
			}
			// Ok found the beginning of the file
			// look for the end
			ptr2 = strstr(ptr, "\r\n.\r\n");
			if( ptr2==NULL ) {
				ptr2 = strstr(ptr, "\n.\n");
				if( ptr2==NULL ) {
					mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
					ptr2=ptr+strlen(ptr); //return -1;
				}
			}
			// Ok found the end
			// do a sanity check
			if( http_hdr->body_size<(unsigned int)(ptr2-ptr) ) {
				mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_UnexpectedFIXME);
				return -1;
			}
			cddb_data->xmcd_file = ptr;
			cddb_data->xmcd_file_size = ptr2-ptr+2;
			cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
			// Avoid the http_free function to free the xmcd file...save a mempcy...
			http_hdr->body = NULL;
			http_hdr->body_size = 0;
			return cddb_write_cache(cddb_data);
		default:
			mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode);
	}
	return 0;
}