Repository: beetbox/beets
Issue: ReplayGain plugin modifies source files in copy mode

### Problem

Importing music into the Beets library in copy mode with the ReplayGain plugin enabled causes the source files to be modified. The expected behaviour would be for Beets to only modify the files copied to the library and to keep the original files intact. For the purpose of this bug report I have reproduced the issue with the following config file:

```yaml
directory: ~/library
ui:
  color: no
plugins: replaygain
replaygain:
  backend: ffmpeg
```

Running command

```sh
$ beet -vv import "source/Aphex Twin - Flim (Laughing Man edit).flac"
```

results in the following output:

```
user configuration: /home/x/.config/beets/config.yaml
data directory: /home/x/.config/beets
plugin paths: 
Sending event: pluginload
library database: /home/x/.config/beets/library.db
library directory: /home/x/library
Sending event: library_opened
Sending event: import_begin
Sending event: import_task_created
Sending event: import_task_start
Looking up: /home/x/source/Aphex Twin - Flim (Laughing Man edit).flac
Tagging Aphex Twin - Flim (Laughing Man Edit)
No album ID found.
Search terms: Aphex Twin - Flim (Laughing Man Edit)
Album might be VA: False
Searching for MusicBrainz releases with: {'release': 'flim (laughing man edit)', 'artist': 'aphex twin', 'tracks': '1'}
Requesting MusicBrainz release 64006df9-ab6c-4b7d-a424-0dfd51ee8c74
primary MB release type: single
secondary MB release type(s): remix
Sending event: albuminfo_received
Candidate: Aphex Twin - Flim (Laughing Man edit) (64006df9-ab6c-4b7d-a424-0dfd51ee8c74)
Computing track assignment...
...done.
Success. Distance: 0.00
Requesting MusicBrainz release b26e7acb-1f99-4a28-a86c-e0ba67aa3b4e
primary MB release type: single
Sending event: albuminfo_received
Candidate: Aphex Twin - Analogue Bubblebath (Dumb Dan edit) (b26e7acb-1f99-4a28-a86c-e0ba67aa3b4e)
Computing track assignment...
...done.
Success. Distance: 0.47
Requesting MusicBrainz release 0ecef291-34ed-409f-9023-06c94644c5ce
primary MB release type: single
Sending event: albuminfo_received
Candidate: Aphex Twin - MARCHROMT30a Edit 2b 96 (0ecef291-34ed-409f-9023-06c94644c5ce)
Computing track assignment...
...done.
Success. Distance: 0.56
Requesting MusicBrainz release cce3f356-87e1-427f-8161-77e08498a7fe
primary MB release type: single
Sending event: albuminfo_received
Candidate: Aphex Twin - MARCHROMT30a Edit 2b 96 (cce3f356-87e1-427f-8161-77e08498a7fe)
Computing track assignment...
...done.
Success. Distance: 0.56
Requesting MusicBrainz release 9644eaec-48e8-43e4-84ea-3cfc123cd82f
primary MB release type: single
secondary MB release type(s): remix
Sending event: albuminfo_received
Candidate: Aphex Twin - Windowlicker (Renaissance Man bootcut) (9644eaec-48e8-43e4-84ea-3cfc123cd82f)
Computing track assignment...
...done.
Success. Distance: 0.49
Evaluating 5 candidates.

/home/x/source/Aphex Twin - Flim (Laughing Man edit).flac (1 items)
Sending event: before_choose_candidate
Correcting tags from:
    Aphex Twin - Flim (Laughing Man Edit)
To:
    Aphex Twin - Flim (Laughing Man edit)
URL:
    https://musicbrainz.org/release/64006df9-ab6c-4b7d-a424-0dfd51ee8c74
(Similarity: 100.0%) (Digital Media, 2018, XW, [none])
 * Flim (Laughing Man Edit) -> Flim (Laughing Man edit)
Sending event: import_task_choice
Sending event: import_task_apply
0 of 1 items replaced
Sending event: database_change
Sending event: database_change
Sending event: database_change
Sending event: database_change
replaygain: analyzing Aphex Twin - Flim (Laughing Man edit)
replaygain: analyzing Aphex Twin - Flim (Laughing Man edit) - Flim (Laughing Man edit)
replaygain: executing ffmpeg -nostats -hide_banner -i /home/x/source/Aphex Twin - Flim (Laughing Man edit).flac -map a:0 -filter ebur128=peak=true -f null -
replaygain: Aphex Twin - Flim (Laughing Man edit) - Flim (Laughing Man edit): 3336 blocks over -19.9 LUFS
replaygain: Aphex Twin - Flim (Laughing Man edit) - Flim (Laughing Man edit): gain -8.3 LU, peak 1.1614486138403428
replaygain: [Item(id=1, path=b'/home/x/source/Aphex Twin - Flim (Laughing Man edit).flac', album_id=1, title='Flim (Laughing Man edit)', artist='Aphex Twin', artist_sort='Aphex Twin', artist_credit='Aphex Twin', album='Flim (Laughing Man edit)', albumartist='Aphex Twin', albumartist_sort='Aphex Twin', albumartist_credit='Aphex Twin', genre='', style='', discogs_albumid=0, discogs_artistid=0, discogs_labelid=0, lyricist='', composer='Richard D. James', composer_sort='James, Richard D.', work='Flim', mb_workid='f891d2f8-7737-31f9-8ef2-e6e54ebc3077', work_disambig='upbeat', arranger='', grouping='', year=2018, month=10, day=24, track=1, tracktotal=1, disc=1, disctotal=1, lyrics='', comments='https://soundcloud.com/1laughter/aphex-twin-flim-laughing-man-edit', bpm=0, comp=0, mb_trackid='30d6a7ab-3e9d-4334-b536-9e477e1bb255', mb_albumid='64006df9-ab6c-4b7d-a424-0dfd51ee8c74', mb_artistid='f22942a1-6f70-4f48-866e-238cb2308fbd', mb_albumartistid='f22942a1-6f70-4f48-866e-238cb2308fbd', mb_releasetrackid='85d08496-2b59-4ac6-9a93-a62135c017b0', trackdisambig='', albumtype='single', label='', acoustid_fingerprint='', acoustid_id='', mb_releasegroupid='0b69af77-959f-4204-a06b-fa62955fea25', asin='', catalognum='[none]', script='Latn', language='eng', country='XW', albumstatus='Bootleg', media='Digital Media', albumdisambig='', releasegroupdisambig='', disctitle='', encoder='Wave', rg_track_gain=None, rg_track_peak=None, rg_album_gain=None, rg_album_peak=None, r128_track_gain=None, r128_album_gain=None, original_year=2018, original_month=10, original_day=24, initial_key=None, length=351.08945578231294, bitrate=774128, format='FLAC', samplerate=44100, bitdepth=16, channels=2, mtime=0.0, added=1615330388.7175112, track_alt='1', data_source='MusicBrainz', artpath=None)]: gain -8.3 LU, peak 1.1614486138403428
Sending event: database_change
replaygain: applied track gain -8.3 LU, peak 1.1614486138403428 of FS
Sending event: database_change
replaygain: applied album gain -8.3 LU, peak 1.1614486138403428 of FS
Sending event: write
Sending event: after_write
replaygain: done analyzing Aphex Twin - Flim (Laughing Man edit) - Flim (Laughing Man edit)
Sending event: item_copied
Sending event: database_change
Sending event: database_change
Sending event: write
Sending event: after_write
Sending event: database_change
Sending event: import_task_files
Sending event: album_imported
Sending event: import
Sending event: cli_exit
```

