Skip to content

Commit

Permalink
Fix mfs touch and chmod
Browse files Browse the repository at this point in the history
Fixes #660
  • Loading branch information
gammazero committed Aug 20, 2024
1 parent f35ddcb commit 47dca25
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ipld/unixfs/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
// Common errors
var (
ErrMalformedFileFormat = errors.New("malformed data in file format")
ErrNotProtoNode = errors.New("expected a ProtoNode as internal node")
ErrUnrecognizedType = errors.New("unrecognized node type")
)

Expand Down Expand Up @@ -544,7 +545,7 @@ func ReadUnixFSNodeData(node ipld.Node) (data []byte, err error) {
func ExtractFSNode(node ipld.Node) (*FSNode, error) {
protoNode, ok := node.(*dag.ProtoNode)
if !ok {
return nil, errors.New("expected a ProtoNode as internal node")
return nil, ErrNotProtoNode
}

fsNode, err := FSNodeFromBytes(protoNode.Data())
Expand Down
43 changes: 29 additions & 14 deletions mfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ func (fi *File) Mode() (os.FileMode, error) {
defer fi.nodeLock.RUnlock()

nd, err := fi.GetNode()
if err == nil {
fsn, err := ft.ExtractFSNode(nd)
if err == nil {
return fsn.Mode() & 0xFFF, nil
}
if err != nil {
return 0, err
}

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

func (fi *File) SetMode(mode os.FileMode) error {
Expand All @@ -203,6 +203,11 @@ func (fi *File) SetMode(mode os.FileMode) error {

fsn, err := ft.ExtractFSNode(nd)
if err != nil {
if errors.Is(err, ft.ErrNotProtoNode) {
// Wrap raw node in protonode.
data := nd.RawData()
return fi.setNodeData(ft.FilePBDataWithStat(data, uint64(len(data)), mode, time.Time{}))
}
return err
}

Expand All @@ -221,14 +226,14 @@ func (fi *File) ModTime() (time.Time, error) {
defer fi.nodeLock.RUnlock()

nd, err := fi.GetNode()
if err == nil {
fsn, err := ft.ExtractFSNode(nd)
if err == nil {
return fsn.ModTime(), nil
}
if err != nil {
return time.Time{}, err
}

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

// SetModTime sets the files' last modification time
Expand All @@ -240,6 +245,11 @@ func (fi *File) SetModTime(ts time.Time) error {

fsn, err := ft.ExtractFSNode(nd)
if err != nil {
if errors.Is(err, ft.ErrNotProtoNode) {
// Wrap raw node in protonode.
data := nd.RawData()
return fi.setNodeData(ft.FilePBDataWithStat(data, uint64(len(data)), 0, ts))
}
return err
}

Expand Down Expand Up @@ -267,3 +277,8 @@ func (fi *File) setNodeData(data []byte) error {

return parent.updateChildEntry(child{name, fi.node})
}

func (fi *File) replaceRawNodeWithStatNode(data []byte, mode os.FileMode, mtime time.Time) error {
data = ft.FilePBDataWithStat(data, uint64(len(data)), mode, mtime)
return fi.setNodeData(data)
}

0 comments on commit 47dca25

Please sign in to comment.