mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Remove empty intermediate directories, closes #12
This commit is contained in:
@@ -18,6 +18,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
### Fixed
|
### Fixed
|
||||||
- No longer run out of file handles when dealing with a large number of
|
- No longer run out of file handles when dealing with a large number of
|
||||||
recipients for a single message.
|
recipients for a single message.
|
||||||
|
- Empty intermediate directories are now removed when a mailbox is deleted,
|
||||||
|
leaving less junk on your filesystem.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Build now requires Go 1.7 or later
|
- Build now requires Go 1.7 or later
|
||||||
|
|||||||
@@ -239,19 +239,6 @@ func (mb *FileMailbox) readIndex() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// createDir checks for the presence of the path for this mailbox, creates it if needed
|
|
||||||
func (mb *FileMailbox) createDir() error {
|
|
||||||
dirMx.Lock()
|
|
||||||
defer dirMx.Unlock()
|
|
||||||
if _, err := os.Stat(mb.path); err != nil {
|
|
||||||
if err := os.MkdirAll(mb.path, 0770); err != nil {
|
|
||||||
log.Errorf("Failed to create directory %v, %v", mb.path, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeIndex overwrites the index on disk with the current mailbox data
|
// writeIndex overwrites the index on disk with the current mailbox data
|
||||||
func (mb *FileMailbox) writeIndex() error {
|
func (mb *FileMailbox) writeIndex() error {
|
||||||
// Lock for writing
|
// Lock for writing
|
||||||
@@ -288,15 +275,66 @@ func (mb *FileMailbox) writeIndex() error {
|
|||||||
} else {
|
} else {
|
||||||
// No messages, delete index+maildir
|
// No messages, delete index+maildir
|
||||||
log.Tracef("Removing mailbox %v", mb.path)
|
log.Tracef("Removing mailbox %v", mb.path)
|
||||||
// deletes are dangerous, requires write lock
|
return mb.removeDir()
|
||||||
dirMx.Lock()
|
|
||||||
defer dirMx.Unlock()
|
|
||||||
return os.RemoveAll(mb.path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createDir checks for the presence of the path for this mailbox, creates it if needed
|
||||||
|
func (mb *FileMailbox) createDir() error {
|
||||||
|
dirMx.Lock()
|
||||||
|
defer dirMx.Unlock()
|
||||||
|
if _, err := os.Stat(mb.path); err != nil {
|
||||||
|
if err := os.MkdirAll(mb.path, 0770); err != nil {
|
||||||
|
log.Errorf("Failed to create directory %v, %v", mb.path, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeDir removes the mailbox, plus empty higher level directories
|
||||||
|
func (mb *FileMailbox) removeDir() error {
|
||||||
|
dirMx.Lock()
|
||||||
|
defer dirMx.Unlock()
|
||||||
|
// remove mailbox dir, including index file
|
||||||
|
if err := os.RemoveAll(mb.path); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// remove parents if empty
|
||||||
|
dir := filepath.Dir(mb.path)
|
||||||
|
if removeDirIfEmpty(dir) {
|
||||||
|
removeDirIfEmpty(filepath.Dir(dir))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeDirIfEmpty will remove the specified directory if it contains no files or directories.
|
||||||
|
// Caller should hold dirMx. Returns true if dir was removed.
|
||||||
|
func removeDirIfEmpty(path string) (removed bool) {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
files, err := f.Readdirnames(0)
|
||||||
|
_ = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(files) > 0 {
|
||||||
|
// Dir not empty
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
log.Tracef("Removing dir %v", path)
|
||||||
|
err = os.Remove(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to remove %q: %v", path, err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// generatePrefix converts a Time object into the ISO style format we use
|
// generatePrefix converts a Time object into the ISO style format we use
|
||||||
// as a prefix for message files. Note: It is used directly by unit
|
// as a prefix for message files. Note: It is used directly by unit
|
||||||
// tests.
|
// tests.
|
||||||
|
|||||||
Reference in New Issue
Block a user