Commit 860f8bda authored by Warren Dukes's avatar Warren Dukes

ok, rework myfprintf so it uses write() and never use any file stream

print functions. this way we can always know wtf is going on! also, remove some places where we were using fprintf and printf instead of myfprintf git-svn-id: https://svn.musicpd.org/mpd/trunk@734 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 5a50fa71
......@@ -4,10 +4,6 @@
quit in the middle of an update
k) when bg-update, have parent print out new old stuff to log on
reading db, not the child
l) IMPORTANT: look over new command list shiznit and make it much
cleaner, please! (in particular, move buffer2array code from
interface.c to command.c, and have command stuff
do buffer2array)
m) MOST IMPORTANT: update needs to deal better with directories/files
that don't exist in the db, but do exit in the fs (i.e.
calling updated on a newly created file).
......
......@@ -553,7 +553,6 @@ int printDirectoryInfo(FILE * fp, char * name) {
return 0;
}
/* DON'T BLOCK SIGNALS IN THIS FUNCTION, called by writeDirectoryDB() */
void writeDirectoryInfo(FILE * fp, Directory * directory) {
ListNode * node = (directory->subDirectories)->firstNode;
Directory * subDirectory;
......@@ -685,17 +684,12 @@ int writeDirectoryDB() {
while(!(fp=fopen(directorydb,"w")) && errno==EINTR);
if(!fp) return -1;
/* block signals so we ensure the db doesn't get corrupted */
/* no functions that writeDirectoryInfoCalls should mess with
signals or signal blocking! */
blockSignals();
myfprintf(fp,"%s\n",DIRECTORY_INFO_BEGIN);
myfprintf(fp,"%s%s\n",DIRECTORY_MPD_VERSION,VERSION);
myfprintf(fp,"%s%s\n",DIRECTORY_FS_CHARSET,getFsCharset());
myfprintf(fp,"%s\n",DIRECTORY_INFO_END);
writeDirectoryInfo(fp,mp3rootDirectory);
unblockSignals();
while(fclose(fp) && errno==EINTR);
......
......@@ -84,14 +84,13 @@ void openInterface(Interface * interface, int fd) {
assert(interface->open==0);
blockSignals();
interface->bufferLength = 0;
interface->fd = fd;
/* fcntl(interface->fd,F_SETOWN,(int)getpid()); */
flags = fcntl(fd,F_GETFL);
while((flags = fcntl(fd,F_GETFL))<0 && errno==EINTR);
flags|=O_NONBLOCK;
fcntl(interface->fd,F_SETFL,flags);
interface->fp = fdopen(fd,"rw");
while(fcntl(interface->fd,F_SETFL,flags)<0 && errno==EINTR);
while((interface->fp = fdopen(fd,"rw"))==NULL && errno==EINTR);
interface->open = 1;
interface->lastTime = time(NULL);
interface->commandList = NULL;
......@@ -121,8 +120,6 @@ void openInterface(Interface * interface, int fd) {
#endif
interface->outBuffer = malloc(interface->outBufSize);
unblockSignals();
myfprintf(interface->fp,"%s %s %s\n",COMMAND_RESPOND_OK,GREETING,
VERSION);
printInterfaceOutBuffer(interface);
......
......@@ -33,6 +33,21 @@ int myfprintf_stdLogMode = 0;
FILE * myfprintf_out;
FILE * myfprintf_err;
void blockingWrite(int fd, char * string) {
int len = strlen(string);
int ret;
while(len) {
ret = write(fd,string,len);
if(ret<0) {
if(errno==EAGAIN || errno==EINTR) continue;
return;
}
len-= ret;
string+= ret;
}
}
void myfprintfStdLogMode(FILE * out, FILE * err) {
myfprintf_stdLogMode = 1;
myfprintf_out = out;
......@@ -40,31 +55,33 @@ void myfprintfStdLogMode(FILE * out, FILE * err) {
}
void myfprintf(FILE * fp, char * format, ... ) {
char buffer[BUFFER_LENGTH+1];
va_list arglist;
int fd = fileno(fp);
int fcntlret;
memset(buffer,0,BUFFER_LENGTH+1);
va_start(arglist,format);
while((fcntlret=fcntl(fd,F_GETFL))==-1 && errno==EINTR);
if(myfprintf_stdLogMode && (fd==1 || fd==2)) {
char str[15];
time_t t = time(NULL);
if(fd==1) fp = myfprintf_out;
else fp = myfprintf_err;
strftime(str,14,"%b %e %R",localtime(&t));
fprintf(fp,"%s : ",str);
vfprintf(fp,format,arglist);
strftime(buffer,14,"%b %e %R",localtime(&t));
blockingWrite(fd,buffer);
blockingWrite(fd," : ");
vsnprintf(buffer,BUFFER_LENGTH,format,arglist);
blockingWrite(fd,buffer);
}
else if(fcntlret & O_NONBLOCK) {
char buffer[BUFFER_LENGTH+1];
else {
vsnprintf(buffer,BUFFER_LENGTH,format,arglist);
if(interfacePrintWithFD(fd,buffer)<0) {
/* not a fd from a interface */
vfprintf(fp,format,arglist);
if((fcntlret & O_NONBLOCK) &&
interfacePrintWithFD(fd,buffer)==0)
{
}
else blockingWrite(fd,buffer);
}
else vfprintf(fp,format,arglist);
fflush(fp);
va_end(arglist);
}
......@@ -397,7 +397,7 @@ int playerSeek(FILE * fp, char * utf8file, float time) {
if(strcmp(pc->file,file)!=0) {
decodeType = playerGetDecodeType(utf8file);
if(decodeType < 0) {
printf("%s unknown file type: %s\n",
myfprintf(fp,"%s unknown file type: %s\n",
COMMAND_RESPOND_ERROR, utf8file);
return -1;
}
......
......@@ -501,12 +501,12 @@ int swapSongsInPlaylist(FILE * fp, int song1, int song2) {
int currentSong = -1;
if(song1<0 || song1>=playlist.length) {
fprintf(fp,"%s \"%i\" is not in the playlist\n",
myfprintf(fp,"%s \"%i\" is not in the playlist\n",
COMMAND_RESPOND_ERROR,song1);
return -1;
}
if(song2<0 || song2>=playlist.length) {
fprintf(fp,"%s \"%i\" is not in the playlist\n",
myfprintf(fp,"%s \"%i\" is not in the playlist\n",
COMMAND_RESPOND_ERROR,song2);
return -1;
}
......@@ -796,13 +796,13 @@ int moveSongInPlaylist(FILE * fp, int from, int to) {
int currentSong = -1;
if(from<0 || from>=playlist.length) {
fprintf(fp,"%s \"%i\" is not a song in the playlist\n",
myfprintf(fp,"%s \"%i\" is not a song in the playlist\n",
COMMAND_RESPOND_ERROR,from);
return -1;
}
if(to<0 || to>=playlist.length) {
fprintf(fp,"%s \"%i\" is not a song in the playlist\n",
myfprintf(fp,"%s \"%i\" is not a song in the playlist\n",
COMMAND_RESPOND_ERROR,to);
return -1;
}
......
......@@ -132,7 +132,6 @@ void freeSongList(SongList * list) {
freeList(list);
}
/* DON'T BLOCK SIGNALS IN THIS FUNCTION, called by writeDirectoryDB() */
int printSongInfo(FILE * fp, Song * song) {
myfprintf(fp,"%s%s\n",SONG_FILE,song->utf8file);
......@@ -152,7 +151,6 @@ int printSongInfoFromList(FILE * fp, SongList * list) {
return 0;
}
/* DON'T BLOCK SIGNALS IN THIS FUNCTION, called by writeDirectoryDB() */
void writeSongInfoFromList(FILE * fp, SongList * list) {
ListNode * tempNode = list->firstNode;
......
......@@ -51,7 +51,6 @@
#include "mp4ff/mp4ff.h"
#endif
/* DON'T BLOCK SIGNALS IN THIS FUNCTION, called by writeDirectoryDB() */
void printMpdTag(FILE * fp, MpdTag * tag) {
if(tag->artist) myfprintf(fp,"Artist: %s\n",tag->artist);
if(tag->album) myfprintf(fp,"Album: %s\n",tag->album);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment