Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
3831428e
Commit
3831428e
authored
Mar 06, 2015
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring unixfs node and importer/helpers to match
parent
a3c28a1a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
21 deletions
+56
-21
format.go
format.go
+50
-16
format_test.go
format_test.go
+6
-5
No files found.
format.go
View file @
3831428e
...
...
@@ -9,6 +9,13 @@ import (
pb
"github.com/jbenet/go-ipfs/unixfs/pb"
)
const
(
TRaw
=
pb
.
Data_Raw
TFile
=
pb
.
Data_File
TDirectory
=
pb
.
Data_Directory
TMetadata
=
pb
.
Data_Metadata
)
var
ErrMalformedFileFormat
=
errors
.
New
(
"malformed data in file format"
)
var
ErrInvalidDirLocation
=
errors
.
New
(
"found directory node in unexpected place"
)
var
ErrUnrecognizedType
=
errors
.
New
(
"unrecognized node type"
)
...
...
@@ -98,33 +105,60 @@ func DataSize(data []byte) (uint64, error) {
}
}
type
MultiBlock
struct
{
Data
[]
byte
type
FSNode
struct
{
Data
[]
byte
// total data size for each child
blocksizes
[]
uint64
subtotal
uint64
// running sum of blocksizes
subtotal
uint64
// node type of this node
Type
pb
.
Data_DataType
}
func
FSNodeFromBytes
(
b
[]
byte
)
(
*
FSNode
,
error
)
{
pbn
:=
new
(
pb
.
Data
)
err
:=
proto
.
Unmarshal
(
b
,
pbn
)
if
err
!=
nil
{
return
nil
,
err
}
n
:=
new
(
FSNode
)
n
.
Data
=
pbn
.
Data
n
.
blocksizes
=
pbn
.
Blocksizes
n
.
subtotal
=
pbn
.
GetFilesize
()
-
uint64
(
len
(
n
.
Data
))
n
.
Type
=
pbn
.
GetType
()
return
n
,
nil
}
// AddBlockSize adds the size of the next child block of this node
func
(
n
*
FSNode
)
AddBlockSize
(
s
uint64
)
{
n
.
subtotal
+=
s
n
.
blocksizes
=
append
(
n
.
blocksizes
,
s
)
}
func
(
mb
*
MultiBlock
)
Add
BlockSize
(
s
u
int
64
)
{
mb
.
subtotal
+
=
s
mb
.
blocksizes
=
append
(
mb
.
blocksizes
,
s
)
func
(
n
*
FSNode
)
Remove
BlockSize
(
i
int
)
{
n
.
subtotal
-
=
n
.
blocksizes
[
i
]
n
.
blocksizes
=
append
(
n
.
blocksizes
[
:
i
],
n
.
blocksizes
[
i
+
1
:
]
...
)
}
func
(
mb
*
MultiBlock
)
GetBytes
()
([]
byte
,
error
)
{
func
(
n
*
FSNode
)
GetBytes
()
([]
byte
,
error
)
{
pbn
:=
new
(
pb
.
Data
)
t
:=
pb
.
Data_File
pbn
.
Type
=
&
t
pbn
.
Filesize
=
proto
.
Uint64
(
uint64
(
len
(
mb
.
Data
))
+
mb
.
subtotal
)
pbn
.
Blocksizes
=
mb
.
blocksizes
pbn
.
Data
=
mb
.
Data
pbn
.
Type
=
&
n
.
Type
pbn
.
Filesize
=
proto
.
Uint64
(
uint64
(
len
(
n
.
Data
))
+
n
.
subtotal
)
pbn
.
Blocksizes
=
n
.
blocksizes
pbn
.
Data
=
n
.
Data
return
proto
.
Marshal
(
pbn
)
}
func
(
mb
*
MultiBlock
)
FileSize
()
uint64
{
return
uint64
(
len
(
mb
.
Data
))
+
mb
.
subtotal
func
(
n
*
FSNode
)
FileSize
()
uint64
{
return
uint64
(
len
(
n
.
Data
))
+
n
.
subtotal
}
func
(
mb
*
MultiBlock
)
NumChildren
()
int
{
return
len
(
mb
.
blocksizes
)
func
(
n
*
FSNode
)
NumChildren
()
int
{
return
len
(
n
.
blocksizes
)
}
type
Metadata
struct
{
...
...
format_test.go
View file @
3831428e
...
...
@@ -7,15 +7,16 @@ import (
pb
"github.com/jbenet/go-ipfs/unixfs/pb"
)
func
TestMultiBlock
(
t
*
testing
.
T
)
{
mbf
:=
new
(
MultiBlock
)
func
TestFSNode
(
t
*
testing
.
T
)
{
fsn
:=
new
(
FSNode
)
fsn
.
Type
=
TFile
for
i
:=
0
;
i
<
15
;
i
++
{
mbf
.
AddBlockSize
(
100
)
fsn
.
AddBlockSize
(
100
)
}
mbf
.
Data
=
make
([]
byte
,
128
)
fsn
.
Data
=
make
([]
byte
,
128
)
b
,
err
:=
mbf
.
GetBytes
()
b
,
err
:=
fsn
.
GetBytes
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment