This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Starnop <[email protected]>
- Loading branch information
Showing
9 changed files
with
829 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* 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 store | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/dragonflyoss/Dragonfly/common/util" | ||
) | ||
|
||
// LocalStorageDriver is a const of local storage driver. | ||
const LocalStorageDriver = "local" | ||
|
||
var fileMutexLocker sync.Map | ||
|
||
func init() { | ||
Register(LocalStorageDriver, NewLocalStorage) | ||
} | ||
|
||
type fileMutex struct { | ||
count int32 | ||
sync.RWMutex | ||
} | ||
|
||
func getLock(key string, ro bool) { | ||
v, _ := fileMutexLocker.LoadOrStore(key, &fileMutex{}) | ||
f := v.(*fileMutex) | ||
|
||
if ro { | ||
f.RLock() | ||
} else { | ||
f.Lock() | ||
} | ||
|
||
atomic.AddInt32(&f.count, 1) | ||
} | ||
|
||
func releaseLock(key string, ro bool) { | ||
v, ok := fileMutexLocker.Load(key) | ||
if !ok { | ||
// return fmt.Errorf("panic error") | ||
} | ||
f := v.(*fileMutex) | ||
|
||
atomic.AddInt32(&f.count, -1) | ||
|
||
if ro { | ||
f.RUnlock() | ||
} else { | ||
f.Unlock() | ||
} | ||
|
||
if f.count < 1 { | ||
fileMutexLocker.Delete(key) | ||
} | ||
} | ||
|
||
// LocalStorage is one of the implementions of StorageDriver by locally. | ||
type localStorage struct { | ||
// baseDir is the dir that local storage driver will store content based on it. | ||
baseDir string | ||
} | ||
|
||
// NewLocalStorage performs initialization for LocalStorage and return a StorageDriver. | ||
func NewLocalStorage(config interface{}) (StorageDriver, error) { | ||
// type assertion for config | ||
cfg, ok := config.(*localStorage) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to parse config") | ||
} | ||
|
||
// prepare the base dir | ||
if !path.IsAbs(cfg.baseDir) { | ||
return nil, fmt.Errorf("not absolute path: %s", cfg.baseDir) | ||
} | ||
if err := util.CreateDirectory(cfg.baseDir); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &localStorage{ | ||
baseDir: cfg.baseDir, | ||
}, nil | ||
} | ||
|
||
// Get the content of key from storage and return in io stream. | ||
func (ls *localStorage) Get(raw *Raw, writer io.Writer) error { | ||
path, _, err := ls.statPath(raw.key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
getLock(getLockKey(path, raw.offset), true) | ||
defer releaseLock(getLockKey(path, raw.offset), true) | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
f.Seek(raw.offset, 0) | ||
if raw.length <= 0 { | ||
_, err = io.Copy(writer, f) | ||
} else { | ||
_, err = io.CopyN(writer, f, raw.length) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// GetBytes gets the content of key from storage and return in bytes. | ||
func (ls *localStorage) GetBytes(raw *Raw) (data []byte, err error) { | ||
path, _, err := ls.statPath(raw.key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
getLock(getLockKey(path, raw.offset), true) | ||
defer releaseLock(getLockKey(path, raw.offset), true) | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
f.Seek(raw.offset, 0) | ||
if raw.length <= 0 { | ||
data, err = ioutil.ReadAll(f) | ||
} else { | ||
data = make([]byte, raw.length) | ||
_, err = f.Read(data) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
// Put reads the content from reader and put it into storage. | ||
func (ls *localStorage) Put(raw *Raw, data io.Reader) error { | ||
path, err := ls.preparePath(raw.key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
getLock(getLockKey(path, raw.offset), false) | ||
defer releaseLock(getLockKey(path, raw.offset), false) | ||
|
||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
f.Seek(raw.offset, 0) | ||
if _, err = io.Copy(f, data); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// PutBytes puts the content of key from storage with bytes. | ||
func (ls *localStorage) PutBytes(raw *Raw, data []byte) error { | ||
path, err := ls.preparePath(raw.key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
getLock(getLockKey(path, raw.offset), false) | ||
defer releaseLock(getLockKey(path, raw.offset), false) | ||
|
||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
f.Seek(raw.offset, 0) | ||
if _, err := f.Write(data); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Stat determine whether the file exists. | ||
func (ls *localStorage) Stat(raw *Raw) (*StorageInfo, error) { | ||
path, fileInfo, err := ls.statPath(raw.key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sys, ok := util.GetSys(fileInfo) | ||
if !ok { | ||
return nil, fmt.Errorf("get create time error") | ||
} | ||
return &StorageInfo{ | ||
Path: path, | ||
Size: fileInfo.Size(), | ||
CreateTime: util.Ctime(sys), | ||
ModTime: fileInfo.ModTime(), | ||
}, nil | ||
} | ||
|
||
// Remove deletes a file or dir. | ||
func (ls *localStorage) Remove(raw *Raw) error { | ||
path, _, err := ls.statPath(raw.key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
getLock(getLockKey(path, raw.offset), false) | ||
defer releaseLock(getLockKey(path, raw.offset), false) | ||
|
||
if err := os.RemoveAll(path); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// helper function | ||
|
||
// preparePath gets the target path and creates the upper directory if it does not exist. | ||
func (ls *localStorage) preparePath(key string) (string, error) { | ||
dir := path.Join(ls.baseDir, getPrefix(key)) | ||
|
||
if err := util.CreateDirectory(dir); err != nil { | ||
return "", err | ||
} | ||
|
||
target := path.Join(dir, key) | ||
return target, nil | ||
} | ||
|
||
// statPath determines whether the target file exists and returns an fileMutex if so. | ||
func (ls *localStorage) statPath(key string) (string, os.FileInfo, error) { | ||
filePath := path.Join(ls.baseDir, getPrefix(key), key) | ||
f, err := os.Stat(filePath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return "", nil, ErrNotFound | ||
} | ||
return "", nil, err | ||
} | ||
|
||
return filePath, f, nil | ||
} | ||
|
||
func getLockKey(path string, offset int64) string { | ||
return fmt.Sprintf("%s%d%d", path, offset, time.Now().Unix()) | ||
} | ||
|
||
func getPrefix(str string) string { | ||
if len(str) > 3 { | ||
return string([]byte(str)[:3]) | ||
} | ||
return str | ||
} |
Oops, something went wrong.