Exemple #1
0
func (m *FileSystem) copyDirectoryDocument(d *Directory, newprefix, oldprefix, newname string, c *mgo.Collection) error {
	// update parent path prefix with new prefix
	_parent_path := d.Header.Parent
	_parent_path = strings.Replace(_parent_path, oldprefix, newprefix, 1)

	// update header info
	id, err := filesystem.NewNodeID()
	if err != nil {
		return err
	}

	d.Header.Parent = _parent_path
	if newname != "" {
		err = filesystem.ValidateDirName(newname)
		if err != nil {
			return err
		}
		d.Header.Name = newname
	}
	d.Header.Created = filesystem.FormatDatetime(time.Now())
	d.Id = id
	// save to mongodb
	err = c.Insert(&d)
	if err != nil {
		return err
	}

	return nil
}
Exemple #2
0
func (m *FileSystem) NewDir(p, db string) error {
	// check path
	p = path.Clean(p)
	if p == "/" {
		err := errors.New("root directory already exists")
		return err
	}
	_name := path.Base(p)
	_parent := path.Dir(p)
	err := filesystem.ValidateDirName(_name)
	if err != nil {
		return err
	}
	// check if parent directory exists
	q := m.findPathQuery(_parent)

	// get collection
	c := m.getBFSCollection(db)
	var _parentdir Directory
	// find record
	err = c.Find(q).One(&_parentdir)
	if err != nil {
		return err
	}
	if _parentdir.Header.Type != "Directory" {
		err = fmt.Errorf("directory '%s' couldn't be created: destination isn't a directory.", p)
		return err
	}
	// check if name already taken
	q = m.findPathQuery(p)
	_count, err := c.Find(q).Count()
	if err != nil {
		return err
	}
	if _count > 0 {
		err = fmt.Errorf("directory '%s' already exists", p)
		return err
	}

	// create directory
	id, err := filesystem.NewNodeID()
	if err != nil {
		return err
	}
	dt := filesystem.FormatDatetime(time.Now())
	h := NodeHeader{_name, "Directory", false, dt, _parent}
	_dir := Directory{h, id}
	// insert node into mongodb
	err = c.Insert(&_dir)
	if err != nil {
		return err
	}

	return nil
}
Exemple #3
0
func (m *FileSystem) Rename(p, newname, db string) error {
	// check path
	p = path.Clean(p)
	if p == "/" {
		err := errors.New("root directory cannot be renamed.")
		return err
	}

	// get collection
	c := m.getBFSCollection(db)

	// get file or directory if it exists
	q := m.findPathQuery(p)
	var ri SimpleResultItem
	err := c.Find(q).One(&ri)
	if err != nil {
		return err
	}

	if ri.Header.Type == "Directory" {
		// check if name is valid
		if err = filesystem.ValidateDirName(newname); err != nil {
			return err
		}
		// check if name isn't already in use
		np := path.Join(path.Dir(p), newname)
		q = m.findPathQuery(np)
		_count, err := c.Find(q).Count()
		if err != nil {
			return err
		}
		if _count > 0 {
			err := fmt.Errorf("directory '%s' already exists", np)
			return err
		}
		// get affected parent directories
		q = m.findAllChildrenQuery(p)
		var _dirs []string
		err = c.Find(q).Distinct("__header__.parent", &_dirs)
		if err != nil {
			return err
		}
		for _, item := range _dirs {
			newparent := strings.Replace(item, p, np, 1)
			q = bson.M{"__header__.parent": item}
			uq := bson.M{"$set": bson.M{"__header__.parent": newparent}}
			_, e := c.UpdateAll(q, uq)
			if e != nil {
				return e
			}
		}
		// rename directory by updating field
		q = bson.M{"$set": bson.M{"__header__.name": newname}}
		err = c.UpdateId(ri.Id, q)
		if err != nil {
			return err
		}

	} else {
		// check if name is valid
		if err = filesystem.ValidateFileName(newname); err != nil {
			return err
		}
		// check if name isn't already in use
		np := path.Join(path.Dir(p), newname)
		q = m.findPathQuery(np)
		_count, e := c.Find(q).Count()
		if e != nil {
			return e
		}
		if _count > 0 {
			err = fmt.Errorf("file '%s' already exists", np)
			return err
		}
		// rename file by updating field
		q = bson.M{"$set": bson.M{"__header__.name": newname}}
		err = c.UpdateId(ri.Id, q)
		if err != nil {
			return err
		}
	}

	return nil
}