dir.go 7.36 KB
Newer Older
1
package mfs
2 3 4 5 6

import (
	"errors"
	"fmt"
	"os"
Jeromy's avatar
Jeromy committed
7
	"path"
Jeromy's avatar
Jeromy committed
8
	"sort"
9
	"sync"
Jeromy's avatar
Jeromy committed
10
	"time"
Jeromy's avatar
Jeromy committed
11

12
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
13

14 15 16
	dag "github.com/ipfs/go-ipfs/merkledag"
	ft "github.com/ipfs/go-ipfs/unixfs"
	ufspb "github.com/ipfs/go-ipfs/unixfs/pb"
17 18
)

Jeromy's avatar
Jeromy committed
19 20
var ErrNotYetImplemented = errors.New("not yet implemented")
var ErrInvalidChild = errors.New("invalid child node")
21
var ErrDirExists = errors.New("directory already has entry by that name")
Jeromy's avatar
Jeromy committed
22

23
type Directory struct {
24
	dserv  dag.DAGService
25 26
	parent childCloser

27
	childDirs map[string]*Directory
28
	files     map[string]*File
29 30

	lock sync.Mutex
31
	node *dag.Node
32
	ctx  context.Context
33

Jeromy's avatar
Jeromy committed
34 35
	modTime time.Time

36
	name string
37 38
}

39
func NewDirectory(ctx context.Context, name string, node *dag.Node, parent childCloser, dserv dag.DAGService) *Directory {
40
	return &Directory{
41
		dserv:     dserv,
42
		ctx:       ctx,
43 44 45 46
		name:      name,
		node:      node,
		parent:    parent,
		childDirs: make(map[string]*Directory),
47
		files:     make(map[string]*File),
Jeromy's avatar
Jeromy committed
48
		modTime:   time.Now(),
49 50 51
	}
}

Jeromy's avatar
Jeromy committed
52
// closeChild updates the child by the given name to the dag node 'nd'
Jeromy's avatar
Jeromy committed
53
// and changes its own dag node
54 55
func (d *Directory) closeChild(name string, nd *dag.Node, sync bool) error {
	mynd, err := d.closeChildUpdate(name, nd, sync)
56 57 58 59
	if err != nil {
		return err
	}

60 61 62 63
	if sync {
		return d.parent.closeChild(d.name, mynd, true)
	}
	return nil
64 65 66
}

// closeChildUpdate is the portion of closeChild that needs to be locked around
67
func (d *Directory) closeChildUpdate(name string, nd *dag.Node, sync bool) (*dag.Node, error) {
68
	d.lock.Lock()
Jeromy's avatar
Jeromy committed
69
	defer d.lock.Unlock()
70 71

	err := d.updateChild(name, nd)
72
	if err != nil {
73
		return nil, err
74 75
	}

76 77 78 79
	if sync {
		return d.flushCurrentNode()
	}
	return nil, nil
80 81 82 83 84 85 86 87 88
}

func (d *Directory) flushCurrentNode() (*dag.Node, error) {
	_, err := d.dserv.Add(d.node)
	if err != nil {
		return nil, err
	}

	return d.node.Copy(), nil
89 90 91 92
}

func (d *Directory) updateChild(name string, nd *dag.Node) error {
	err := d.node.RemoveNodeLink(name)
93 94 95 96 97 98 99 100 101
	if err != nil && err != dag.ErrNotFound {
		return err
	}

	err = d.node.AddNodeLinkClean(name, nd)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
102 103
	d.modTime = time.Now()

104
	return nil
105 106 107 108 109 110
}

func (d *Directory) Type() NodeType {
	return TDir
}

111 112 113
// childNode returns a FSNode under this directory by the given name if it exists.
// it does *not* check the cached dirs and files
func (d *Directory) childNode(name string) (FSNode, error) {
Jeromy's avatar
Jeromy committed
114 115 116 117 118
	nd, err := d.childFromDag(name)
	if err != nil {
		return nil, err
	}

119 120 121 122 123
	return d.cacheNode(name, nd)
}

