2015-04-05 04:53:41 -04:00
|
|
|
/*
|
|
|
|
* Minimalist Object Storage, (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 donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-06-18 19:02:34 -04:00
|
|
|
"path/filepath"
|
2015-06-23 14:44:32 -04:00
|
|
|
"strconv"
|
2015-04-05 04:53:41 -04:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"io/ioutil"
|
2015-04-07 18:55:44 -04:00
|
|
|
|
2015-05-11 19:23:10 -04:00
|
|
|
"github.com/minio/minio/pkg/iodine"
|
2015-04-05 04:53:41 -04:00
|
|
|
)
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// internal disk struct
|
2015-04-05 04:53:41 -04:00
|
|
|
type disk struct {
|
|
|
|
root string
|
|
|
|
order int
|
|
|
|
filesystem map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDisk - instantiate new disk
|
|
|
|
func NewDisk(diskPath string, diskOrder int) (Disk, error) {
|
|
|
|
if diskPath == "" || diskOrder < 0 {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(InvalidArgument{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
s := syscall.Statfs_t{}
|
|
|
|
err := syscall.Statfs(diskPath, &s)
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
st, err := os.Stat(diskPath)
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
if !st.IsDir() {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(syscall.ENOTDIR, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
d := disk{
|
|
|
|
root: diskPath,
|
|
|
|
order: diskOrder,
|
|
|
|
filesystem: make(map[string]string),
|
|
|
|
}
|
|
|
|
if fsType := d.getFSType(s.Type); fsType != "UNKNOWN" {
|
|
|
|
d.filesystem["FSType"] = fsType
|
|
|
|
d.filesystem["MountPoint"] = d.root
|
|
|
|
return d, nil
|
|
|
|
}
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(UnsupportedFilesystem{
|
|
|
|
Type: strconv.FormatInt(s.Type, 10),
|
|
|
|
}, map[string]string{"Type": strconv.FormatInt(s.Type, 10)})
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// GetPath - get root disk path
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) GetPath() string {
|
|
|
|
return d.root
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// GetOrder - get order of disk present in graph
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) GetOrder() int {
|
|
|
|
return d.order
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// GetFSInfo - get disk filesystem and its usage information
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) GetFSInfo() map[string]string {
|
|
|
|
s := syscall.Statfs_t{}
|
|
|
|
err := syscall.Statfs(d.root, &s)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-26 04:31:05 -04:00
|
|
|
d.filesystem["Total"] = d.formatBytes(uint64(s.Bsize) * s.Blocks)
|
|
|
|
d.filesystem["Free"] = d.formatBytes(uint64(s.Bsize) * s.Bfree)
|
2015-04-05 04:53:41 -04:00
|
|
|
return d.filesystem
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// MakeDir - make a directory inside disk root path
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) MakeDir(dirname string) error {
|
2015-06-18 19:02:34 -04:00
|
|
|
return os.MkdirAll(filepath.Join(d.root, dirname), 0700)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// ListDir - list a directory inside disk root path, get only directories
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) ListDir(dirname string) ([]os.FileInfo, error) {
|
2015-06-18 19:02:34 -04:00
|
|
|
contents, err := ioutil.ReadDir(filepath.Join(d.root, dirname))
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
var directories []os.FileInfo
|
|
|
|
for _, content := range contents {
|
|
|
|
// Include only directories, ignore everything else
|
|
|
|
if content.IsDir() {
|
|
|
|
directories = append(directories, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return directories, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// ListFiles - list a directory inside disk root path, get only files
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) ListFiles(dirname string) ([]os.FileInfo, error) {
|
2015-06-18 19:02:34 -04:00
|
|
|
contents, err := ioutil.ReadDir(filepath.Join(d.root, dirname))
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
var files []os.FileInfo
|
|
|
|
for _, content := range contents {
|
|
|
|
// Include only regular files, ignore everything else
|
|
|
|
if content.Mode().IsRegular() {
|
|
|
|
files = append(files, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// MakeFile - create a file inside disk root path
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) MakeFile(filename string) (*os.File, error) {
|
|
|
|
if filename == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(InvalidArgument{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-06-18 19:02:34 -04:00
|
|
|
filePath := filepath.Join(d.root, filename)
|
2015-04-05 04:53:41 -04:00
|
|
|
// Create directories if they don't exist
|
2015-06-18 19:02:34 -04:00
|
|
|
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
dataFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0600)
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
return dataFile, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// OpenFile - read a file inside disk root path
|
2015-04-05 04:53:41 -04:00
|
|
|
func (d disk) OpenFile(filename string) (*os.File, error) {
|
|
|
|
if filename == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(InvalidArgument{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-06-18 19:02:34 -04:00
|
|
|
dataFile, err := os.Open(filepath.Join(d.root, filename))
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
return dataFile, nil
|
|
|
|
}
|