Skip to content

Commit

Permalink
- Add NewUnixfsFileWithStat
Browse files Browse the repository at this point in the history
- Fix windows test
  • Loading branch information
gammazero committed Aug 6, 2024
1 parent 312a27f commit 7371b76
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/unixfs-file-cid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func runClient(ctx context.Context, h host.Host, c cid.Cid, targetPeer string) (
return nil, err
}

unixFSNode, err := unixfile.NewUnixfsFile(ctx, dserv, nd, nil)
unixFSNode, err := unixfile.NewUnixfsFile(ctx, dserv, nd)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions gateway/backend_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (bb *BlocksBackend) Get(ctx context.Context, path path.ImmutablePath, range
}

// This code path covers full graph, single file/directory, and range requests
f, err := ufile.NewUnixfsFile(ctx, bb.dagService, nd, nil)
f, err := ufile.NewUnixfsFile(ctx, bb.dagService, nd)
// Note: there is an assumption here that non-UnixFS dag-pb should not be returned which is currently valid
if err != nil {
return md, nil, err
Expand Down Expand Up @@ -197,7 +197,7 @@ func (bb *BlocksBackend) GetAll(ctx context.Context, path path.ImmutablePath) (C
}

// This code path covers full graph, single file/directory, and range requests
n, err := ufile.NewUnixfsFile(ctx, bb.dagService, nd, nil)
n, err := ufile.NewUnixfsFile(ctx, bb.dagService, nd)
if err != nil {
return md, nil, err
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (bb *BlocksBackend) Head(ctx context.Context, path path.ImmutablePath) (Con

// TODO: We're not handling non-UnixFS dag-pb. There's a bit of a discrepancy
// between what we want from a HEAD request and a Resolve request here and we're using this for both
fileNode, err := ufile.NewUnixfsFile(ctx, bb.dagService, nd, nil)
fileNode, err := ufile.NewUnixfsFile(ctx, bb.dagService, nd)
if err != nil {
return ContentPathMetadata{}, nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions gateway/backend_car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestCarBackendTar(t *testing.T) {
require.NoError(t, err)
nd, err := dsrv.Get(ctx, expected)
require.NoError(t, err)
expectedFile, err := unixfile.NewUnixfsFile(ctx, dsrv, nd, nil)
expectedFile, err := unixfile.NewUnixfsFile(ctx, dsrv, nd)
require.NoError(t, err)

expectedFileData, err := io.ReadAll(expectedFile.(files.File))
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestCarBackendTarAtEndOfPath(t *testing.T) {
require.NoError(t, err)
nd, err := dsrv.Get(ctx, expected)
require.NoError(t, err)
expectedFile, err := unixfile.NewUnixfsFile(ctx, dsrv, nd, nil)
expectedFile, err := unixfile.NewUnixfsFile(ctx, dsrv, nd)
require.NoError(t, err)

expectedFileData, err := io.ReadAll(expectedFile.(files.File))
Expand Down Expand Up @@ -511,7 +511,7 @@ func TestCarBackendGetFile(t *testing.T) {
dsrv := merkledag.NewDAGService(blockservice.New(robs, offline.Exchange(robs)))
fileRootNd, err := dsrv.Get(ctx, cid.MustParse("bafybeigcisqd7m5nf3qmuvjdbakl5bdnh4ocrmacaqkpuh77qjvggmt2sa"))
require.NoError(t, err)
uio, err := unixfile.NewUnixfsFile(ctx, dsrv, fileRootNd, nil)
uio, err := unixfile.NewUnixfsFile(ctx, dsrv, fileRootNd)
require.NoError(t, err)
f := uio.(files.File)
expectedFileData, err := io.ReadAll(f)
Expand Down Expand Up @@ -620,7 +620,7 @@ func TestCarBackendGetFileRangeRequest(t *testing.T) {
dsrv := merkledag.NewDAGService(blockservice.New(robs, offline.Exchange(robs)))
fileRootNd, err := dsrv.Get(ctx, cid.MustParse("bafybeigcisqd7m5nf3qmuvjdbakl5bdnh4ocrmacaqkpuh77qjvggmt2sa"))
require.NoError(t, err)
uio, err := unixfile.NewUnixfsFile(ctx, dsrv, fileRootNd, nil)
uio, err := unixfile.NewUnixfsFile(ctx, dsrv, fileRootNd)
require.NoError(t, err)
f := uio.(files.File)
_, err = f.Seek(int64(startIndex), io.SeekStart)
Expand Down Expand Up @@ -725,7 +725,7 @@ func TestCarBackendGetFileWithBadBlockReturned(t *testing.T) {
dsrv := merkledag.NewDAGService(blockservice.New(robs, offline.Exchange(robs)))
fileRootNd, err := dsrv.Get(ctx, cid.MustParse("bafybeigcisqd7m5nf3qmuvjdbakl5bdnh4ocrmacaqkpuh77qjvggmt2sa"))
require.NoError(t, err)
uio, err := unixfile.NewUnixfsFile(ctx, dsrv, fileRootNd, nil)
uio, err := unixfile.NewUnixfsFile(ctx, dsrv, fileRootNd)
require.NoError(t, err)
f := uio.(files.File)
expectedFileData, err := io.ReadAll(f)
Expand Down
8 changes: 6 additions & 2 deletions ipld/unixfs/file/unixfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (it *ufsIterator) Next() bool {
}

it.curName = l.Name
it.curFile, it.err = NewUnixfsFile(it.ctx, it.dserv, nd, nil)
it.curFile, it.err = NewUnixfsFile(it.ctx, it.dserv, nd)
return it.err == nil
}

Expand Down Expand Up @@ -183,7 +183,11 @@ func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd *dag.ProtoNode,
}, nil
}

func NewUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, stat os.FileInfo) (files.Node, error) {
func NewUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node) (files.Node, error) {
return NewUnixfsFileWithStat(ctx, dserv, nd, nil)
}

func NewUnixfsFileWithStat(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, stat os.FileInfo) (files.Node, error) {
switch dn := nd.(type) {
case *dag.ProtoNode:
fsn, err := ft.FSNodeFromBytes(dn.Data())
Expand Down
10 changes: 8 additions & 2 deletions tar/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,17 @@ func TestSymlinkWithModTime(t *testing.T) {

fi, err := os.Lstat(fp.Join(extractDir, "a"))
assert.NoError(t, err)
assert.Equal(t, add5(), fi.ModTime())
add5()
if runtime.GOOS != "windows" {
assert.Equal(t, tm, fi.ModTime())
}

fi, err = os.Lstat(fp.Join(extractDir, "file1-sl"))
assert.NoError(t, err)
assert.Equal(t, add5(), fi.ModTime())
add5()
if runtime.GOOS != "windows" {
assert.Equal(t, tm, fi.ModTime())
}
},
nil)
}
Expand Down

0 comments on commit 7371b76

Please sign in to comment.