Example #1
0
// NewSnapFile loads a snap from the given snapFile
func NewSnapFile(snapFile string, origin string, unsignedOk bool) (*SnapFile, error) {
	d, err := snap.Open(snapFile)
	if err != nil {
		return nil, err
	}

	yamlData, err := d.MetaMember("package.yaml")
	if err != nil {
		return nil, err
	}

	_, err = d.MetaMember("hooks/config")
	hasConfig := err == nil

	m, err := parsePackageYamlData(yamlData, hasConfig)
	if err != nil {
		return nil, err
	}

	targetDir := dirs.SnapSnapsDir
	if origin == SideloadedOrigin {
		m.Version = helpers.NewSideloadVersion()
	}

	fullName := m.qualifiedName(origin)
	instDir := filepath.Join(targetDir, fullName, m.Version)

	return &SnapFile{
		instdir: instDir,
		origin:  origin,
		m:       m,
		deb:     d,
	}, nil
}
Example #2
0
func readSnapInfo(snapPath string) (*snap.Info, error) {
	// TODO Only open if in devmode or we have the assertion proving content right.
	snapf, err := snap.Open(snapPath)
	if err != nil {
		return nil, err
	}
	return snapf.Info()
}
Example #3
0
func (s *Store) bulkEndpoint(w http.ResponseWriter, req *http.Request) {
	var pkgs bulkReqJSON
	var replyData []bulkReplyJSON

	decoder := json.NewDecoder(req.Body)
	if err := decoder.Decode(&pkgs); err != nil {
		http.Error(w, fmt.Sprintf("can't decode request body: %v", err), http.StatusBadRequest)
		return
	}

	s.refreshSnaps()

	// check if we have downloadable snap of the given name
	for _, pkgWithChannel := range pkgs.Name {
		pkg := strings.Split(pkgWithChannel, "/")[0]

		if fn, ok := s.snaps[pkg]; ok {
			snapFile, err := snap.Open(fn)
			if err != nil {
				http.Error(w, fmt.Sprintf("can not read: %v: %v", fn, err), http.StatusBadRequest)
				return
			}

			info, err := snapFile.Info()
			if err != nil {
				http.Error(w, fmt.Sprintf("can get info for: %v: %v", fn, err), http.StatusBadRequest)
				return
			}

			replyData = append(replyData, bulkReplyJSON{
				Status:          "Published",
				Name:            fmt.Sprintf("%s.%s", info.Name(), s.defaultDeveloper),
				PackageName:     info.Name(),
				Developer:       defaultDeveloper,
				AnonDownloadURL: fmt.Sprintf("%s/download/%s", s.URL(), filepath.Base(fn)),
				Version:         info.Version,
				Revision:        makeRevision(info),
			})
		}
	}

	// use indent because this is a development tool, output
	// should look nice
	out, err := json.MarshalIndent(replyData, "", "    ")
	if err != nil {
		http.Error(w, fmt.Sprintf("can marshal: %v: %v", replyData, err), http.StatusBadRequest)
		return
	}
	w.Write(out)
}
Example #4
0
// openSnapFile opens a snap blob returning both a snap.Info completed
// with sideInfo (if not nil) and a corresponding snap.File.
func openSnapFile(snapPath string, unsignedOk bool, sideInfo *snap.SideInfo) (*snap.Info, snap.File, error) {
	// TODO: what precautions to take if unsignedOk == false ?

	snapf, err := snap.Open(snapPath)
	if err != nil {
		return nil, nil, err
	}

	info, err := snapf.Info()
	if err != nil {
		return nil, nil, err
	}

	var snapInfo snap.Info
	snapInfo = *info
	if sideInfo != nil {
		snapInfo.SideInfo = *sideInfo
	}

	return &snapInfo, snapf, nil
}
Example #5
0
func (s *Store) detailsEndpoint(w http.ResponseWriter, req *http.Request) {
	s.refreshSnaps()

	pkg := req.URL.Path[len("/package/"):]
	fn, ok := s.snaps[pkg]
	if !ok {
		http.NotFound(w, req)
		return
	}

	snapFile, err := snap.Open(fn)
	if err != nil {
		http.Error(w, fmt.Sprintf("can not read: %v: %v", fn, err), http.StatusBadRequest)
		return
	}
	info, err := snapFile.Info()
	if err != nil {
		http.Error(w, fmt.Sprintf("can get info for: %v: %v", fn, err), http.StatusBadRequest)
		return
	}

	replyData := detailsReplyJSON{
		Name:            fmt.Sprintf("%s.%s", info.Name(), s.defaultDeveloper),
		PackageName:     info.Name(),
		Developer:       defaultDeveloper,
		AnonDownloadURL: fmt.Sprintf("%s/download/%s", s.URL(), filepath.Base(fn)),
		DownloadURL:     fmt.Sprintf("%s/download/%s", s.URL(), filepath.Base(fn)),
		Version:         info.Version,
		Revision:        makeRevision(info),
	}

	// use indent because this is a development tool, output
	// should look nice
	out, err := json.MarshalIndent(replyData, "", "    ")
	if err != nil {
		http.Error(w, fmt.Sprintf("can marshal: %v: %v", replyData, err), http.StatusBadRequest)
		return
	}
	w.Write(out)
}
Example #6
0
func (s *Store) refreshSnaps() error {
	s.snaps = map[string]string{}

	snaps, err := filepath.Glob(filepath.Join(s.blobDir, "*.snap"))
	if err != nil {
		return err
	}

	for _, fn := range snaps {
		snapFile, err := snap.Open(fn)
		if err != nil {
			return err
		}
		info, err := snapFile.Info()
		if err != nil {
			return err
		}
		s.snaps[fmt.Sprintf("%s.%s", info.Name(), s.defaultDeveloper)] = fn
	}

	return nil
}