dir.go 7.13 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
func (d *Directory) closeChild(name string, nd *dag.Node) error {
55
	mynd, err := d.closeChildUpdate(name, nd)
56 57 58 59
	if err != nil {
		return err
	}

60 61 62 63 64
	return d.parent.closeChild(d.name, mynd)
}

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

	err := d.updateChild(name, nd)
69
	if err != nil {
70
		return nil, err
71 72
	}

73 74 75 76 77 78 79 80 81 82
	return d.flushCurrentNode()
}

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

	return d.node.Copy(), nil
83 84 85 86
}

func (d *Directory) updateChild(name string, nd *dag.Node) error {
	err := d.node.RemoveNodeLink(name)
87 88 89 90 91 92 93 94 95
	if err != nil && err != dag.ErrNotFound {
		return err
	}

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

Jeromy's avatar
Jeromy committed
96 97
	d.modTime = time.Now()

98
	return nil
99 100 101 102 103 104
}

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

105 106 107
// 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
108 109 110 111 112 113 114 115 116 117 118 119
	nd, err := d.childFromDag(name)
	if err != nil {
		return nil, err
	}

	i, err := ft.FromBytes(nd.Data)
	if err != nil {
		return nil, err
	}

	switch i.GetType() {
	case ufspb.Data_Directory:
120
		ndir := NewDirectory(d.ctx, name, nd, d, d.dserv)
Jeromy's avatar
Jeromy committed
121 122 123
		d.childDirs[name] = ndir
		return ndir, nil
	case ufspb.Data_File:
124 125 126 127 128 129
		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
130 131 132 133 134 135 136
	case ufspb.Data_Metadata:
		return nil, ErrNotYetImplemented
	default:
		return nil, ErrInvalidChild
	}
}

Jeromy's avatar
Jeromy committed
137 138 139 140 141 142 143
// 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
144 145 146
// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
147 148
	for _, lnk := range d.node.Links {
		if lnk.Name == name {
149
			return lnk.GetNode(d.ctx, d.dserv)
150 151 152
		}
	}

Jeromy's avatar
Jeromy committed
153
	return nil, os.ErrNotExist
154 155
}

Jeromy's avatar
Jeromy committed
156 157
// 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
158
func (d *Directory) childUnsync(name string) (FSNode, error) {
159 160 161
	cdir, ok := d.childDirs[name]
	if ok {
		return cdir, nil
162
	}
163 164 165 166

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

169
	return d.childNode(name)
170 171
}

172 173 174 175 176 177 178
type NodeListing struct {
	Name string
	Type int
	Size int64
	Hash string
}

Jeromy's avatar
Jeromy committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
func (d *Directory) ListNames() []string {
	d.Lock()
	defer d.Unlock()

	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
199
	sort.Strings(out)
Jeromy's avatar
Jeromy committed
200 201 202 203

	return out
}

204
func (d *Directory) List() ([]NodeListing, error) {
205 206 207
	d.lock.Lock()
	defer d.lock.Unlock()

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	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)
239
	}
240 241

	return out, nil
242 243 244 245
}

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

Jeromy's avatar
Jeromy committed
248
	fsn, err := d.childUnsync(name)
249
	if err == nil {
Jeromy's avatar
Jeromy committed
250 251 252 253 254 255 256 257
		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)
		}
258 259 260
	}

	ndir := &dag.Node{Data: ft.FolderPBData()}
261 262 263 264 265 266

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

267 268 269 270 271
	err = d.node.AddNodeLinkClean(name, ndir)
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
272 273 274
	dirobj := NewDirectory(d.ctx, name, ndir, d, d.dserv)
	d.childDirs[name] = dirobj
	return dirobj, nil
275 276 277 278
}

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

281 282 283 284 285 286 287 288
	delete(d.childDirs, name)
	delete(d.files, name)

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

289 290 291 292 293
	_, err = d.dserv.Add(d.node)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
294
	return nil
295 296
}

297 298 299 300 301
func (d *Directory) Flush() error {
	nd, err := d.flushCurrentNode()
	if err != nil {
		return err
	}
302

303
	return d.parent.closeChild(d.name, nd)
304 305
}

Jeromy's avatar
Jeromy committed
306
// AddChild adds the node 'nd' under this directory giving it the name 'name'
307
func (d *Directory) AddChild(name string, nd *dag.Node) error {
308 309
	d.lock.Lock()
	defer d.lock.Unlock()
310

Jeromy's avatar
Jeromy committed
311
	_, err := d.childUnsync(name)
312
	if err == nil {
313
		return ErrDirExists
314 315
	}

Jeromy's avatar
Jeromy committed
316 317 318 319 320
	_, err = d.dserv.Add(nd)
	if err != nil {
		return err
	}

321 322 323 324 325
	err = d.node.AddNodeLinkClean(name, nd)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
326 327
	d.modTime = time.Now()

Jeromy's avatar
Jeromy committed
328
	return nil
329 330
}

331 332 333 334 335 336 337 338 339 340 341 342 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
func (d *Directory) sync() error {
	for name, dir := range d.childDirs {
		nd, err := dir.GetNode()
		if err != nil {
			return err
		}

		_, err = d.dserv.Add(nd)
		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.dserv.Add(nd)
		if err != nil {
			return err
		}

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

	return nil
}

Jeromy's avatar
Jeromy committed
369 370 371 372 373 374 375 376 377 378
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
}

379
func (d *Directory) GetNode() (*dag.Node, error) {
380 381
	d.lock.Lock()
	defer d.lock.Unlock()
382 383 384 385 386 387

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

Jeromy's avatar
Jeromy committed
388
	return d.node.Copy(), nil
389 390
}

391 392
func (d *Directory) Lock() {
	d.lock.Lock()
393 394
}

395 396
func (d *Directory) Unlock() {
	d.lock.Unlock()
397
}