GODT-2160: Ensure we can safely move cache file
It's currently impossible to wait until all SQLite write finish to disk. This is not guaranteed when closing the ent DB client. The existing code to move the cache handles the case where the new location is on a new drive. However, due to the above issue this can now lead to database corruption. To avoid database corruption we now use the `os.Rename` function and prevent moving the cache between drives until a better solution can be implemented.
This commit is contained in:
@ -42,7 +42,7 @@ func moveDir(from, to string) error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := move(filepath.Join(from, entry.Name()), filepath.Join(to, entry.Name())); err != nil {
|
||||
if err := moveFile(filepath.Join(from, entry.Name()), filepath.Join(to, entry.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -51,32 +51,12 @@ func moveDir(from, to string) error {
|
||||
return os.Remove(from)
|
||||
}
|
||||
|
||||
func move(from, to string) error {
|
||||
func moveFile(from, to string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(to), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.Open(from) // nolint:gosec
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
c, err := os.Create(to) // nolint:gosec
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
if err := os.Chmod(to, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := c.ReadFrom(f); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Remove(from); err != nil {
|
||||
if err := os.Rename(from, to); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user