If I undo all changes caused by the import, run

```sh
$ chmod 444 "source/Aphex Twin - Flim (Laughing Man edit).flac"
```

and import the music again using the same command, then the last few lines of the output change to the following:

```
[...]
Sending event: database_change
replaygain: applied track gain -8.3 LU, peak 1.1614486138403428 of FS
Sending event: database_change
replaygain: applied album gain -8.3 LU, peak 1.1614486138403428 of FS
Sending event: write
error writing /home/x/source/Aphex Twin - Flim (Laughing Man edit).flac: [Errno 13] Permission denied: b'/home/x/source/Aphex Twin - Flim (Laughing Man edit).flac'
replaygain: done analyzing Aphex Twin - Flim (Laughing Man edit) - Flim (Laughing Man edit)
Sending event: item_copied
Sending event: database_change
Sending event: database_change
Sending event: write
Sending event: after_write
Sending event: database_change
Sending event: import_task_files
Sending event: album_imported
Sending event: import
Sending event: cli_exit
```

The difference is the write error on line 7 of the above output. The issue stops happening after changing the config file to:

```yaml
directory: ~/library
ui:
  color: no
plugins: replaygain
replaygain:
  auto: no
  backend: ffmpeg
```

Below is the output of

```sh
$ mediainfo "source/Aphex Twin - Flim (Laughing Man edit).flac"
```

before importing the file into Beets:

