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

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

	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11

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

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

21
type Directory struct {
22
	dserv  dag.DAGService
23 24
	parent childCloser

25
	childDirs map[string]*Directory
26
	files     map[string]*File
27 28

	lock sync.Mutex
29
	node *dag.Node
30
	ctx  context.Context
31

Jeromy's avatar
Jeromy committed
32 33
	modTime time.Time

34
	name string
35 36
}

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

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

58 59 60 61 62
	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) {
63
	d.lock.Lock()
Jeromy's avatar
Jeromy committed
64
	defer d.lock.Unlock()
65 66

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

71 72 73 74 75 76 77 78 79 80
	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
81 82 83 84
}

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

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

Jeromy's avatar
Jeromy committed
94 95
	d.modTime = time.Now()

96
	return nil
97 98 99 100 101 102
}

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

Jeromy's avatar
Jeromy committed
103
// childFile returns a file under this directory by the given name if it exists
104
func (d *Directory) childFile(name string) (*File, error) {
105 106 107 108 109
	fi, ok := d.files[name]
	if ok {
		return fi, nil
	}

110
	fsn, err := d.childNode(name)
Jeromy's avatar
Jeromy committed
111 112 113
	if err != nil {
		return nil, err
	}
114

115 116
	if fi, ok := fsn.(*File); ok {
		return fi, nil
117
	}
118 119

	return nil, fmt.Errorf("%s is not a file", name)
120 121
}

Jeromy's avatar
Jeromy committed
122 123
// childDir returns a directory under this directory by the given name if it
// exists.
124 125 126 127 128 129
func (d *Directory) childDir(name string) (*Directory, error) {
	dir, ok := d.childDirs[name]
	if ok {
		return dir, nil
	}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	fsn, err := d.childNode(name)
	if err != nil {
		return nil, err
	}

	if dir, ok := fsn.(*Directory); ok {
		return dir, nil
	}

	return nil, fmt.Errorf("%s is not a directory", name)
}

// 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
145 146 147 148 149 150 151 152 153 154 155 156
	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:
157
		ndir := NewDirectory(d.ctx, name, nd, d, d.dserv)
Jeromy's avatar
Jeromy committed
158 159 160
		d.childDirs[name] = ndir
		return ndir, nil
	case ufspb.Data_File:
161 162 163 164 165 166
		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
167 168 169 170 171 172 173 174 175 176
	case ufspb.Data_Metadata:
		return nil, ErrNotYetImplemented
	default:
		return nil, ErrInvalidChild
	}
}

// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
177 178
	for _, lnk := range d.node.Links {
		if lnk.Name == name {
179
			return lnk.GetNode(d.ctx, d.dserv)
180 181 182
		}
	}

Jeromy's avatar
Jeromy committed
183
	return nil, os.ErrNotExist
184 185
}

Jeromy's avatar
Jeromy committed
186
// Child returns the child of this directory by the given name
187 188 189
func (d *Directory) Child(name string) (FSNode, error) {
	d.lock.Lock()
	defer d.lock.Unlock()
Jeromy's avatar
Jeromy committed
190 191 192
	return d.childUnsync(name)
}

Jeromy's avatar
Jeromy committed
193 194
// 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
195
func (d *Directory) childUnsync(name string) (FSNode, error) {
196 197 198
	cdir, ok := d.childDirs[name]
	if ok {
		return cdir, nil
199
	}
200 201 202 203

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

206
	return d.childNode(name)
207 208
}

209 210 211 212 213 214 215 216
type NodeListing struct {
	Name string
	Type int
	Size int64
	Hash string
}

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

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 250
	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)
251
	}
252 253

	return out, nil
254 255 256 257
}

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

	_, err := d.childDir(name)
	if err == nil {
Jeromy's avatar
Jeromy committed
262
		return nil, os.ErrExist
263 264 265
	}
	_, err = d.childFile(name)
	if err == nil {
Jeromy's avatar
Jeromy committed
266
		return nil, os.ErrExist
267 268 269
	}

	ndir := &dag.Node{Data: ft.FolderPBData()}
270 271 272 273 274 275

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

276 277 278 279 280
	err = d.node.AddNodeLinkClean(name, ndir)
	if err != nil {
		return nil, err
	}

281
	err = d.flushUp()
282 283 284 285
	if err != nil {
		return nil, err
	}

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

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

295 296 297 298 299 300 301 302
	delete(d.childDirs, name)
	delete(d.files, name)

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

303 304 305 306 307
	return d.flushUp()
}

func (d *Directory) flushUp() error {

308 309 310
	return d.parent.closeChild(d.name, d.node)
}

Jeromy's avatar
Jeromy committed
311
// AddChild adds the node 'nd' under this directory giving it the name 'name'
312
func (d *Directory) AddChild(name string, nd *dag.Node) error {
313 314
	d.lock.Lock()
	defer d.lock.Unlock()
315

Jeromy's avatar
Jeromy committed
316
	_, err := d.childUnsync(name)
317
	if err == nil {
318
		return ErrDirExists
319 320
	}

Jeromy's avatar
Jeromy committed
321 322 323 324 325
	_, err = d.dserv.Add(nd)
	if err != nil {
		return err
	}

326 327 328 329 330
	err = d.node.AddNodeLinkClean(name, nd)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
331 332
	d.modTime = time.Now()

Jeromy's avatar
Jeromy committed
333
	return nil
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 369 370 371 372 373
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
}

374
func (d *Directory) GetNode() (*dag.Node, error) {
375 376
	d.lock.Lock()
	defer d.lock.Unlock()
377 378 379 380 381 382

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

383 384 385
	return d.node, nil
}

386 387
func (d *Directory) Lock() {
	d.lock.Lock()
388 389
}

390 391
func (d *Directory) Unlock() {
	d.lock.Unlock()
392
}