示例#1
0
func GetItem(log *logging.Logger, db *DB, element string, section *common.Section) (common.JSONFile, error) {
	var result common.JSONFile
	var rows *sql.Rows
	var err error

	var query = "SELECT os, username, groupname, type, " +
		"link, mtime, ctime, hash, perms, compressed FROM attrs " +
		"WHERE element = $1 AND area = $2 AND grace = $3 AND dataset = $4"

	rows, err = db.Conn.Query(query, element, section.Name, section.Grace, section.Dataset)
	if err != nil {
		log.Error("Get item error: " + err.Error())
	}

	// FIXME: ugly. Find a method for using itemexists() for check if item exists or not
	if rows.Next() {
		err = rows.Scan(&result.Os, &result.User, &result.Group, &result.Type, &result.Link,
			&result.Mtime, &result.Ctime, &result.Hash, &result.Mode, &result.Compressed)
		if err != nil {
			log.Error("Get item values extraction error: " + err.Error())
		} else {
			result.Acl, err = GetAcls(log, db, element, section)
			if err != nil {
				log.Error("Get ACLs values extraction error: " + err.Error())
			} else {
				result.Name = element
			}
		}

		return result, err
	} else {
		return common.JSONFile{}, errorMsg(log, 7, "Item not found: "+element)
	}
}
示例#2
0
func (v visit) visitFile(fp string, fi os.FileInfo, err error) error {
	var res common.JSONResult
	var file common.JSONFile

	if err != nil {
		res = common.JSONResult{Result: "ko", Message: err.Error()}
		res.Send(v.connection)
		return nil
	}

	if v.exclude != "" && isMatch(v.exclude, fp) {
		return nil
	}

	// Set the file name and the operating system
	file.Name = fp
	file.Os = runtime.GOOS
	file.Mtime = fi.ModTime().Unix()

	if runtime.GOOS != "windows" {
		file.User, _ = getUserName(fi)
		file.Group, _ = getGroupName(fi)
		file.Mode = fi.Mode().String()
		file.Ctime = getCtime(fi)
	}

	// Set type of element (file or directory)
	if fi.IsDir() {
		file.Type = "directory"
		file.Size = fi.Size()
	} else if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		file.Type = "symlink"
		file.Size = fi.Size()

		link, err := os.Readlink(fp)
		if err != nil {
			v.log.Debug("Error when readlink for " + fp + ": " + err.Error())
		} else {
			file.Link = link
		}
	} else {
		file.Type = "file"
		file.Size = fi.Size()
		file.Hash = hex.EncodeToString(common.Md5(fp))
	}

	if v.acl && file.Type != "symlink" {
		if runtime.GOOS != "windows" {
			fa := FileACL(fp)
			file.Acl = fa.List(v.log)
		}
	}

	// Set result
	res.Result = "ok"
	res.Data = file
	res.Send(v.connection)
	return nil
}