Exemplo n.º 1
0
Arquivo: zip.go Projeto: jsli/gtbox
func ExtractZipFile(src string, dest string) error {
	if !strings.HasSuffix(dest, pathutil.SLASH) {
		dest += pathutil.SLASH
	}
	rc, err := zip.OpenReader(src)
	if err != nil {
		return err
	}
	defer rc.Close()

	for _, zf := range rc.Reader.File {
		name := zf.Name
		mode := zf.Mode()
		if mode.IsDir() {
			os.MkdirAll(dest+name, zf.Mode())
		} else {
			parent_path := pathutil.ParentPath(dest + name)
			os.MkdirAll(parent_path, pathutil.DEFAULT_DIR_ACCESS)
			if err := unpackZippedFile(dest, name, zf); err != nil {
				return err
			}
			os.Chmod(dest+name, mode)
		}
	}

	return nil
}
Exemplo n.º 2
0
Arquivo: file.go Projeto: jsli/gtbox
func CopyFile(src, dest string) (int64, error) {
	DeleteFile(dest)
	fi, fi_err := os.Stat(src)
	if fi_err != nil {
		return 0, fi_err
	}
	srcFile, err := os.OpenFile(src, os.O_RDONLY, fi.Mode())
	if err != nil {
		return 0, err
	}
	defer srcFile.Close()

	if strings.HasSuffix(dest, pathutil.SLASH) {
		pathutil.MkDir(dest)
		dest += pathutil.BaseName(src)
	} else {
		DeleteFile(dest)
		parent_path := pathutil.ParentPath(dest)
		pathutil.MkDir(parent_path)
	}

	destFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, fi.Mode())
	if err != nil {
		return 0, err
	}
	defer destFile.Close()

	return io.Copy(destFile, srcFile)
}
Exemplo n.º 3
0
Arquivo: misc.go Projeto: jsli/ota
func RecordMd5(path, txt_path string) error {
	md5_str, err := file.Md5SumFile(path)
	if md5_str == "" || err != nil {
		return err
	} else {
		parent := pathutil.ParentPath(path)
		if parent == "" {
			return fmt.Errorf("record md5 failed: %s parent path is empty", path)
		}
		base_name := pathutil.BaseName(path)
		err := file.WriteString2File(fmt.Sprintf("%s %s", md5_str, base_name), txt_path)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 4
0
func (cp *CpRelease) LoadSelfFromFileEvent(event *fsnotify.FileEvent) error {
	path := event.Name
	parent_path := pathutil.ParentPath(path)
	cp.Mode = constant.PATH_TO_MODE[parent_path[:len(parent_path)-1]]

	base_name := pathutil.BaseName(path)
	version := policy.ExtractVersion(base_name)
	if version == "" {
		return errors.New(fmt.Sprintf("Illegal version : %s", base_name))
	}
	cp.Version = version
	cp.VersionScalar = policy.QuantitateVersion(version)

	cp.Sim = constant.MODE_TO_SIM[cp.Mode]
	cp.LastModifyTs = time.Now().Unix()
	cp.Flag = constant.AVAILABLE_FLAG
	cp.RelPath = path[constant.PATH_PREFIX_LEN:]
	return nil
}