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 f4259d6
Show file tree
Hide file tree
Showing 2 changed files with 26 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

Check warning on line 548 in ipld/unixfs/unixfs.go

View check run for this annotation

Codecov / codecov/patch

ipld/unixfs/unixfs.go#L548

Added line #L548 was not covered by tests
}

fsNode, err := FSNodeFromBytes(protoNode.Data())
Expand Down
38 changes: 24 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
}

Check warning on line 190 in mfs/file.go

View check run for this annotation

Codecov / codecov/patch

mfs/file.go#L189-L190

Added lines #L189 - L190 were not covered by tests

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

Check warning on line 194 in mfs/file.go

View check run for this annotation

Codecov / codecov/patch

mfs/file.go#L193-L194

Added lines #L193 - L194 were not covered by tests
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

Check warning on line 211 in mfs/file.go

View check run for this annotation

Codecov / codecov/patch

mfs/file.go#L206-L211

Added lines #L206 - L211 were not covered by tests
}

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
}

Check warning on line 231 in mfs/file.go

View check run for this annotation

Codecov / codecov/patch

mfs/file.go#L230-L231

Added lines #L230 - L231 were not covered by tests

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

Check warning on line 235 in mfs/file.go

View check run for this annotation

Codecov / codecov/patch

mfs/file.go#L234-L235

Added lines #L234 - L235 were not covered by tests
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

Check warning on line 253 in mfs/file.go

View check run for this annotation

Codecov / codecov/patch

mfs/file.go#L248-L253

Added lines #L248 - L253 were not covered by tests
}

Expand Down

0 comments on commit f4259d6

Please sign in to comment.