Fix fd leak from bad files in mp3 scan

This commit is contained in:
Ron Pedde 2004-11-18 06:56:04 +00:00
parent 2fa6a01019
commit 5280856f95
2 changed files with 35 additions and 21 deletions

View File

@ -149,6 +149,8 @@ static int gdbm_mp3_mustfree=0; /**< is the data in #gdbm_mp3 valid? Should it
/* /*
* Forwards * Forwards
*/ */
static void db_writelock(void);
static void db_readlock(void);
int db_start_initial_update(void); int db_start_initial_update(void);
int db_end_initial_update(void); int db_end_initial_update(void);
@ -413,9 +415,6 @@ int db_start_initial_update(void) {
int db_end_initial_update(void) { int db_end_initial_update(void) {
const void *val; const void *val;
db_writelock();
db_update_mode=0;
DPRINTF(E_DBG,L_DB|L_SCAN,"Initial update over. Removing stale items\n"); DPRINTF(E_DBG,L_DB|L_SCAN,"Initial update over. Removing stale items\n");
for(val=rblookup(RB_LUFIRST,NULL,db_removed); val != NULL; for(val=rblookup(RB_LUFIRST,NULL,db_removed); val != NULL;
val=rblookup(RB_LUNEXT,val,db_removed)) { val=rblookup(RB_LUNEXT,val,db_removed)) {
@ -427,9 +426,12 @@ int db_end_initial_update(void) {
rbdestroy(db_removed); rbdestroy(db_removed);
DPRINTF(E_DBG,L_DB,"Reorganizing db\n"); DPRINTF(E_DBG,L_DB,"Reorganizing db\n");
db_writelock();
gdbm_reorganize(db_songs); gdbm_reorganize(db_songs);
gdbm_sync(db_songs); gdbm_sync(db_songs);
DPRINTF(E_DBG,L_DB,"Reorganize done\n"); DPRINTF(E_DBG,L_DB,"Reorganize done\n");
db_update_mode=0;
db_unlock(); db_unlock();
return 0; return 0;
@ -1176,7 +1178,6 @@ int db_playlist_items_enum_end(ENUMHANDLE handle) {
MP3FILE *db_find(int id) { /* FIXME: Not reentrant */ MP3FILE *db_find(int id) { /* FIXME: Not reentrant */
MP3FILE *pmp3=NULL; MP3FILE *pmp3=NULL;
datum key, content; datum key, content;
int is_locked=0;
key.dptr=(char*)&id; key.dptr=(char*)&id;
key.dsize=sizeof(int); key.dsize=sizeof(int);
@ -1198,9 +1199,9 @@ MP3FILE *db_find(int id) { /* FIXME: Not reentrant */
return NULL; return NULL;
} }
db_unlock();
db_unpackrecord(&content,pmp3); db_unpackrecord(&content,pmp3);
free(content.dptr); free(content.dptr);
db_unlock();
return pmp3; return pmp3;
} }
@ -1210,7 +1211,12 @@ MP3FILE *db_find(int id) { /* FIXME: Not reentrant */
* return the number of playlists * return the number of playlists
*/ */
int db_get_playlist_count(void) { int db_get_playlist_count(void) {
return db_playlist_count; int retval;
db_readlock();
retval=db_playlist_count;
db_unlock();
return retval;
} }
/* /*
@ -1220,7 +1226,13 @@ int db_get_playlist_count(void) {
* request * request
*/ */
int db_get_song_count(void) { int db_get_song_count(void) {
return db_song_count; int retval;
db_readlock();
retval=db_song_count;
db_unlock();
return retval;
} }
@ -1280,8 +1292,6 @@ int db_get_playlist_entry_count(int playlistid) {
* db_get_playlist_name * db_get_playlist_name
* *
* return the name of a playlist * return the name of a playlist
*
* FIXME: Small race here
*/ */
char *db_get_playlist_name(int playlistid) { char *db_get_playlist_name(int playlistid) {
char *name; char *name;

View File

@ -340,6 +340,7 @@ int scan_path(char *path) {
while(1) { while(1) {
if(config.stop) { if(config.stop) {
DPRINTF(E_WARN,L_SCAN,"Stop detected. Aborting scan of %s.\n",path); DPRINTF(E_WARN,L_SCAN,"Stop detected. Aborting scan of %s.\n",path);
closedir(current_dir);
return 0; return 0;
} }
@ -1157,6 +1158,7 @@ int scan_get_mp3fileinfo(char *file, MP3FILE *pmp3) {
} else { } else {
DPRINTF(E_LOG,L_SCAN,"Short file: %s\n",file); DPRINTF(E_LOG,L_SCAN,"Short file: %s\n",file);
} }
fclose(infile);
return -1; return -1;
} }
@ -1176,6 +1178,7 @@ int scan_get_mp3fileinfo(char *file, MP3FILE *pmp3) {
fseek(infile,fp_size,SEEK_SET); fseek(infile,fp_size,SEEK_SET);
if(fread(buffer,1,sizeof(buffer),infile) < sizeof(buffer)) { if(fread(buffer,1,sizeof(buffer),infile) < sizeof(buffer)) {
DPRINTF(E_LOG,L_SCAN,"Short file: %s\n",file); DPRINTF(E_LOG,L_SCAN,"Short file: %s\n",file);
fclose(infile);
return -1; return -1;
} }
@ -1224,11 +1227,13 @@ int scan_get_mp3fileinfo(char *file, MP3FILE *pmp3) {
if((layer_index < 0) || (layer_index > 4)) { if((layer_index < 0) || (layer_index > 4)) {
DPRINTF(E_LOG,L_SCAN,"Bad mp3 header in %s: bad layer_index\n",file); DPRINTF(E_LOG,L_SCAN,"Bad mp3 header in %s: bad layer_index\n",file);
fclose(infile);
return -1; return -1;
} }
if((sample_index < 0) || (sample_index > 2)) { if((sample_index < 0) || (sample_index > 2)) {
DPRINTF(E_LOG,L_SCAN,"Bad mp3 header in %s: bad sample_index\n",file); DPRINTF(E_LOG,L_SCAN,"Bad mp3 header in %s: bad sample_index\n",file);
fclose(infile);
return -1; return -1;
} }
@ -1249,12 +1254,11 @@ int scan_get_mp3fileinfo(char *file, MP3FILE *pmp3) {
DPRINTF(E_DBG,L_SCAN," Bit Rate: %d\n",bitrate); DPRINTF(E_DBG,L_SCAN," Bit Rate: %d\n",bitrate);
/* guesstimate the file length */ /* guesstimate the file length */
if(!pmp3->song_length) /* could have gotten it from the tag */ if(!pmp3->song_length) { /* could have gotten it from the tag */
{
/* DWB: use ms time instead of seconds, use doubles to /* DWB: use ms time instead of seconds, use doubles to
avoid overflow */ avoid overflow */
if(bitrate) if(bitrate) {
{
pmp3->song_length = (int) ((double) file_size * 8000. / pmp3->song_length = (int) ((double) file_size * 8000. /
(double) bitrate / (double) bitrate /
1024.); 1024.);