FS: remove .minio directory if .minio/multipart is empty. (#1899)

fixes #1886
This commit is contained in:
Krishna Srinivas 2016-06-16 08:50:38 +05:30 committed by Harshavardhana
parent de1c7d33eb
commit e2743d05e8
3 changed files with 82 additions and 0 deletions

View File

@ -49,6 +49,22 @@ func loadFormatFS(storageDisk StorageAPI) ([]byte, error) {
return readAll(storageDisk, minioMetaBucket, fsFormatJSONFile) return readAll(storageDisk, minioMetaBucket, fsFormatJSONFile)
} }
// Should be called when process shuts down.
func shutdownFS(storage StorageAPI) {
_, err := storage.ListDir(minioMetaBucket, mpartMetaPrefix)
if err != errFileNotFound {
// Multipart directory is not empty hence do not remove .minio volume.
os.Exit(0)
}
prefix := ""
if err := cleanupDir(storage, minioMetaBucket, prefix); err != nil {
os.Exit(0)
return
}
storage.DeleteVol(minioMetaBucket)
os.Exit(0)
}
// newFSObjects - initialize new fs object layer. // newFSObjects - initialize new fs object layer.
func newFSObjects(disk string) (ObjectLayer, error) { func newFSObjects(disk string) (ObjectLayer, error) {
storage, err := newStorageAPI(disk) storage, err := newStorageAPI(disk)
@ -72,6 +88,10 @@ func newFSObjects(disk string) (ObjectLayer, error) {
return nil, err return nil, err
} }
} }
// Register the callback that should be called when the process shuts down.
registerShutdown(func() {
shutdownFS(storage)
})
// Return successfully initialized object layer. // Return successfully initialized object layer.
return fsObjects{ return fsObjects{
storage: storage, storage: storage,

View File

@ -17,9 +17,11 @@
package main package main
import ( import (
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"syscall"
) )
const ( const (
@ -27,6 +29,17 @@ const (
blockSizeV1 = 10 * 1024 * 1024 // 10MiB. blockSizeV1 = 10 * 1024 * 1024 // 10MiB.
) )
// Register callback functions that needs to be called when process shutsdown.
// For now, SIGINT triggers the callbacks, in future controller can trigger
// shutdown callbacks.
func registerShutdown(callback func()) {
go func() {
trapCh := signalTrap(os.Interrupt, syscall.SIGTERM)
<-trapCh
callback()
}()
}
// House keeping code needed for FS. // House keeping code needed for FS.
func fsHouseKeeping(storageDisk StorageAPI) error { func fsHouseKeeping(storageDisk StorageAPI) error {
// Attempt to create `.minio`. // Attempt to create `.minio`.

49
signals.go Normal file
View File

@ -0,0 +1,49 @@
/*
* Minio Client, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"os"
"os/signal"
)
// signalTrap traps the registered signals and notifies the caller.
func signalTrap(sig ...os.Signal) <-chan bool {
// channel to notify the caller.
trapCh := make(chan bool, 1)
go func(chan<- bool) {
// channel to receive signals.
sigCh := make(chan os.Signal, 1)
defer close(sigCh)
// `signal.Notify` registers the given channel to
// receive notifications of the specified signals.
signal.Notify(sigCh, sig...)
// Wait for the signal.
<-sigCh
// Once signal has been received stop signal Notify handler.
signal.Stop(sigCh)
// Notify the caller.
trapCh <- true
}(trapCh)
return trapCh
}