From ee4432bc40614170d4d6a7dc47f0caa29c147821 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Sun, 2 Aug 2015 17:34:29 +0530 Subject: [PATCH] rename disk.OpenFile to Open which will do os.Open (which will be read-only). disk.OpenFile will do os.OpenFile (which can be rw, append) --- pkg/donut/bucket.go | 4 ++-- pkg/donut/disk/disk.go | 19 +++++++++++++++++-- pkg/donut/disk/disk_test.go | 8 ++++---- pkg/donut/donut-v1.go | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/donut/bucket.go b/pkg/donut/bucket.go index b6ee6c7e4..b75e0171f 100644 --- a/pkg/donut/bucket.go +++ b/pkg/donut/bucket.go @@ -101,7 +101,7 @@ func (b bucket) getBucketMetadataReaders() (map[int]io.ReadCloser, error) { } var bucketMetaDataReader io.ReadCloser for order, disk := range disks { - bucketMetaDataReader, err = disk.OpenFile(filepath.Join(b.donutName, bucketMetadataConfig)) + bucketMetaDataReader, err = disk.Open(filepath.Join(b.donutName, bucketMetadataConfig)) if err != nil { continue } @@ -581,7 +581,7 @@ func (b bucket) getObjectReaders(objectName, objectMeta string) (map[int]io.Read var objectSlice io.ReadCloser bucketSlice := fmt.Sprintf("%s$%d$%d", b.name, nodeSlice, order) objectPath := filepath.Join(b.donutName, bucketSlice, objectName, objectMeta) - objectSlice, err = disk.OpenFile(objectPath) + objectSlice, err = disk.Open(objectPath) if err == nil { readers[order] = objectSlice } diff --git a/pkg/donut/disk/disk.go b/pkg/donut/disk/disk.go index 270f0645b..8c450740b 100644 --- a/pkg/donut/disk/disk.go +++ b/pkg/donut/disk/disk.go @@ -170,8 +170,8 @@ func (disk Disk) CreateFile(filename string) (*atomic.File, error) { return f, nil } -// OpenFile - read a file inside disk root path -func (disk Disk) OpenFile(filename string) (*os.File, error) { +// Open - read a file inside disk root path +func (disk Disk) Open(filename string) (*os.File, error) { disk.lock.Lock() defer disk.lock.Unlock() @@ -185,6 +185,21 @@ func (disk Disk) OpenFile(filename string) (*os.File, error) { return dataFile, nil } +// OpenFile - Use with caution +func (disk Disk) OpenFile(filename string, flags int, perm os.FileMode) (*os.File, error) { + disk.lock.Lock() + defer disk.lock.Unlock() + + if filename == "" { + return nil, iodine.New(InvalidArgument{}, nil) + } + dataFile, err := os.OpenFile(filepath.Join(disk.path, filename), flags, perm) + if err != nil { + return nil, iodine.New(err, nil) + } + return dataFile, nil +} + // formatBytes - Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B func formatBytes(i int64) (result string) { switch { diff --git a/pkg/donut/disk/disk_test.go b/pkg/donut/disk/disk_test.go index bdb5cf1ed..5eb59a8db 100644 --- a/pkg/donut/disk/disk_test.go +++ b/pkg/donut/disk/disk_test.go @@ -65,19 +65,19 @@ func (s *MyDiskSuite) TestDiskCreateFile(c *C) { // close renames the file f.Close() - // Openfile should be a success - _, err = s.disk.OpenFile("hello1") + // Open should be a success + _, err = s.disk.Open("hello1") c.Assert(err, IsNil) } -func (s *MyDiskSuite) TestDiskOpenFile(c *C) { +func (s *MyDiskSuite) TestDiskOpen(c *C) { f1, err := s.disk.CreateFile("hello2") c.Assert(err, IsNil) c.Assert(f1.Name(), Not(Equals), filepath.Join(s.path, "hello2")) // close renames the file f1.Close() - f2, err := s.disk.OpenFile("hello2") + f2, err := s.disk.Open("hello2") c.Assert(err, IsNil) c.Assert(f2.Name(), Equals, filepath.Join(s.path, "hello2")) defer f2.Close() diff --git a/pkg/donut/donut-v1.go b/pkg/donut/donut-v1.go index c8676fabb..0735e1698 100644 --- a/pkg/donut/donut-v1.go +++ b/pkg/donut/donut-v1.go @@ -576,7 +576,7 @@ func (donut API) getBucketMetadataReaders() (map[int]io.ReadCloser, error) { } var bucketMetaDataReader io.ReadCloser for order, disk := range disks { - bucketMetaDataReader, err = disk.OpenFile(filepath.Join(donut.config.DonutName, bucketMetadataConfig)) + bucketMetaDataReader, err = disk.Open(filepath.Join(donut.config.DonutName, bucketMetadataConfig)) if err != nil { continue }