[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

initial support for setting/updating file modes and modification times #93

Closed
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
support setting mode and modtime on directories
  • Loading branch information
kstuart committed Oct 25, 2022
commit e9330ff61f0702b53ca920f29ca7431ea73d052b
66 changes: 66 additions & 0 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path"
"sync"
"time"

cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
Expand Down Expand Up @@ -420,3 +421,68 @@ func (d *Directory) GetNode() (ipld.Node, error) {

return nd.Copy(), err
}

func (d *Directory) SetMode(mode os.FileMode) error {
nd, err := d.GetNode()
if err != nil {
return err
}

fsn, err := ft.ExtractFSNode(nd)
if err != nil {
return err
}

fsn.SetMode(mode)
data, err := fsn.GetBytes()
if err != nil {
return err
}

return d.setNodeData(data, nd.Links())
}

func (d *Directory) SetModTime(ts time.Time) error {
nd, err := d.GetNode()
if err != nil {
return err
}

fsn, err := ft.ExtractFSNode(nd)
if err != nil {
return err
}

fsn.SetModTime(ts)
data, err := fsn.GetBytes()
if err != nil {
return err
}

return d.setNodeData(data, nd.Links())
}

func (d *Directory) setNodeData(data []byte, links []*ipld.Link) error {
nd := dag.NodeWithData(data)
nd.SetLinks(links)

err := d.dagService.Add(d.ctx, nd)
if err != nil {
return err
}

err = d.parent.updateChildEntry(child{d.name, nd})
if err != nil {
return err
}

d.lock.Lock()
defer d.lock.Unlock()
db, err := uio.NewDirectoryFromNode(d.dagService, nd)
if err != nil {
return err
}
d.unixfsDir = db

return nil
}