Skip to content

Commit 573da6c

Browse files
committed
Opus files: Close TagLib File object before writing to header gain
1 parent d4676e3 commit 573da6c

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/tag.cpp

+26-21
Original file line numberDiff line numberDiff line change
@@ -446,24 +446,26 @@ static bool tag_flac(ScanJob::Track &track, const Config &config)
446446

447447
template<typename T>
448448
static bool tag_ogg(ScanJob::Track &track, const Config &config) {
449-
T file(track.path.string().c_str());
450-
TagLib::Ogg::XiphComment *tag = nullptr;
451-
if constexpr(std::is_same_v<T, TagLib::FileRef>)
452-
tag = dynamic_cast<TagLib::Ogg::XiphComment*>(file.tag());
453-
else
454-
tag = file.tag();
455-
if (!tag)
456-
return false;
457-
tag_clear<T>(tag);
458-
if (config.tag_mode == 'i' && (!std::is_same_v<T, TagLib::Ogg::Opus::File> ||
459-
(config.opus_mode != 't' && config.opus_mode != 'a')))
460-
tag_write<T>(tag, track.result, config);
461-
462-
bool ret = file.save();
463-
if (!std::is_same_v<T, TagLib::Ogg::Opus::File> || config.tag_mode == 's' ||
464-
!(config.opus_mode == 't' || config.opus_mode == 'a') || !ret)
465-
return ret;
449+
{
450+
T file(track.path.string().c_str());
451+
TagLib::Ogg::XiphComment* tag = nullptr;
452+
if constexpr (std::is_same_v<T, TagLib::FileRef>)
453+
tag = dynamic_cast<TagLib::Ogg::XiphComment*>(file.tag());
454+
else
455+
tag = file.tag();
456+
if (!tag)
457+
return false;
458+
tag_clear<T>(tag);
459+
if (config.tag_mode == 'i' && (!std::is_same_v<T, TagLib::Ogg::Opus::File> ||
460+
(config.opus_mode != 't' && config.opus_mode != 'a')))
461+
tag_write<T>(tag, track.result, config);
466462

463+
bool ret = file.save();
464+
if (!std::is_same_v<T, TagLib::Ogg::Opus::File> || config.tag_mode == 's' ||
465+
!(config.opus_mode == 't' || config.opus_mode == 'a') || !ret)
466+
return ret;
467+
468+
}
467469
int16_t gain = config.opus_mode == 'a' && config.do_album ?
468470
GAIN_TO_Q78(track.result.album_gain) : GAIN_TO_Q78(track.result.track_gain);
469471
return set_opus_header_gain(track.path.string().c_str(), gain);
@@ -722,13 +724,16 @@ static void tag_write(TagLib::ASF::Tag *tag, const ScanResult &result, const Con
722724
}
723725

724726
static_assert(-1 == ~0); // 2's complement for signed integers
725-
bool set_opus_header_gain(const char *path, int16_t gain)
726-
{
727+
bool set_opus_header_gain(const char* path, int16_t gain)
728+
{
727729
uint32_t crc;
728-
if constexpr(std::endian::native == std::endian::big)
730+
if constexpr (std::endian::native == std::endian::big)
729731
gain = static_cast<int16_t>((gain << 8) & 0xff00) | ((gain >> 8) & 0x00ff);
730-
732+
731733
std::unique_ptr<std::FILE, int (*)(FILE*)> file(fopen(path, "rb+"), fclose);
734+
if (!file)
735+
return false;
736+
732737
char buffer[8];
733738
size_t page_size = 0;
734739
size_t opus_header_size = 0;

0 commit comments

Comments
 (0)