func ProcessDir(info os.FileInfo, dal *release.Dal, path string, mode string, sim string, force bool) error { version := policy.ExtractVersion(info.Name()) if version == "" { //illegal version fmt, ignore return fmt.Errorf("Illegal version format : %s", info.Name()) } rel_path := fmt.Sprintf("%s/%s", path, info.Name())[constant.PATH_PREFIX_LEN:] cp, err := release.FindCpReleaseByPath(dal, rel_path) if err != nil { return err } if cp != nil { if !force { return fmt.Errorf("Existed CP release : %s", cp) } else { log.Printf("Existed CP release, delete arbi&grbi for force updating : %s", cp) release.DeleteArbiByCpId(dal, cp.Id) release.DeleteGrbiByCpId(dal, cp.Id) release.DeleteRficByCpId(dal, cp.Id) } } else { cp = &release.CpRelease{} cp.Mode = mode cp.Sim = sim cp.Version = version cp.VersionScalar = policy.QuantitateVersion(version) cp.LastModifyTs = time.Now().Unix() cp.Flag = constant.AVAILABLE_FLAG cp.RelPath = rel_path slice := strings.Split(rel_path, "/") cp.Prefix = strings.TrimSuffix(slice[2], version) log.Printf("Find new CP release : %s\n", cp) id, err := cp.Save(dal) if err != nil { cp.Id = -1 log.Printf("Save CP release failed: %s\n", err) } else { cp.Id = id log.Printf("Save CP release success: %d | %s\n", id, cp) } } //find detail information if cp.Id > 0 && FLAG_SCAN_DETAIL { ProcessDetail(cp, dal) } else { return fmt.Errorf("Neither finding or saving CP release success! in [%s]", info.Name()) } return nil }
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 }
func (ci *CpImage) LoadSelf(attrs []string) error { ci.Id = attrs[0] ci.Network = attrs[1] ci.Sim = attrs[2] ci.Path = attrs[3] slice := strings.Split(strings.TrimSpace(ci.Path), "/") slice = TrimArrayTail(slice) if len(slice) < 3 { return fmt.Errorf("Parse image path failed. path = %s", ci.Path) } ci.Mode = slice[1] ci.Version = cp_policy.ExtractVersion(slice[2]) if ci.Version == "" { return fmt.Errorf("Parse version from image path failed. path = %s", ci.Path) } ci.Prefix = strings.TrimSuffix(slice[2], ci.Version) return nil }