Skip to content

Commit

Permalink
fix: For Windows, close temp file before removing (#22492)
Browse files Browse the repository at this point in the history
closes #21470

(cherry picked from commit d2199ef)
  • Loading branch information
davidby-influx committed Oct 14, 2021
1 parent a083fa9 commit f172a2b
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions cmd/influxd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (cmd *Command) parseFlags(args []string) (err error) {
return err
}

func (cmd *Command) backupShard(db, rp, sid string) error {
func (cmd *Command) backupShard(db, rp, sid string) (err error) {
reqType := snapshotter.RequestShardBackup
if !cmd.isBackup {
reqType = snapshotter.RequestShardExport
Expand Down Expand Up @@ -254,73 +254,71 @@ func (cmd *Command) backupShard(db, rp, sid string) error {
// TODO: verify shard backup data
err = cmd.downloadAndVerify(req, shardArchivePath, nil)
if err != nil {
os.Remove(shardArchivePath)
_ = os.Remove(shardArchivePath)
return err
}
if !cmd.portable {
cmd.BackupFiles = append(cmd.BackupFiles, shardArchivePath)
}

if cmd.portable {
f, err := os.Open(shardArchivePath)
var f, out *os.File
f, err = os.Open(shardArchivePath)
if err != nil {
return err
}
defer f.Close()
defer os.Remove(shardArchivePath)
defer func() {
if closeErr := f.Close(); err == nil {
err = closeErr
}
if remErr := os.Remove(shardArchivePath); err == nil {
err = remErr
}
}()

filePrefix := cmd.portableFileBase + ".s" + sid
filename := filePrefix + ".tar.gz"
out, err := os.OpenFile(filepath.Join(cmd.path, filename), os.O_CREATE|os.O_RDWR, 0600)
out, err = os.OpenFile(filepath.Join(cmd.path, filename), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}

defer func() {
if out != nil {
if e := out.Close(); err == nil {
err = e
}
}
}()
zw := gzip.NewWriter(out)
defer func() {
if zw != nil {
if e := zw.Close(); err == nil {
err = e
}
}
}()
zw.Name = filePrefix + ".tar"

cw := backup_util.CountingWriter{Writer: zw}

_, err = io.Copy(&cw, f)
if err != nil {
if err := zw.Close(); err != nil {
return err
}

if err := out.Close(); err != nil {
return err
}
return err
}

shardid, err := strconv.ParseUint(sid, 10, 64)
var shardID uint64
shardID, err = strconv.ParseUint(sid, 10, 64)
if err != nil {
if err := zw.Close(); err != nil {
return err
}

if err := out.Close(); err != nil {
return err
}
return err
}
cmd.manifest.Files = append(cmd.manifest.Files, backup_util.Entry{
Database: db,
Policy: rp,
ShardID: shardid,
ShardID: shardID,
FileName: filename,
Size: cw.Total,
LastModified: 0,
})

if err := zw.Close(); err != nil {
return err
}

if err := out.Close(); err != nil {
return err
}

cmd.BackupFiles = append(cmd.BackupFiles, filename)
}
return nil
Expand Down

0 comments on commit f172a2b

Please sign in to comment.