You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/filesystemlayer/interface.go

61 lines
1.4 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package filesystemlayer
// 注意这个package并没有完整实现所有的文件操作使用内存作为存储时存在一些限制
// 目前只是服务于我另一个项目中的特定场景 https://github.com/q191201771/lal
var (
DefaultDiskFileSystemLayer IFileSystemLayer
DefaultMemoryFileSystemLayer IFileSystemLayer
)
type IFileSystemLayer interface {
Type() FslType
// 创建文件
// 原始语义:如果文件已经存在,原文件内容被清空
Create(name string) (IFile, error)
Rename(oldpath string, newpath string) error
MkdirAll(path string, perm uint32) error
Remove(name string) error
RemoveAll(path string) error
ReadFile(filename string) ([]byte, error)
WriteFile(filename string, data []byte, perm uint32) error
}
type IFile interface {
Write(b []byte) (n int, err error)
Close() error
}
type FslType int
const (
FslTypeDisk FslType = 1
FslTypeMemory = 2
)
func FslFactory(t FslType) IFileSystemLayer {
switch t {
case FslTypeDisk:
return &FslDisk{}
case FslTypeMemory:
return NewFslMemory()
}
return nil
}
func init() {
DefaultDiskFileSystemLayer = FslFactory(FslTypeDisk)
DefaultMemoryFileSystemLayer = FslFactory(FslTypeMemory)
}