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

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

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

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

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

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

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

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

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

35
	name string
36 37
}

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

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

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

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

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

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

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

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

97
	return nil
98 99 100 101 102 103
}

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

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

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

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

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

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

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	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
146 147 148 149 150 151 152 153 154 155 156 157
	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:
158
		ndir := NewDirectory(d.ctx, name, nd, d, d.dserv)
Jeromy's avatar
Jeromy committed
159 160 161
		d.childDirs[name] = ndir
		return ndir, nil
	case ufspb.Data_File:
162 163 164 165 166 167
		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
168 169 170 171 172 173 174 175 176 177
	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) {
178 179
	for _, lnk := range d.node.Links {
		if lnk.Name == name {
180
			return lnk.GetNode(d.ctx, d.dserv)
181 182 183
		}
	}

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

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

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

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

207
	return d.childNode(name)
208 209
}

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

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

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

	return out, nil
255 256 257 258
}

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

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

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

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

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

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

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

291 292 293 294 295 296 297 298
	delete(d.childDirs, name)
	delete(d.files, name)

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

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

Jeromy's avatar
Jeromy committed
304
	return nil
305 306
}

307 308 309 310 311
func (d *Directory) Flush() error {
	nd, err := d.flushCurrentNode()
	if err != nil {
		return err
	}
312

313
	return d.parent.closeChild(d.name, nd)
314 315
}

Jeromy's avatar
Jeromy committed
316
// AddChild adds the node 'nd' under this directory giving it the name 'name'
317
func (d *Directory) AddChild(name string, nd *dag.Node) error {
318 319
	d.lock.Lock()
	defer d.lock.Unlock()
320

Jeromy's avatar
Jeromy committed
321
	_, err := d.childUnsync(name)
322
	if err == nil {
323
		return ErrDirExists
324 325
	}

Jeromy's avatar
Jeromy committed
326 327 328 329 330
	_, err = d.dserv.Add(nd)
	if err != nil {
		return err
	}

331 332 333 334 335
	err = d.node.AddNodeLinkClean(name, nd)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
336 337
	d.modTime = time.Now()

Jeromy's avatar
Jeromy committed
338
	return nil
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 374 375 376 377 378
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
379 380 381 382 383 384 385 386 387 388
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
}

389
func (d *Directory) GetNode() (*dag.Node, error) {
390 391
	d.lock.Lock()
	defer d.lock.Unlock()
392 393 394 395 396 397

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

398 399 400
	return d.node, nil
}

401 402
func (d *Directory) Lock() {
	d.lock.Lock()
403 404
}

405 406
func (d *Directory) Unlock() {
	d.lock.Unlock()
407
}