mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 12:46:46 +00:00
68 lines
1.1 KiB
Go
68 lines
1.1 KiB
Go
package bridge
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func moveDir(from, to string) error {
|
|
entries, err := os.ReadDir(from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
if err := os.Mkdir(filepath.Join(to, entry.Name()), 0700); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := moveDir(filepath.Join(from, entry.Name()), filepath.Join(to, entry.Name())); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.RemoveAll(filepath.Join(from, entry.Name())); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err := move(filepath.Join(from, entry.Name()), filepath.Join(to, entry.Name())); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return os.Remove(from)
|
|
}
|
|
|
|
func move(from, to string) error {
|
|
if err := os.MkdirAll(filepath.Dir(to), 0700); err != nil {
|
|
return err
|
|
}
|
|
|
|
f, err := os.Open(from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
c, err := os.Create(to)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer c.Close()
|
|
|
|
if err := os.Chmod(to, 0600); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := c.ReadFrom(f); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.Remove(from); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|