forked from photon-storage/go-ipfs-car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.go
189 lines (169 loc) · 4.02 KB
/
importer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package car
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-cidutil"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
ipld "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/kubo/core/coreiface"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/ipfs/kubo/core/coreunix"
exampleds "github.com/ipfs/go-datastore/examples"
)
// DataImporter creates a new importer that imports data (can be byte slice,
// io.Reader or path from local file system) into in-memory dag service.
type DataImporter struct {
bstore blockstore.Blockstore
dagServ ipld.DAGService
}
// NewDataImporter creates a new DataImporter.
func NewDataImporter() *DataImporter {
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
return &DataImporter{
bstore: bstore,
dagServ: merkledag.NewDAGService(
blockservice.New(bstore, newNoopExchg()),
),
}
}
func NewDataImporterDisk(path string) (*DataImporter, error) {
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(path, 0755)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
dstore, err := exampleds.NewDatastore(path)
if err != nil {
return nil, err
}
bstore := blockstore.NewBlockstore(dssync.MutexWrap(dstore))
return &DataImporter{
bstore: bstore,
dagServ: merkledag.NewDAGService(
blockservice.New(bstore, newNoopExchg()),
),
}, nil
}
// Import imports the given input.
func (di *DataImporter) Import(
ctx context.Context,
input any,
opts ...ImportOption,
) (cid.Cid, error) {
// Build options from defaults.
ioptions, err := buildImportOptions(opts...)
if err != nil {
return cid.Undef, err
}
// Build CID builder.
prefix, err := merkledag.PrefixForCidVersion(ioptions.cidVersion)
if err != nil {
return cid.Undef, err
}
prefix.MhType = ioptions.mhType
prefix.MhLength = -1
var target files.Node
var path string
switch v := input.(type) {
case string:
var err error
if target, err = newFsPath(
v,
ioptions.ignoreFile,
ioptions.ignoreRules,
ioptions.includeHiddenFiles,
); err != nil {
return cid.Undef, err
}
path = v
case []byte:
target = files.NewBytesFile(v)
case io.Reader:
target = files.NewReaderFile(v)
}
adder, err := coreunix.NewAdder(ctx, nil, nil, di.dagServ)
if err != nil {
return cid.Undef, err
}
adder.CidBuilder = prefix
if ioptions.inline && ioptions.inlineLimit > 0 {
adder.CidBuilder = cidutil.InlineBuilder{
Builder: prefix,
Limit: ioptions.inlineLimit,
}
}
adder.RawLeaves = ioptions.rawLeaves
adder.Chunker = ioptions.chunker
if ioptions.layout == options.TrickleLayout {
adder.Trickle = true
}
adder.Pin = false
if ioptions.out != nil {
ch := make(chan interface{}, 8)
adder.Progress = true
adder.Out = ch
defer close(ch)
go func() {
defer close(ioptions.out)
_, isDir := target.(files.Directory)
for v := range ch {
ev, ok := v.(*coreiface.AddEvent)
if !ok {
continue
}
if ev.Path.String() == "" {
continue
}
name := ev.Name
if !isDir && path != "" {
name = path
} else {
name = filepath.Join(path, ev.Name)
}
ioptions.out <- &ImportEvent{
Name: name,
CID: ev.Path.RootCid(),
Bytes: ev.Bytes,
Size: ev.Size,
}
}
}()
}
nd, err := adder.AddAllAndPin(ctx, target)
if err != nil {
return cid.Undef, err
}
return nd.Cid(), nil
}
func (di *DataImporter) Blockstore() blockstore.Blockstore {
return di.bstore
}
func newFsPath(
path string,
ignoreFile string,
ignoreRules []string,
includeHiddenFiles bool,
) (files.Node, error) {
stat, err := os.Lstat(path)
if err != nil {
return nil, err
}
filter, err := files.NewFilter(ignoreFile, ignoreRules, includeHiddenFiles)
if err != nil {
return nil, err
}
return files.NewSerialFileWithFilter(path, filter, stat)
}