// cacheNode caches a node into d.childDirs or d.files and returns the FSNode.
func (d *Directory) cacheNode(name string, nd *dag.Node) (FSNode, error) {
124
	i, err := ft.FromBytes(nd.Data())
Jeromy's avatar
Jeromy committed
125 126 127 128 129 130
	if err != nil {
		return nil, err
	}

	switch i.GetType() {
	case ufspb.Data_Directory:
131
		ndir := NewDirectory(d.ctx, name, nd, d, d.dserv)
Jeromy's avatar
Jeromy committed
132 133
		d.childDirs[name] = ndir
		return ndir, nil
Jeromy's avatar
Jeromy committed
134
	case ufspb.Data_File, ufspb.Data_Raw, ufspb.Data_Symlink:
135 136 137 138 139 140
		nfi, err := NewFile(name, nd, d, d.dserv)
		if err != nil {
			return nil, err
		}
		d.files[name] = nfi
		return nfi, nil
Jeromy's avatar
Jeromy committed
141 142 143 144 145 146 147
	case ufspb.Data_Metadata:
		return nil, ErrNotYetImplemented
	default:
		return nil, ErrInvalidChild
	}
}

Jeromy's avatar
Jeromy committed
148 149 150 151 152 153 154
// Child returns the child of this directory by the given name
func (d *Directory) Child(name string) (FSNode, error) {
	d.lock.Lock()
	defer d.lock.Unlock()
	return d.childUnsync(name)
}

Jeromy's avatar
Jeromy committed
155 156 157
// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
158 159
	for _, lnk := range d.node.Links {
		if lnk.Name == name {
160
			return lnk.GetNode(d.ctx, d.dserv)
161 162 163
		}
	}

Jeromy's avatar
Jeromy committed
164
	return nil, os.ErrNotExist
165 166
}

Jeromy's avatar
Jeromy committed
167 168
// childUnsync returns the child under this directory by the given name
// without locking, useful for operations which already hold a lock
Jeromy's avatar
Jeromy committed
169
func (d *Directory) childUnsync(name string) (FSNode, error) {
170 171 172
	cdir, ok := d.childDirs[name]
	if ok {
		return cdir, nil
173
	}
174 175 176 177

	cfile, ok := d.files[name]
	if ok {
		return cfile, nil
178 179
	}

180
	return d.childNode(name)
181 182
}

183 184 185 186 187 188 189
type NodeListing struct {
	Name string
	Type int
	Size int64
	Hash string
}

Jeromy's avatar
Jeromy committed
190
func (d *Directory) ListNames() []string {
191 192
	d.lock.Lock()
	defer d.lock.Unlock()
Jeromy's avatar
Jeromy committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

	names := make(map[string]struct{})
	for n, _ := range d.childDirs {
		names[n] = struct{}{}
	}
	for n, _ := range d.files {
		names[n] = struct{}{}
	}

	for _, l := range d.node.Links {
		names[l.Name] = struct{}{}
	}

	var out []string
	for n, _ := range names {
		out = append(out, n)
	}
Jeromy's avatar
Jeromy committed
210
	sort.Strings(out)
Jeromy's avatar
Jeromy committed
211 212 213 214

	return out
}

215
func (d *Directory) List() ([]NodeListing, error) {
216 217 218
	d.lock.Lock()
	defer d.lock.Unlock()

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	var out []NodeListing
	for _, l := range d.node.Links {
		child := NodeListing{}
		child.Name = l.Name

		c, err := d.childUnsync(l.Name)
		if err != nil {
			return nil, err
		}

		child.Type = int(c.Type())
		if c, ok := c.(*File); ok {
			size, err := c.Size()
			if err != nil {
				return nil, err
			}
			child.Size = size
		}
		nd, err := c.GetNode()
		if err != nil {
			return nil, err
		}

		k, err := nd.Key()
		if err != nil {
			return nil, err
		}

		child.Hash = k.B58String()

		out = append(out, child)
250
	}
251 252

	return out, nil
253 254 255 256
}

