+/*
+ * This wad of functions supports filename- and callback-based chain reading/writing.
+ * Everything up to set_file_stats_() is copied from libFLAC/metadata_iterators.c
+ */
+bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
+{
+ static const char *tempfile_suffix = ".metadata_edit";
+
+ if(0 == (*tempfilename = (char*)malloc(strlen(filename) + strlen(tempfile_suffix) + 1)))
+ return false;
+ strcpy(*tempfilename, filename);
+ strcat(*tempfilename, tempfile_suffix);
+
+ if(0 == (*tempfile = fopen(*tempfilename, "wb")))
+ return false;
+
+ return true;
+}
+
+void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
+{
+ if(0 != *tempfile) {
+ (void)fclose(*tempfile);
+ *tempfile = 0;
+ }
+
+ if(0 != *tempfilename) {
+ (void)unlink(*tempfilename);
+ free(*tempfilename);
+ *tempfilename = 0;
+ }
+}
+
+bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
+{
+ FLAC__ASSERT(0 != filename);
+ FLAC__ASSERT(0 != tempfile);
+ FLAC__ASSERT(0 != tempfilename);
+ FLAC__ASSERT(0 != *tempfilename);
+
+ if(0 != *tempfile) {
+ (void)fclose(*tempfile);
+ *tempfile = 0;
+ }
+
+#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
+ /* on some flavors of windows, rename() will fail if the destination already exists */
+ if(unlink(filename) < 0) {
+ cleanup_tempfile_(tempfile, tempfilename);
+ return false;
+ }
+#endif
+
+ if(0 != rename(*tempfilename, filename)) {
+ cleanup_tempfile_(tempfile, tempfilename);
+ return false;
+ }
+
+ cleanup_tempfile_(tempfile, tempfilename);
+
+ return true;
+}
+
+bool get_file_stats_(const char *filename, struct stat *stats)
+{
+ FLAC__ASSERT(0 != filename);
+ FLAC__ASSERT(0 != stats);
+ return (0 == stat(filename, stats));
+}
+
+void set_file_stats_(const char *filename, struct stat *stats)
+{
+ struct utimbuf srctime;
+
+ FLAC__ASSERT(0 != filename);
+ FLAC__ASSERT(0 != stats);
+
+ srctime.actime = stats->st_atime;
+ srctime.modtime = stats->st_mtime;
+ (void)chmod(filename, stats->st_mode);
+ (void)utime(filename, &srctime);
+#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
+ FLAC_CHECK_RETURN(chown(filename, stats->st_uid, (gid_t)(-1)));
+ FLAC_CHECK_RETURN(chown(filename, (uid_t)(-1), stats->st_gid));
+#endif
+}
+
+#ifdef FLAC__VALGRIND_TESTING
+static size_t chain_write_cb_(const void *ptr, size_t size, size_t nmemb, ::FLAC__IOHandle handle)
+{
+ FILE *stream = (FILE*)handle;
+ size_t ret = fwrite(ptr, size, nmemb, stream);
+ if(!ferror(stream))
+ fflush(stream);
+ return ret;
+}
+#endif
+
+static int chain_seek_cb_(::FLAC__IOHandle handle, FLAC__int64 offset, int whence)
+{
+ FLAC__off_t o = (FLAC__off_t)offset;
+ FLAC__ASSERT(offset == o);
+ return fseeko((FILE*)handle, o, whence);
+}
+
+static FLAC__int64 chain_tell_cb_(::FLAC__IOHandle handle)
+{
+ return ftello((FILE*)handle);
+}
+
+static int chain_eof_cb_(::FLAC__IOHandle handle)
+{
+ return feof((FILE*)handle);
+}
+
+static bool write_chain_(FLAC::Metadata::Chain &chain, bool use_padding, bool preserve_file_stats, bool filename_based, const char *filename)
+{
+ if(filename_based)
+ return chain.write(use_padding, preserve_file_stats);
+ else {
+ ::FLAC__IOCallbacks callbacks;
+
+ memset(&callbacks, 0, sizeof(callbacks));
+ callbacks.read = (::FLAC__IOCallback_Read)fread;
+#ifdef FLAC__VALGRIND_TESTING
+ callbacks.write = chain_write_cb_;
+#else
+ callbacks.write = (::FLAC__IOCallback_Write)fwrite;
+#endif
+ callbacks.seek = chain_seek_cb_;
+ callbacks.eof = chain_eof_cb_;
+
+ if(chain.check_if_tempfile_needed(use_padding)) {
+ struct stat stats;
+ FILE *file, *tempfile;
+ char *tempfilename;
+ if(preserve_file_stats) {
+ if(!get_file_stats_(filename, &stats))
+ return false;
+ }
+ if(0 == (file = fopen(filename, "rb")))
+ return false; /*@@@@ chain status still says OK though */
+ if(!open_tempfile_(filename, &tempfile, &tempfilename)) {
+ fclose(file);
+ cleanup_tempfile_(&tempfile, &tempfilename);
+ return false; /*@@@@ chain status still says OK though */
+ }
+ if(!chain.write(use_padding, (::FLAC__IOHandle)file, callbacks, (::FLAC__IOHandle)tempfile, callbacks)) {
+ fclose(file);
+ fclose(tempfile);
+ return false;
+ }
+ fclose(file);
+ fclose(tempfile);
+ file = tempfile = 0;
+ if(!transport_tempfile_(filename, &tempfile, &tempfilename))
+ return false;
+ if(preserve_file_stats)
+ set_file_stats_(filename, &stats);
+ }
+ else {
+ FILE *file = fopen(filename, "r+b");
+ if(0 == file)
+ return false; /*@@@@ chain status still says OK though */
+ if(!chain.write(use_padding, (::FLAC__IOHandle)file, callbacks)) {
+ fclose(file);
+ return false;
+ }
+ fclose(file);
+ }
+ }
+
+ return true;
+}
+
+static bool read_chain_(FLAC::Metadata::Chain &chain, const char *filename, bool filename_based, bool is_ogg)
+{
+ if(filename_based)
+ return chain.read(filename, is_ogg);
+ else {
+ ::FLAC__IOCallbacks callbacks;
+
+ memset(&callbacks, 0, sizeof(callbacks));
+ callbacks.read = (::FLAC__IOCallback_Read)fread;
+ callbacks.seek = chain_seek_cb_;
+ callbacks.tell = chain_tell_cb_;
+
+ {
+ bool ret;
+ FILE *file = fopen(filename, "rb");
+ if(0 == file)
+ return false; /*@@@@ chain status still says OK though */
+ ret = chain.read((::FLAC__IOHandle)file, callbacks, is_ogg);
+ fclose(file);
+ return ret;
+ }
+ }
+}
+