コード例 #1
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

}
コード例 #2
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

}