func (d *Directory) Mkdir(name string) (*Directory, error) {
	d.lock.Lock()
Jeromy's avatar
Jeromy committed
257
	defer d.lock.Unlock()
258

Jeromy's avatar
Jeromy committed
259
	fsn, err := d.childUnsync(name)
260
	if err == nil {
Jeromy's avatar
Jeromy committed
261 262 263 264 265 266 267 268
		switch fsn := fsn.(type) {
		case *Directory:
			return fsn, os.ErrExist
		case *File:
			return nil, os.ErrExist
		default:
			return nil, fmt.Errorf("unrecognized type: %#v", fsn)
		}
269 270
	}

271 272
	ndir := new(dag.Node)
	ndir.SetData(ft.FolderPBData())
273 274 275 276 277 278

	_, err = d.dserv.Add(ndir)
	if err != nil {
		return nil, err
	}

279 280 281 282 283
	err = d.node.AddNodeLinkClean(name, ndir)
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
284 285 286
	dirobj := NewDirectory(d.ctx, name, ndir, d, d.dserv)
	d.childDirs[name] = dirobj
	return dirobj, nil
287 288 289 290
}

func (d *Directory) Unlink(name string) error {
	d.lock.Lock()
Jeromy's avatar
Jeromy committed
291 292
	defer d.lock.Unlock()

293 294 295 296 297 298 299 300
	delete(d.childDirs, name)
	delete(d.files, name)

	err := d.node.RemoveNodeLink(name)
	if err != nil {
		return err
	}

301 302 303 304 305
	_, err = d.dserv.Add(d.node)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
306
	return nil
307 308
}

309
func (d *Directory) Flush() error {
310
	d.lock.Lock()
311 312
	nd, err := d.flushCurrentNode()
	if err != nil {
313
		d.lock.Unlock()
314 315
		return err
	}
316
	d.lock.Unlock()
317

318
	return d.parent.closeChild(d.name, nd, true)
319 320
}

Jeromy's avatar
Jeromy committed
321
// AddChild adds the node 'nd' under this directory giving it the name 'name'
322
func (d *Directory) AddChild(name string, nd *dag.Node) error {
323 324
	d.lock.Lock()
	defer d.lock.Unlock()
325

Jeromy's avatar
Jeromy committed
326
	_, err := d.childUnsync(name)
327
	if err == nil {
328
		return ErrDirExists
329 330
	}

Jeromy's avatar
Jeromy committed
331 332 333 334 335
	_, err = d.dserv.Add(nd)
	if err != nil {
		return err
	}

336 337 338 339 340
	err = d.node.AddNodeLinkClean(name, nd)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
341
	d.modTime = time.Now()
Jeromy's avatar
Jeromy committed
342
	return nil
343 344
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
func (d *Directory) sync() error {
	for name, dir := range d.childDirs {
		nd, err := dir.GetNode()
		if err != nil {
			return err
		}

		err = d.updateChild(name, nd)
		if err != nil {
			return err
		}
	}

	for name, file := range d.files {
		nd, err := file.GetNode()
		if err != nil {
			return err
		}

		err = d.updateChild(name, nd)
		if err != nil {
			return err
		}
	}

	return nil
}

Jeromy's avatar
Jeromy committed
373 374 375 376 377 378 379 380 381 382
func (d *Directory) Path() string {
	cur := d
	var out string
	for cur != nil {
		out = path.Join(cur.name, out)
		cur = cur.parent.(*Directory)
	}
	return out
}

383
func (d *Directory) GetNode() (*dag.Node, error) {
384 385
	d.lock.Lock()
	defer d.lock.Unlock()
386 387 388 389 390 391

	err := d.sync()
	if err != nil {
		return nil, err
	}

392 393 394 395 396
	_, err = d.dserv.Add(d.node)
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
397
	return d.node.Copy(), nil
398
}