コード例 #1
0
ファイル: objects.go プロジェクト: alankm/vimages
func (f *folder) Exec(args ...string) interface{} {

	records, err := f.children()
	if err != nil {
		return err
	}

	exec := make(map[string]object)
	for _, rec := range records {
		vals := strings.Split(rec.path, "/")
		path := vals[len(vals)-1]
		rules, err := privileges.NewRules(rec.owner, rec.group, rec.rules)
		if err != nil {
			return err
		}
		if rec.dir {
			exec[path] = newFolder(f.tx, f.s, rec.path, rules)
		} else {
			exec[path] = newImage(f.tx, f.s, rec.path, rec.chk, rules, rec)
		}
	}

	return exec

}
コード例 #2
0
ファイル: objects.go プロジェクト: alankm/vimages
func getObject(tx *sql.Tx, s *privileges.Session, path string) (object, error) {
	rules, _ := privileges.NewRules("root", "root", "0755")
	var obj object = newFolder(tx, s, "", rules)
	if len(path) < 2 {
		return obj, nil
	}
	route := strings.Split(path[1:], "/")
	for _, o := range route {
		val, err := s.Exec(obj)
		if err != nil {
			return nil, err
		}

		children, ok := val.(map[string]object)
		if !ok {
			return nil, errInternal
		}

		obj = children[o]
		if obj == nil {
			return nil, errExist
		}
	}

	return obj, nil

}
コード例 #3
0
ファイル: objects.go プロジェクト: alankm/vimages
func (f *folder) Read(args ...string) interface{} {

	if !f.s.CanExec(f) {
		return errDenied
	}

	ret, err := f.children()
	if err != nil {
		return err
	}

	var names []string
	var pref string
	for _, child := range ret {
		i := strings.LastIndex(child.path, "/")
		pref = child.path[:i+1]
		names = append(names, child.path[i+1:])
	}

	sort.Strings(names)

	if args != nil && len(args) > 0 && args[0] == "long" {
		var list []longData
		for _, name := range names {
			rec := ret[pref+name]
			t := "image"
			if rec.dir {
				t = "folder"
			}
			rule, _ := privileges.NewRules(rec.owner, rec.group, rec.rules)
			list = append(list, longData{
				Name:  name,
				Type:  t,
				Rules: rule.Symbolic(rec.dir),
			})
		}
		return list
	} else {
		var list []shortData
		for _, name := range names {
			rec := ret[pref+name]
			t := "image"
			if rec.dir {
				t = "folder"
			}
			list = append(list, shortData{
				Name: name,
				Type: t,
			})
		}
		return list
	}

}
コード例 #4
0
ファイル: objects.go プロジェクト: alankm/vimages
func newObject(tx *sql.Tx, s *privileges.Session, overwrite bool, path, checksum string, headers map[string][]string) ([]string, error) {

	var deleted []string
	rules, _ := privileges.NewRules("root", "root", "0755")
	var obj object = newFolder(tx, s, "", rules)
	route := strings.Split(path[1:], "/")
	ctype := "folder"

	var date uint64
	a, ok := headers["Date"]
	if ok {
		date, _ = strconv.ParseUint(a[0], 10, 64)
	}

	author := ""
	a, ok = headers["Author"]
	if ok {
		author = a[0]
	}

	description := ""
	a, ok = headers["Description"]
	if ok {
		description = a[0]
	}

	for i, o := range route {

		if i == len(route)-1 && checksum != emptyPayloadChecksum {
			ctype = "image"
		}

		val, err := s.Exec(obj)
		if err != nil {
			return nil, err
		}

		children, ok := val.(map[string]object)
		if !ok {
			return nil, errInternal
		}

		gid, _ := s.Gid("", "")
		mod, _ := s.Umask("")
		rule, _ := privileges.NewRules(s.User, gid, mod)
		path := ""
		for j := 0; j <= i; j++ {
			path = path + "/" + route[j]
		}

		if children[o] == nil { // make an entry
			if !s.CanWrite(obj) {
				return nil, errDenied
			}
			if ctype == "folder" {
				folder := newFolder(tx, s, path, rule)
				children[o] = folder
				_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, true, s.User, gid, mod, "", 0, "", "")
				if err != nil {
					return nil, err
				}
			} else { // image
				_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, false, s.User, gid, mod, checksum, date, author, description)
				if err != nil {
					return nil, err
				}
				return deleted, nil
			}

		} else if children[o].otype() != ctype { // overwrite?
			if !overwrite {
				return nil, errCollission
			}
			if !s.CanWrite(obj) {
				return nil, errDenied
			}
			// overwrite EVERYTHING
			d, err := children[o].delete()
			if err != nil {
				return nil, err
			}
			deleted = append(deleted, d...)

			if ctype == "folder" {
				folder := newFolder(tx, s, path, rule)
				children[o] = folder
				_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, true, s.User, gid, mod, checksum, date, author, description)
				if err != nil {
					return nil, err
				}
			} else { // image
				_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, false, s.User, gid, mod, checksum, date, author, description)
				if err != nil {
					return nil, err
				}
				return deleted, nil
			}

		} else if i == len(route)-1 { // overwrite if final image
			if !overwrite {
				return nil, errCollission
			}
			if s.CanWrite(children[o]) {
				d, err := children[o].delete()
				if err != nil {
					return nil, err
				}
				deleted = append(deleted, d...)
				_, err = tx.Exec("DELETE FROM vimages WHERE path=?", path)
				if err != nil {
					return nil, err
				}
				_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, false, s.User, gid, mod, checksum, date, author, description)
				if err != nil {
					return nil, err
				}
				return deleted, nil

			} else {
				return nil, errDenied
			}
		}

		obj = children[o]

	}

	return deleted, nil

}