You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
4.0 KiB
154 lines
4.0 KiB
10 years ago
|
/*
|
||
|
* 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 (
|
||
|
"errors"
|
||
|
"os"
|
||
|
"path"
|
||
|
"syscall"
|
||
|
|
||
|
"io/ioutil"
|
||
10 years ago
|
|
||
10 years ago
|
"github.com/minio/minio/pkg/iodine"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// internal disk struct
|
||
10 years ago
|
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 {
|
||
10 years ago
|
return nil, iodine.New(errors.New("invalid argument"), nil)
|
||
10 years ago
|
}
|
||
|
s := syscall.Statfs_t{}
|
||
|
err := syscall.Statfs(diskPath, &s)
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
|
st, err := os.Stat(diskPath)
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
|
if !st.IsDir() {
|
||
10 years ago
|
return nil, iodine.New(syscall.ENOTDIR, nil)
|
||
10 years ago
|
}
|
||
|
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
|
||
|
}
|
||
10 years ago
|
return nil, iodine.New(errors.New("unsupported filesystem"), nil)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// GetPath - get root disk path
|
||
10 years ago
|
func (d disk) GetPath() string {
|
||
|
return d.root
|
||
|
}
|
||
|
|
||
10 years ago
|
// GetOrder - get order of disk present in graph
|
||
10 years ago
|
func (d disk) GetOrder() int {
|
||
|
return d.order
|
||
|
}
|
||
|
|
||
10 years ago
|
// GetFSInfo - get disk filesystem and its usage information
|
||
10 years ago
|
func (d disk) GetFSInfo() map[string]string {
|
||
|
s := syscall.Statfs_t{}
|
||
|
err := syscall.Statfs(d.root, &s)
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
10 years ago
|
d.filesystem["Total"] = d.formatBytes(uint64(s.Bsize) * s.Blocks)
|
||
|
d.filesystem["Free"] = d.formatBytes(uint64(s.Bsize) * s.Bfree)
|
||
10 years ago
|
return d.filesystem
|
||
|
}
|
||
|
|
||
10 years ago
|
// MakeDir - make a directory inside disk root path
|
||
10 years ago
|
func (d disk) MakeDir(dirname string) error {
|
||
|
return os.MkdirAll(path.Join(d.root, dirname), 0700)
|
||
|
}
|
||
|
|
||
10 years ago
|
// ListDir - list a directory inside disk root path, get only directories
|
||
10 years ago
|
func (d disk) ListDir(dirname string) ([]os.FileInfo, error) {
|
||
|
contents, err := ioutil.ReadDir(path.Join(d.root, dirname))
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
|
var directories []os.FileInfo
|
||
|
for _, content := range contents {
|
||
|
// Include only directories, ignore everything else
|
||
|
if content.IsDir() {
|
||
|
directories = append(directories, content)
|
||
|
}
|
||
|
}
|
||
|
return directories, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// ListFiles - list a directory inside disk root path, get only files
|
||
10 years ago
|
func (d disk) ListFiles(dirname string) ([]os.FileInfo, error) {
|
||
|
contents, err := ioutil.ReadDir(path.Join(d.root, dirname))
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
|
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
|
||
|
}
|
||
|
|
||
10 years ago
|
// MakeFile - create a file inside disk root path
|
||
10 years ago
|
func (d disk) MakeFile(filename string) (*os.File, error) {
|
||
|
if filename == "" {
|
||
10 years ago
|
return nil, iodine.New(errors.New("Invalid argument"), nil)
|
||
10 years ago
|
}
|
||
|
filePath := path.Join(d.root, filename)
|
||
|
// Create directories if they don't exist
|
||
|
if err := os.MkdirAll(path.Dir(filePath), 0700); err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
10 years ago
|
dataFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0600)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
|
return dataFile, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// OpenFile - read a file inside disk root path
|
||
10 years ago
|
func (d disk) OpenFile(filename string) (*os.File, error) {
|
||
|
if filename == "" {
|
||
10 years ago
|
return nil, iodine.New(errors.New("Invalid argument"), nil)
|
||
10 years ago
|
}
|
||
|
dataFile, err := os.Open(path.Join(d.root, filename))
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
|
return dataFile, nil
|
||
|
}
|