Hi.
Finally got a bit of time to do some hacking today so I've added the
non-dupe code which works by testing the file rid as discussed a
couple of weeks ago.
This patch also changes rio_rw so that the file type is discovered
based on its contents rather than just testing the suffix.
This will be needed by lkarmafs if it uses files in the fids0/ directory
for generating properties since those files don't have suffixes.
Keith.
# HG changeset patch
# User Keith Bennett <[EMAIL PROTECTED]>
# Node ID 5c6fe6b1e9d591c6bcac570bc19771e0b4a61d81
# Parent 30eabda0bf34316d6796ed568747c881a3255f16
Get file type from its contents rather than the suffix.
Don't copy duplicate files onto the karma based on matching rids.
diff -r 30eabda0bf34 -r 5c6fe6b1e9d5 src/rio_rw.c
--- a/src/rio_rw.c Wed Aug 2 23:03:48 2006
+++ b/src/rio_rw.c Sun Aug 13 16:07:35 2006
@@ -2,6 +2,7 @@
* libkarma/rio_rw.c
*
* Copyright (c) Frank Zschockelt <[EMAIL PROTECTED]> 2004
+ * Copyright (c) Keith Bennett <[EMAIL PROTECTED]> 2006
*
* You may distribute and modify this program under the terms of
* the GNU GPL, version 2 or later.
@@ -32,14 +33,17 @@
#define BLOCKSIZE 131072
#define NOT_TAGGED "[karmalib 0.4]"
-static void add_general(HASH * p, const char * file, unsigned int len);
-static int get_ogg_props(HASH *p, const char * oggfile, unsigned int len);
-static int get_flac_props(HASH *p, const char * flacfile, unsigned int len);
-static int get_mp3_props(HASH *p, const char * mp3file);
+enum { TYPE_DATA, TYPE_MP3, TYPE_OGG, TYPE_FLAC, TYPE_WAV, TYPE_WMA };
+
+static void add_general(HASH * p, const char *filename);
+static int get_ogg_props(HASH *p, const char *filename);
+static int get_flac_props(HASH *p, const char *filename);
+static int get_mp3_props(HASH *p, mp3info *mp3);
static int get_taxi_props(HASH *p);
-static int get_wave_props(HASH *p, const char * wavefile);
+static int get_wave_props(HASH *p, const char *filename);
+static void add_tag(HASH *p, char *key, char *value);
static void set_tag_props(HASH *p, const char *filename);
-static void add_tag(HASH *p, char *key, char *value);
+static int get_file_type(mp3info *mp3);
int lk_synchronize_necessary(int rio)
{
@@ -136,14 +140,32 @@
uint32_t new_fid;
uint64_t offs=0;
const uint64_t siz=BLOCKSIZE;
- char * p, tmp[BLOCKSIZE];
- struct stat * len;
+ char * p, tmp[BLOCKSIZE], *rid;
+ struct stat len;
HASH * props;
-
- len=malloc(sizeof(struct stat));
- if(lstat(filename, len)==-1)
- return -1;
-
+ int type, rid_fd;
+ mp3info mp3;
+ uint32_t *fids;
+
+ if(lstat(filename, &len)==-1)
+ return -1;
+
+ memset(&mp3, 0, sizeof(mp3info));
+ mp3.filename = (char*)filename;
+ type = get_file_type(&mp3);
+
+ rid_fd = open(filename, O_RDONLY);
+ rid = (char*)lk_generate_rid(rid_fd, mp3.offset, mp3.datasize);
+ close(rid_fd);
+
+ fids = lk_properties_andOrSearch(EXACT|ORS, NULL, "rid", rid);
+
+ if (fids != NULL) {
+ free(fids);
+ printf("File already present. Skipping...\n");
+ return -1;
+ }
+
new_fid=lk_properties_new_property();
/* uses lk_properties_getnextfid()...*/
props=lk_properties_idsearch(new_fid);
@@ -151,29 +173,26 @@
printf("huh, no hash found?\n");
return -1;
}
- p=strrchr(filename, '.');
-
- add_general(props, filename,len->st_size);
- if(p != NULL){
- ++p;
- if(strcmp(p, "ogg") == 0)
- got=get_ogg_props(props, filename, len->st_size);
- else if(strcmp(p, "flac") == 0)
- got=get_flac_props(props, filename, len->st_size);
- else if(strcmp(p, "mp3") == 0)
- got=get_mp3_props(props, filename);
- else if(strcmp(p, "wav") == 0)
- got=get_wave_props(props,filename);
- /*
- else if(strcmp(p, "wma") == 0)
- got=get_wma_props(props,filename);
- */
- else
- got=get_taxi_props(props);
- }else got=get_taxi_props(props);
-
- free(len);
-
+
+ add_general(props, filename);
+ lk_properties_set_property_hash(props, "offset", simple_itoa(mp3.offset));
+ lk_properties_set_property_hash(props, "rid", rid);
+
+ if (type == TYPE_OGG)
+ got=get_ogg_props(props, filename);
+ else if (type == TYPE_FLAC)
+ got=get_flac_props(props, filename);
+ else if (type == TYPE_MP3)
+ got=get_mp3_props(props, &mp3);
+ else if (type == TYPE_WAV)
+ got=get_wave_props(props, filename);
+ /*
+ else if (type == TYPE_WMA)
+ got=get_wma_props(props, filename);
+ */
+ else
+ got=get_taxi_props(props);
+
if(got==0){
fd=open(filename, O_RDONLY);
while((got=read(fd, &tmp, siz)) > 0){
@@ -203,10 +222,13 @@
}
}
-static void add_general(HASH * p, const char * file, unsigned int len)
+static void add_general(HASH * p, const char *filename)
{
time_t generation_time;
char *t;
+ struct stat len;
+
+ lstat(filename, &len);
generation_time=time(NULL);
lk_properties_set_property_hash(p, "profile", "");
@@ -214,33 +236,24 @@
simple_itoa(generation_time));
lk_properties_set_property_hash(p, "ctime",
simple_itoa(generation_time));
- lk_properties_set_property_hash(p, "length", simple_itoa(len));
- t=strrchr(file, '/');
- t=utf8_to_codeset((char *)(t?t+1:file));
+ lk_properties_set_property_hash(p, "length", simple_itoa(len.st_size));
+ t=strrchr(filename, '/');
+ t=utf8_to_codeset((char *)(t?t+1:filename));
lk_properties_set_property_hash(p, "title", t);
free(t);
}
-static int get_ogg_props(HASH *p, const char * oggfile, unsigned int len)
-{
- int rid_fd;
-
+static int get_ogg_props(HASH *p, const char *filename)
+{
lk_properties_set_property_hash(p, "codec", "vorbis");
lk_properties_set_property_hash(p, "type", "tune");
- rid_fd=open(oggfile, O_RDONLY);
- lk_properties_set_property_hash(p, "rid",
- (char*)lk_generate_rid(rid_fd, 0, len));
- close(rid_fd);
-
- set_tag_props(p, oggfile);
- return 0;
-}
-
-static int get_flac_props(HASH *p, const char * flacfile, unsigned int len)
-{
- int rid_fd;
-
+ set_tag_props(p, filename);
+ return 0;
+}
+
+static int get_flac_props(HASH *p, const char *filename)
+{
/*default values*/
lk_properties_set_property_hash(p, "source", NOT_TAGGED);
lk_properties_set_property_hash(p, "artist", NOT_TAGGED);
@@ -248,12 +261,69 @@
lk_properties_set_property_hash(p, "codec", "flac");
lk_properties_set_property_hash(p, "type", "tune");
- rid_fd=open(flacfile, O_RDONLY);
- lk_properties_set_property_hash(p, "rid",
- (char*)lk_generate_rid(rid_fd, 0, len));
- close(rid_fd);
-
- set_tag_props(p, flacfile);
+ set_tag_props(p, filename);
+ return 0;
+}
+
+static int get_mp3_props(HASH *p, mp3info *mp3)
+{
+ char bitrate[5];
+
+ lk_properties_set_property_hash(p, "type", "tune");
+ lk_properties_set_property_hash(p, "codec", "mp3");
+ /*default values*/
+ lk_properties_set_property_hash(p, "artist", NOT_TAGGED);
+ lk_properties_set_property_hash(p, "source", NOT_TAGGED);
+
+ set_tag_props(p, mp3->filename);
+
+ if(!mp3->vbr){
+ strcpy(bitrate, "fs");
+ strcat(bitrate, simple_itoa(header_bitrate(&mp3->header)));
+ lk_properties_set_property_hash(p, "bitrate", bitrate);
+ }
+
+ return 0;
+}
+
+static int get_taxi_props(HASH *p)
+{
+ lk_properties_set_property_hash(p, "duration", "0");
+ lk_properties_set_property_hash(p, "offset", "0");
+ lk_properties_set_property_hash(p, "bitrate", "fs128");
+ lk_properties_set_property_hash(p, "codec", "taxi");
+ lk_properties_set_property_hash(p, "type", "taxi");
+
+ return 0;
+}
+/*
+int get_wma_props(HASH *p, const char *filename)
+{
+ return 0;
+}
+*/
+static int get_wave_props(HASH *p, const char *filename)
+{
+ wave_header *wh;
+
+ if(openwav(&wh, (char*)filename) == -1){
+ cleanup(&wh);
+ return 1;
+ }
+ lk_properties_set_property_hash(p, "codec", "wave");
+ lk_properties_set_property_hash(p, "type", "tune");
+ lk_properties_set_property_hash(p, "bitrate", "fs999");
+ lk_properties_set_property_hash(p, "source", "wave_file");
+ lk_properties_set_property_hash(p, "artist", "wave_file");
+ lk_properties_set_property_hash(p, "samplerate",
+ simple_itoa(wh->nSamplesPerSec));
+ lk_properties_set_property_hash(p, "duration", simple_itoa(1000 *
+ wh->RiffSize/wh->nChannels/
+ (wh->nChannels * wh->nSamplesPerSec)));
+ lk_properties_set_property_hash(p, "file_id", "0");
+ lk_properties_set_property_hash(p, "tracknr", "0");
+
+ cleanup(&wh);
return 0;
}
@@ -312,88 +382,97 @@
/*taglib_tag_free_strings();*/
}
-static int get_mp3_props(HASH *p, const char * mp3file)
-{
- mp3info mp3;
- char bitrate[5];
- int rid_fd;
-
- lk_properties_set_property_hash(p, "type", "tune");
- lk_properties_set_property_hash(p, "codec", "mp3");
- /*default values*/
- lk_properties_set_property_hash(p, "artist", NOT_TAGGED);
- lk_properties_set_property_hash(p, "source", NOT_TAGGED);
-
- memset(&mp3,0,sizeof(mp3info));
-
- mp3.filename=(char *)mp3file;
- mp3.file=fopen(mp3file, "r");
- if(mp3.file==NULL) return -1;
- get_mp3_info(&mp3);
- fclose(mp3.file);
-
- lk_properties_set_property_hash(p, "offset", simple_itoa(mp3.offset));
- rid_fd=open(mp3file, O_RDONLY);
- lk_properties_set_property_hash(p, "rid", (char *)lk_generate_rid(rid_fd,
- mp3.offset, mp3.datasize));
- close(rid_fd);
-
- set_tag_props(p, mp3file);
-
- if(!mp3.vbr){
- strcpy(bitrate, "fs");
- strcat(bitrate, simple_itoa(header_bitrate(&mp3.header)));
- lk_properties_set_property_hash(p, "bitrate", bitrate);
- }
-
- return 0;
-}
-
-static int get_taxi_props(HASH *p)
-{
- lk_properties_set_property_hash(p, "duration", "0");
- lk_properties_set_property_hash(p, "offset", "0");
- lk_properties_set_property_hash(p, "bitrate", "fs128");
- lk_properties_set_property_hash(p, "codec", "taxi");
- lk_properties_set_property_hash(p, "type", "taxi");
-
- return 0;
-}
-/*
-int get_wma_props(HASH *p, const char * wmafile)
-{
- return 0;
-}
-*/
-static int get_wave_props(HASH *p, const char * wavefile)
-{
- int rid_fd;
- wave_header *wh;
-
- if(openwav(&wh, (char*)wavefile) == -1){
+static int get_file_type(mp3info *mp3)
+{
+ int ret, type = TYPE_DATA;
+ unsigned int i, off;
+ unsigned char buf[48];
+ struct stat st;
+
+ ret = stat(mp3->filename, &st);
+ if (ret != 0)
+ return -1;
+
+ mp3->datasize = st.st_size;
+
+ mp3->file = fopen(mp3->filename, "r");
+ if (mp3->file == NULL)
+ return -1;
+
+ if (mp3->datasize >= 128) {
+ if (fseek(mp3->file, -128, SEEK_END )) {
+ fprintf(stderr, "ERROR: Couldn't read last 128 bytes of %s.\n",
+ mp3->filename);
+ return -1;
+ } else {
+ fread(buf, 1, 3, mp3->file);
+ if (memcmp(buf, "TAG", 3) == 0)
+ mp3->datasize -= 128;
+ }
+ fseek(mp3->file, 0, SEEK_SET);
+ }
+
+ fread(buf, 1, 48, mp3->file);
+ if (memcmp(buf, "ID3", 3) == 0) {
+ mp3->offset = (((buf[6]&0x7f)<<21)|((buf[7]&0x7f)<<14)|
+ ((buf[8]&0x7f)<<7) |(buf[9]&0x7f)) + 10;
+ fseek(mp3->file, mp3->offset, SEEK_SET);
+ fread(buf, 1, 48, mp3->file);
+ }
+
+ if (memcmp(buf, "OggS", 4) == 0) {
+ mp3->offset += buf[27] + 28;
+ if (memcmp(buf+29, "FLAC", 4) == 0) {
+ type = TYPE_FLAC;
+ fseek(mp3->file, mp3->offset, SEEK_SET);
+ fread(buf, 1, 48, mp3->file);
+ off = buf[26] + 27;
+ while ((buf[off]&0x80) == 0) {
+ mp3->offset += off + buf[27];
+ fseek(mp3->file, mp3->offset, SEEK_SET);
+ fread(buf, 1, 48, mp3->file);
+ off = buf[26] + 27;
+ }
+ mp3->offset += off;
+ off = 0;
+ for (i=0; i<buf[26]; i++)
+ off += buf[27+i];
+ mp3->offset += off;
+ } else if (memcmp(buf+29, "vorbis", 6) == 0) {
+ type = TYPE_OGG;
+ fseek(mp3->file, mp3->offset, SEEK_SET);
+ fread(buf, 1, 48, mp3->file);
+ mp3->offset += buf[26] + buf[27] + 28;
+ }
+ } else if (memcmp(buf, "fLaC", 4) == 0) {
+ type = TYPE_FLAC;
+ mp3->offset += 4;
+ memcpy(buf, buf+4, 4);
+ while ((buf[0]&0x80) == 0) {
+ mp3->offset += (((buf[1]&0xff)<<16)|
+ ((buf[2]&0xff)<<8) |(buf[3]&0xff)) + 4;
+ fseek(mp3->file, mp3->offset, SEEK_SET);
+ fread(buf, 1, 4, mp3->file);
+ }
+ mp3->offset += (((buf[1]&0xff)<<16)|
+ ((buf[2]&0xff)<<8) |(buf[3]&0xff)) + 4;
+ } else if (memcmp(buf, "RIFF", 4) == 0) {
+ wave_header *wh;
+
+ if (openwav(&wh, mp3->filename) == -1){
+ cleanup(&wh);
+ return -1;
+ }
+ type = TYPE_WAV;
+ mp3->offset = wh->RiffSize/1024;
cleanup(&wh);
- return 1;
- }
- lk_properties_set_property_hash(p, "codec", "wave");
- lk_properties_set_property_hash(p, "type", "tune");
- lk_properties_set_property_hash(p, "bitrate", "fs999");
- lk_properties_set_property_hash(p, "source", "wave_file");
- lk_properties_set_property_hash(p, "artist", "wave_file");
- lk_properties_set_property_hash(p, "offset",
- simple_itoa(sizeof(wave_header)));
- lk_properties_set_property_hash(p, "samplerate",
- simple_itoa(wh->nSamplesPerSec));
- lk_properties_set_property_hash(p, "duration", simple_itoa(1000 *
- wh->RiffSize/wh->nChannels/
- (wh->nChannels * wh->nSamplesPerSec)));
- lk_properties_set_property_hash(p, "file_id", "0");
- lk_properties_set_property_hash(p, "tracknr", "0");
-
- rid_fd=open(wavefile, O_RDONLY);
- lk_properties_set_property_hash(p, "rid", (char*)lk_generate_rid(rid_fd,
- 0, wh->RiffSize/1024));
- close(rid_fd);
-
- cleanup(&wh);
- return 0;
-}
+ } else {
+ fseek(mp3->file, 0, SEEK_SET);
+ get_mp3_info(mp3);
+ if (mp3->header_isvalid)
+ type = TYPE_MP3;
+ }
+ fclose(mp3->file);
+
+ return type;
+}
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
linux-karma-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-karma-devel