Пример #1
0
// CreateFile creates a File in the provided Directory
func CreateFile(d dir.Directory, name string) *File {
	f := &File{
		uuid: uuid.NewV4(),
		name: name,
	}
	if d != nil {
		f.directory = d
		err := d.AttachFile(f)
		if err != nil {
			// TODO: decide how to handle err != nil; we would like CreateFile to be chainable, hence not returning err
			return nil
		}
	}
	return f
}
Пример #2
0
// AttachDirectory adds a dir.Directory to this Directory
// You likely would use the .Create() or .CreateDirectory() method instead, if available.
func (d *Directory) AttachDirectory(dirIn dir.Directory) error {
	d.Lock()
	defer d.Unlock()
	for _, child := range d.children {
		if child == dirIn {
			return dir.ErrExists
		}
	}
	err := dirIn.SetRoot(d.root)
	if err != nil {
		return err
	}
	d.children = append(d.children, dirIn)
	return nil
}
Пример #3
0
// CreateDirectory creates a sub-Directory of the provided Directory
func CreateDirectory(d dir.Directory, name string) *Directory {
	subdir := &Directory{
		uuid: uuid.NewV4(),
		name: name,
	}
	if d != nil {
		subdir.root = d.Root()
		subdir.parent = d
		err := d.AttachDirectory(subdir)
		if err != nil {
			// TODO: decide how to handle err != nil; we would like CreateDirectory to be chainable, hence not returning err
			return nil
		}
	} else {
		subdir.root = subdir
	}
	return subdir
}