gboolean write_and_pivot_files (vcedit_state * state) { gchar * temp; GError * error; gint handle = g_file_open_tmp (NULL, & temp, & error); if (handle < 0) { fprintf (stderr, "Failed to create temp file: %s.\n", error->message); g_error_free (error); return FALSE; } close (handle); gchar * temp_uri = filename_to_uri (temp); g_return_val_if_fail (temp_uri, FALSE); VFSFile * temp_vfs = vfs_fopen (temp_uri, "r+"); g_return_val_if_fail (temp_vfs, FALSE); str_unref (temp_uri); if (vcedit_write (state, temp_vfs) < 0) { fprintf (stderr, "Tag update failed: %s.\n", state->lasterror); vfs_fclose (temp_vfs); g_free (temp); return FALSE; } if (! copy_vfs (temp_vfs, state->in)) { fprintf (stderr, "Failed to copy temp file. The temp file has not " "been deleted: %s.\n", temp); vfs_fclose (temp_vfs); g_free (temp); return FALSE; } vfs_fclose (temp_vfs); if (g_unlink (temp) < 0) fprintf (stderr, "Failed to delete temp file: %s.\n", temp); g_free (temp); return TRUE; }
static gint close_files(vcedit_state *state) { int retval = 0, ofh; char *tmpfn; FILE* out; tmpfn = g_strdup_printf("%s.XXXXXX", vte.filename); if ((ofh = mkstemp(tmpfn)) < 0) { g_free(tmpfn); fclose(vte.in); return -1; } if ((out = fdopen(ofh, "wb")) == NULL) { close(ofh); remove(tmpfn); g_free(tmpfn); fclose(vte.in); return -1; } if (vcedit_write(state, out) < 0) { g_warning("vcedit_write: %s", state->lasterror); retval = -1; } fclose(vte.in); if (fclose(out) != 0) retval = -1; if (retval < 0 || rename(tmpfn, vte.filename) < 0) { remove(tmpfn); retval = -1; } g_free(tmpfn); return retval; }
int main(int argc, char **argv) { vcedit_state *state; vorbis_comment *vc; param_t *param; int i; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); /* initialize the cmdline interface */ param = new_param(); parse_options(argc, argv, param); /* take care of opening the requested files */ /* relevent file pointers are returned in the param struct */ open_files(param); /* which mode are we in? */ if (param->mode == MODE_LIST) { state = vcedit_new_state(); if(vcedit_open(state, param->in) < 0) { fprintf(stderr, _("Failed to open file as vorbis: %s\n"), vcedit_error(state)); close_files(param); free_param(param); vcedit_clear(state); return 1; } /* extract and display the comments */ vc = vcedit_comments(state); print_comments(param->com, vc, param->raw); /* done */ vcedit_clear(state); close_files(param); free_param(param); return 0; } if (param->mode == MODE_WRITE || param->mode == MODE_APPEND) { state = vcedit_new_state(); if(vcedit_open(state, param->in) < 0) { fprintf(stderr, _("Failed to open file as vorbis: %s\n"), vcedit_error(state)); close_files(param); free_param(param); vcedit_clear(state); return 1; } /* grab and clear the exisiting comments */ vc = vcedit_comments(state); if(param->mode != MODE_APPEND) { vorbis_comment_clear(vc); vorbis_comment_init(vc); } for(i=0; i < param->commentcount; i++) { if(add_comment(param->comments[i], vc, param->raw) < 0) fprintf(stderr, _("Bad comment: \"%s\"\n"), param->comments[i]); } /* build the replacement structure */ if(param->commentcount==0) { /* FIXME should use a resizeable buffer! */ char *buf = (char *)malloc(sizeof(char)*1024); while (fgets(buf, 1024, param->com)) if (add_comment(buf, vc, param->raw) < 0) { fprintf(stderr, _("bad comment: \"%s\"\n"), buf); } free(buf); } /* write out the modified stream */ if(vcedit_write(state, param->out) < 0) { fprintf(stderr, _("Failed to write comments to output file: %s\n"), vcedit_error(state)); close_files(param); free_param(param); vcedit_clear(state); return 1; } /* done */ vcedit_clear(state); close_files(param); free_param(param); return 0; } /* should never reach this point */ fprintf(stderr, _("no action specified\n")); free_param(param); return 1; }
int write_ogg_tag(const string fname, struct fennec_audiotag *wtag) { vcedit_state *state; vorbis_comment *vc; param_t *param; FILE *tfile; FILE *of; string outname; struct fennec_audiotag_item *ct; if(!fname || !wtag)return -3; setlocale(LC_ALL, ""); /* initialize the cmdline interface */ param = new_param(); tfile = _wfsopen(fname, uni("r+b"), _SH_DENYRW); if(!tfile) { MessageBox(0, uni("Access denied, please stop playback and try again (you don't need to close this window)."), uni("Tag Editing"), MB_ICONINFORMATION); return -1; } state = vcedit_new_state(); if(vcedit_open(state, tfile) < 0) { fclose(tfile); free_param(param); vcedit_clear(state); return -2; } vc = vcedit_comments(state); ct = &wtag->tag_title; if(ct->tsize)local_addcomment(vc, "TITLE", ct->tdata); else local_addcomment(vc, "TITLE", uni("") ); ct = &wtag->tag_album; if(ct->tsize)local_addcomment(vc, "ALBUM", ct->tdata); else local_addcomment(vc, "ALBUM", uni("") ); ct = &wtag->tag_artist; if(ct->tsize)local_addcomment(vc, "ARTIST", ct->tdata); else local_addcomment(vc, "ARTIST", uni("") ); ct = &wtag->tag_origartist; if(ct->tsize)local_addcomment(vc, "ORIGINALARTIST", ct->tdata); else local_addcomment(vc, "ORIGINALARTIST", uni("") ); ct = &wtag->tag_composer; if(ct->tsize)local_addcomment(vc, "COMPOSER", ct->tdata); else local_addcomment(vc, "COMPOSER", uni("") ); ct = &wtag->tag_lyricist; if(ct->tsize)local_addcomment(vc, "LYRICIST", ct->tdata); else local_addcomment(vc, "LYRICIST", uni("") ); ct = &wtag->tag_band; if(ct->tsize)local_addcomment(vc, "BANDNAME", ct->tdata); else local_addcomment(vc, "BANDNAME", uni("") ); ct = &wtag->tag_copyright; if(ct->tsize)local_addcomment(vc, "COPYRIGHT", ct->tdata); else local_addcomment(vc, "COPYRIGHT", uni("") ); ct = &wtag->tag_publish; if(ct->tsize)local_addcomment(vc, "PUBLISHER", ct->tdata); else local_addcomment(vc, "PUBLISHER", uni("") ); ct = &wtag->tag_encodedby; if(ct->tsize)local_addcomment(vc, "ENCODEDBY", ct->tdata); else local_addcomment(vc, "ENCODEDBY", uni("") ); ct = &wtag->tag_genre; if(ct->tsize)local_addcomment(vc, "GENRE", ct->tdata); else local_addcomment(vc, "GENRE", uni("") ); ct = &wtag->tag_year; if(ct->tsize)local_addcomment(vc, "YEAR", ct->tdata); else local_addcomment(vc, "YEAR", uni("") ); ct = &wtag->tag_url; if(ct->tsize)local_addcomment(vc, "URL", ct->tdata); else local_addcomment(vc, "URL", uni("") ); ct = &wtag->tag_offiartisturl; if(ct->tsize)local_addcomment(vc, "OFFICIALARTISTURL", ct->tdata); else local_addcomment(vc, "OFFICIALARTISTURL", uni("") ); ct = &wtag->tag_comments; if(ct->tsize)local_addcomment(vc, "COMMENT", ct->tdata); else local_addcomment(vc, "COMMENT", uni("") ); ct = &wtag->tag_lyric; if(ct->tsize)local_addcomment(vc, "LYRIC", ct->tdata); else local_addcomment(vc, "LYRIC", uni("") ); ct = &wtag->tag_bpm; if(ct->tsize)local_addcomment(vc, "BPM", ct->tdata); else local_addcomment(vc, "BPM", uni("") ); ct = &wtag->tag_tracknum; if(ct->tsize)local_addcomment(vc, "TRACKNUMBER", ct->tdata); else local_addcomment(vc, "TRACKNUMBER", uni("") ); outname = (string) malloc(str_size(fname) + (5 * sizeof(letter))); str_cpy(outname, fname); str_cat(outname, uni(".tmp")); of = _wfopen(outname, uni("wb")); if(vcedit_write(state, of) < 0) { fclose(of); fclose(tfile); free_param(param); vcedit_clear(state); free(outname); return 1; } fclose(of); /* done */ vcedit_clear(state); fclose(tfile); free_param(param); _wremove(fname); sys_sleep(0); _wrename(outname, fname); free(outname); return 0; }
/* * Write tag information to a new temporary file, and rename it the the initial name. */ gboolean Ogg_Tag_Write_File (FILE *file_in, gchar *filename_in, vcedit_state *state) { gchar *filename_out; gint file_mkstemp; FILE *file_out; gboolean return_code = TRUE; filename_out = g_strdup_printf("%s.XXXXXX",filename_in); // Function mkstemp() opens also the file! if ((file_mkstemp = mkstemp(filename_out)) < 0) { fclose(file_in); close(file_mkstemp); g_free(filename_out); return FALSE; } close(file_mkstemp); if ( (file_out=fopen(filename_out,"wb")) == NULL ) { fclose(file_in); remove(filename_out); g_free(filename_out); return FALSE; } if (vcedit_write(state,file_out) < 0) { fclose(file_in); fclose(file_out); remove(filename_out); g_free(filename_out); return FALSE; } fclose(file_in); fclose(file_out); // Some platforms fail to rename a file if the new name already // exists, so we need to remove, then rename... if (rename(filename_out,filename_in)) { // Can't rename directly if (remove(filename_in)) { // Can't remove file remove(filename_out); return_code = FALSE; }else if (rename(filename_out,filename_in)) { // Can't rename after removing file remove(filename_out); return_code = FALSE; } } g_free(filename_out); return return_code; }