一起来读源码232-Ipfs 第三方库:go-mfs

    技术2022-07-15  68

    摘要:

    文件/目录的创建,链接和发布

    详情:

    go-mfs

    知识点:

    io.WriteAt(p []byte, off int64) (n int, err error)

         WriteAt 从 p 中将 len(p) 个字节写入到偏移量 off 处的基本数据流中。它返回从 p 中被写入的字节数

         n(0 <= n <= len(p))以及任何遇到的引起写入提前停止的错误。若 WriteAt 返回的 n < len(p),

         它就必须返回一个非nil的错误。若 WriteAt 按查找偏移量写入到目标中,WriteAt 应当既不影响基本查找偏移量

         也不被它所影响。若区域没有重叠,WriteAt 的客户端可对相同的目标并行执行 WriteAt 调用

    定义类型:

    type PubFunc func(context.Context, cid.Cid) error //发布函数

     

    定义全局函数:

    func NewDirectory(ctx context.Context, name string, node ipld.Node, parent parent, dserv ipld.DAGService

        ) (*Directory, error) //新建目录(上下文,目录名,节点,父类, dagservice)(目录,err)

    func NewFile(name string, node ipld.Node, parent Parent, dserv ipld.DAGService

        )(*File, error) //新建文件(文件名, 节点, 父类, DAGService)(文件, err)

    func Mv(r *Root, src, dst string) error //将src移动到dst,如果dst下已存在src文件,取消已有文件的链接,

        如果已存在src目录,就放到src目录的子目录,即 /dst/src/src

    func PutNode(r *Root, path string, nd ipld.Node) error //放置节点

    func Mkdir(r *Root, pth string, opts MkdirOpts) error //创建目录(root目录节点, 路径, 创建选项)

    func Lookup(r *Root, path string) (FSNode, error) //遍历目录/文件,返回节点

    func DirLookup(d *Directory, pth string) (FSNode, error) //遍历目录,返回节点

    func FlushPath(ctx context.Context, rt *Root, pth string) (ipld.Node, error) //刷入目录/文件

    func NewRepublisher(ctx context.Context, pf PubFunc, tshort, tlong time.Duration) *Republisher //new一个发布器

     

    定义接口:

    type FSNode interface { //节点

        GetNode()(ipld.Node, error) //拿节点

        Flush() error //刷入磁盘

        Type() NodeType //类型

    }

    type parent interface { //父类

        updateChildEntry(c child) error //更新子类

    }

    type DAGService interface {

        NodeGetter

        NodeAdder

        Remove(context.Context, cid.Cid) error

        RemoveMany(context.Context, []cid.Cid) error

    }

    type FileDescriptor interface { //文件描述器

        io.Reader //读入器

        CtxReadFull(context.Context, []byte) (int, error) //全部读

        io.Writer //写入器

        io.WriterAt //全写入

        io.Closer //读写关闭器

        io.Seeker //索引器

        Truncate(int64) error //截断

        Size() (int64, error) //尺寸

        Flush() error //刷入

    }

    定义类:

    type child struct {

        Name string //子类名

        Node ipld.Node //子类节点

    }

    type Directory struct { //目录

        inode //

        entriesCache map[string]FSNode //缓存

        lock sync.Mutex //原子锁

        ctx context.Context //上下文

        unixfsDir uio.Directory //

        modTime time.Time //修改时间

    }

    func (d *Directory) GetCidBuilder() cid.Builder //获得自描述地址构建器

    func (d *Directory SetCidBuilder(b cid.Builder) //设置自描述地址构建器

    func (d *Directory) Child(name string) (FSNode, error) //返回缓存的子(目录/文件)节点

    func (d *Directory) Uncache(name string) //取消对name的缓存

    func (d *Directory) ListNames(ctx context.Context)([]string, error) //返回名称列表

    func (d *Directory) List(ctx context.Context) ([]NodeListing, error) //返回节点列表

    func (d *Directory) ForEachEntry(ctx context.Context, f func(NodeListing) error) error //遍历缓存

    func (d *Directory) Mkdir(name string) (*Directory, error) //创建目录(不存在就创建,已存在就返回,name是文件就返回nil)

    func (d *Directory) Unlink(name string) error //取消链接

    func (d *Directory) Flush() error //刷入

    func (d *Directory) AddChild(name string, nd ipld.Node) error //添加子节点

    func (d *Directory) Path() string //路径

    func (d *Directory) GetNode() (ipld.Node, error) //以复制的方式拿一个节点

    type NodeListing struct{ //节点列表条目

        Name string //名字

        Type int //类型

        Size int64 //大小

        Hash string //hash

    }

    type File struct { //文件

        inode //继承自节点

        desclock sync.RWMutex //读写锁

        node ipld.Node //根节点

        nodeLock sync.RWMutex //节点读写锁

        RawLeaves bool //

    }

    func (fi *File) Open(flags Flags) (_ FileDescriptor, _retErr error)  //打开文件,返回文件描述 flags:读写标记

    func (fi *File) Size() (int64, error)                                                   //返回文件尺寸(单位:)

    func (fi *File) GetNode() (ipld.Node, error)                                    //拿文件节点

    func (fi *File) Flush() error                                                             //刷入数据

    func (fi *File) Sync() error                                                              //同步(此处没做处理)

    func (fi *File) Type() NodeType                                                     //返回

    type MkdirOpts struct { //创建目录选项     Mkparents  bool           //是否创建父目录     Flush      bool               //是否刷入     CidBuilder cid.Builder  //cid构建器 }

    type Flags struct { //标记     Read  bool  //读     Write bool   //写     Sync  bool  //同步 }

    type Republisher struct { //发布器     TimeoutLong  time.Duration                //长超时     TimeoutShort time.Duration                //短超时     RetryTimeout time.Duration                //重试超时     pubfunc      PubFunc                          //发布函数

        update           chan cid.Cid                  //待更新的cid管道,容量1     immediatePublish chan chan struct{} //需要立即发布的管道,无容量

        ctx    context.Context                          //上下文     cancel func()                                       //取消函数 }

    func (rp *Republisher) WaitPub(ctx context.Context) error //等待发布

    func (rp *Republisher) Close() error //关闭发布器

    func (rp *Republisher) Update(c cid.Cid) //将cid放入update管道发布

    func (rp *Republisher) Run(lastPublished cid.Cid) //不停地从update管道拿cid发布

    Processed: 0.012, SQL: 9