// Update the meta data, meta sign, public key func (uc *UpdateClient) Update(repoURL string) error { repo, err := client.NewUCRepo(repoURL) if err != nil { return err } needUpdate := false remoteMetaBytes, err := repo.GetMeta() if err != nil { return err } localMetaFile := filepath.Join(uc.getCacheDir(), repo.NRString(), defaultMeta) localMetaBytes, err := ioutil.ReadFile(localMetaFile) if err != nil { needUpdate = true } else { var remoteMeta utils.Meta var localMeta utils.Meta json.Unmarshal(remoteMetaBytes, &remoteMeta) json.Unmarshal(localMetaBytes, &localMeta) needUpdate = localMeta.Before(remoteMeta) } if !needUpdate { fmt.Println("Already updated to the latest version.") return nil } uc.save(repo, defaultMeta, remoteMetaBytes) signBytes, _ := repo.GetMetaSign() pubBytes, _ := repo.GetPublicKey() err = utils.SHA256Verify(pubBytes, remoteMetaBytes, signBytes) if err != nil { fmt.Println("Fail to verify meta by public key") return err } metaURL, err := uc.save(repo, defaultMeta, remoteMetaBytes) if err != nil { return err } signURL, err := uc.save(repo, defaultMetaSign, signBytes) if err != nil { os.Remove(metaURL) return err } _, err = uc.save(repo, defaultPublicKey, pubBytes) if err != nil { os.Remove(metaURL) os.Remove(signURL) return err } return nil }
// Delete removes an application from a repository func (r LocalRepo) Delete(name string) error { var meta utils.Meta metaFile := r.GetMetaFile() if !utils.IsFileExist(metaFile) { return ErrorEmptyLocalRepo } data, err := ioutil.ReadFile(metaFile) if err != nil { return err } err = json.Unmarshal(data, &meta) if err != nil { //This may happend in migration, meta struct changes. os.Remove(metaFile) return nil } for i := range meta.Items { if meta.Items[i].Name != name { continue } dataFile := filepath.Join(r.GetTopDir(), defaultTargetDir, meta.Items[i].Hash) if err := os.Remove(dataFile); err != nil { return err } meta.Items = append(meta.Items[:i], meta.Items[i+1:]...) if err := r.saveMeta(meta); err != nil { return err } return nil } return ErrorAppNotExist }
func (r LocalRepo) saveMeta(meta utils.Meta) error { meta.Updated = time.Now() metaContent, _ := json.Marshal(meta) metaFile := r.GetMetaFile() err := ioutil.WriteFile(metaFile, metaContent, 0644) if err != nil { return err } // write sign file err = r.saveSign(metaContent) if err != nil { os.Remove(metaFile) return err } return nil }
// Put adds an application to a repository func (r LocalRepo) Put(name string, content []byte, method utils.EncryptMethod) (string, error) { topDir := r.GetTopDir() if !utils.IsDirExist(topDir) { if err := os.MkdirAll(topDir, 0777); err != nil { return "", err } } var meta utils.Meta metaFile := r.GetMetaFile() if utils.IsFileExist(metaFile) { data, err := ioutil.ReadFile(metaFile) if err != nil { return "", err } err = json.Unmarshal(data, &meta) if err != nil { //This may happend in migration, meta struct changes. os.Remove(metaFile) } } encryptContent, err := r.encrypt(method, content) if err != nil { return "", err } item := utils.GenerateMetaItem(name, encryptContent) item.SetEncryption(method) // Using the 'hash' value to rename the original file dataFileName := item.GetHash() dataFile := filepath.Join(topDir, defaultTargetDir, dataFileName) if !utils.IsDirExist(filepath.Dir(dataFile)) { if err := os.MkdirAll(filepath.Dir(dataFile), 0777); err != nil { return "", err } } // write data if err := ioutil.WriteFile(dataFile, encryptContent, 0644); err != nil { return "", err } // get meta content exist := false for i := range meta.Items { if meta.Items[i].Name == name { meta.Items[i] = item exist = true } } if !exist { meta.Items = append(meta.Items, item) } // write meta data err = r.saveMeta(meta) if err != nil { os.Remove(dataFile) return "", err } return dataFile, nil }