```
General
Complete name                            : source/Aphex Twin - Flim (Laughing Man edit).flac
Format                                   : FLAC
Format/Info                              : Free Lossless Audio Codec
File size                                : 32.4 MiB
Duration                                 : 5 min 51 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 775 kb/s
Album                                    : Flim (Laughing Man Edit)
Album/Performer                          : Aphex Twin
Track name                               : Flim (Laughing Man Edit)
Track name/Position                      : 1
Performer                                : Aphex Twin
Recorded date                            : 2018
Writing application                      : Wave
Cover                                    : Yes
Cover type                               : Cover (front)
Cover MIME                               : image/jpeg
Comment                                  : https://soundcloud.com/1laughter/aphex-twin-flim-laughing-man-edit
encoder+                                 : Wave -compression="PCM" -bits="16" -freq="44100"

Audio
Format                                   : FLAC
Format/Info                              : Free Lossless Audio Codec
Duration                                 : 5 min 51 s
Bit rate mode                            : Variable
Bit rate                                 : 774 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 44.1 kHz
Bit depth                                : 16 bits
Compression mode                         : Lossless
Stream size                              : 32.4 MiB (100%)
Writing library                          : libFLAC 1.3.2 (UTC 2017-01-01)
```

And this is the output of the same command after the Beets import command mistakenly modifies it:

```
General
Complete name                            : source/Aphex Twin - Flim (Laughing Man edit).flac
Format                                   : FLAC
Format/Info                              : Free Lossless Audio Codec
File size                                : 32.5 MiB
Duration                                 : 5 min 51 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 776 kb/s
Album replay gain                        : -8.30 dB
Album replay gain peak                   : 1.161449
Album                                    : Flim (Laughing Man edit)
Album/Performer                          : Aphex Twin / Aphex Twin
Part                                     : 1
Part/Total                               : 1
Track name                               : Flim (Laughing Man edit)
Track name/Position                      : 1
Track name/Total                         : 1
Performer                                : Aphex Twin
Composer                                 : Richard D. James
Encoded by                               : Wave
Description                              : https://soundcloud.com/1laughter/aphex-twin-flim-laughing-man-edit
Recorded date                            : 2018-10-24 / 2018
Writing application                      : Wave
BPM                                      : 0
Cover                                    : Yes
Cover type                               : Cover (front)
Cover MIME                               : image/jpeg
Comment                                  : https://soundcloud.com/1laughter/aphex-twin-flim-laughing-man-edit
encoder+                                 : Wave -compression="PCM" -bits="16" -freq="44100"
ALBUMARTIST_CREDIT                       : Aphex Twin
ALBUMARTISTSORT                          : Aphex Twin
RELEASESTATUS                            : Bootleg
MUSICBRAINZ_ALBUMSTATUS                  : Bootleg
RELEASETYPE                              : single
MUSICBRAINZ_ALBUMTYPE                    : single
ARTIST_CREDIT                            : Aphex Twin
ARTISTSORT                               : Aphex Twin
CATALOGNUMBER                            : [none]
COMPILATION                              : 0
COMPOSERSORT                             : James, Richard D.
RELEASECOUNTRY                           : XW
DISCC                                    : 1
MUSICBRAINZ_RELEASEGROUPID               : 0b69af77-959f-4204-a06b-fa62955fea25
MUSICBRAINZ_RELEASETRACKID               : 85d08496-2b59-4ac6-9a93-a62135c017b0
MUSICBRAINZ_WORKID                       : f891d2f8-7737-31f9-8ef2-e6e54ebc3077
MEDIA                                    : Digital Media
ORIGINALDATE                             : 2018-10-24
SCRIPT                                   : Latn
TRACK                                    : 1
TRACKC                                   : 1

Audio
[...]
```

### Setup

* OS: GNU/Linux (Arch Linux)
* Python version: Python 3.9.2
* Beets version: debd382837ef1d30574c2234710d536bb299f979 (this is the latest version at the time of writing)

Changed files:
beetsplug/replaygain.py

diff --git a/beetsplug/replaygain.py b/beetsplug/replaygain.py
index 8b9447bdb..d85a95022 100644
--- a/beetsplug/replaygain.py
+++ b/beetsplug/replaygain.py
@@ -1321,17 +1321,9 @@ class ReplayGainPlugin(BeetsPlugin):
         """
         if self.config['auto']:
             if task.is_album:
-                self.handle_album(
-                    task.album,
-                    self.config['auto'].get(bool),
-                    self.config['overwrite'].get(bool)
-                )
+                self.handle_album(task.album, False)
             else:
-                self.handle_track(
-                    task.item,
-                    self.config['auto'].get(bool),
-                    self.config['overwrite'].get(bool)
-                )
+                self.handle_track(task.item, False)
 
     def commands(self):
         """Return the "replaygain" ui subcommand.