From 52b55afce072579c11944aeaf94ff16a988ab54a Mon Sep 17 00:00:00 2001 From: Bala FA Date: Mon, 4 Jul 2016 08:31:40 +0530 Subject: [PATCH] FS: check whether disk format is FS or not. (#2083) Fixes #2060 --- fs-v1-errors.go | 22 ++++++++++++++++++++++ fs-v1-metadata.go | 5 +++++ fs-v1.go | 4 +++- fs-v1_test.go | 28 +++++++++++++++++++++++++--- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 fs-v1-errors.go diff --git a/fs-v1-errors.go b/fs-v1-errors.go new file mode 100644 index 000000000..f2f3f3100 --- /dev/null +++ b/fs-v1-errors.go @@ -0,0 +1,22 @@ +/* + * Minio Cloud Storage, (C) 2016 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 "errors" + +// errFSDiskFormat - returned when given disk format is other than FS format. +var errFSDiskFormat = errors.New("Disk is not in FS format.") diff --git a/fs-v1-metadata.go b/fs-v1-metadata.go index 7399b446b..d4070235f 100644 --- a/fs-v1-metadata.go +++ b/fs-v1-metadata.go @@ -93,6 +93,11 @@ func newFSFormatV1() (format formatConfigV1) { } } +// isFSFormat - returns whether given formatConfigV1 is FS type or not. +func isFSFormat(format formatConfigV1) bool { + return format.Format == "fs" +} + // writes FS format (format.json) into minioMetaBucket. func writeFSFormatData(storage StorageAPI, fsFormat formatConfigV1) error { metadataBytes, err := json.Marshal(fsFormat) diff --git a/fs-v1.go b/fs-v1.go index 0dd67ec73..cc1c416b1 100644 --- a/fs-v1.go +++ b/fs-v1.go @@ -103,7 +103,7 @@ func newFSObjects(disk string) (ObjectLayer, error) { // loading format.json from minioMetaBucket. // Note: The format.json content is ignored, reserved for future use. - _, err = loadFormatFS(storage) + format, err := loadFormatFS(storage) if err != nil { if err == errFileNotFound { // format.json doesn't exist, create it inside minioMetaBucket. @@ -114,6 +114,8 @@ func newFSObjects(disk string) (ObjectLayer, error) { } else { return nil, err } + } else if !isFSFormat(format) { + return nil, errFSDiskFormat } // Register the callback that should be called when the process shuts down. diff --git a/fs-v1_test.go b/fs-v1_test.go index 49f9e6131..8e6a2ad36 100644 --- a/fs-v1_test.go +++ b/fs-v1_test.go @@ -31,9 +31,31 @@ func TestNewFS(t *testing.T) { disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) defer removeAll(disk) - // Initializes single disk, validate if there is no error. - _, err := newFSObjects(disk) + // Setup to test errFSDiskFormat. + disks := []string{} + for i := 0; i < 6; i++ { + xlDisk := filepath.Join(os.TempDir(), "minio-"+nextSuffix()) + defer removeAll(xlDisk) + disks = append(disks, xlDisk) + } + + // Initializes all disks with XL + _, err := newXLObjects(disks) if err != nil { - t.Fatalf("Unable to initialize erasure, %s", err) + t.Fatalf("Unable to initialize XL object, %s", err) + } + + testCases := []struct { + disk string + expectedErr error + }{ + {disk, nil}, + {disks[0], errFSDiskFormat}, + } + + for _, testCase := range testCases { + if _, err := newFSObjects(testCase.disk); err != testCase.expectedErr { + t.Fatalf("expected: %s, got: %s", testCase.expectedErr, err) + } } }