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) }
func (rcj *ReleaseCreationJob) Run() { tag := "JOB_D: " revel.INFO.Println(tag, "running") dal, err := models.NewDal() if err != nil { revel.ERROR.Println(tag, err) return } defer dal.Close() task, err := models.PopOneCreationTask(dal) if err != nil { revel.ERROR.Println(tag, err) return } if task != nil { task.Flag = ota_constant.FLAG_CREATING task.ModifiedTs = time.Now().Unix() task.Update(dal) root_path := fmt.Sprintf("%s%s/", ota_constant.TMP_FILE_ROOT, policy.GenerateRandFileName()) pathutil.MkDir(root_path) defer file.DeleteDir(root_path) revel.INFO.Println(tag, "Processing task : ", task.UpdateRequest) revel.INFO.Println(tag, "TEMP dir : ", root_path) release, err := policy.GenerateOtaPackage(dal, task, root_path) if err != nil || release == nil { revel.ERROR.Println(tag, "Failed: ", err) revel.INFO.Println(tag, "Failed, task id= ", task.Id, " error msg: ", err) if task.RetryCount >= ota_constant.RETRY_COUNT { task.Flag = ota_constant.FLAG_DROPPED } else { task.RetryCount = task.RetryCount + 1 task.Flag = ota_constant.FLAG_INIT revel.INFO.Println(tag, "Failed, task id= ", task.Id, " retry: ", task.RetryCount) } task.ModifiedTs = time.Now().Unix() _, uerr := task.Update(dal) revel.ERROR.Println(tag, " task UPDATE Failed: ", uerr) return } task.Delete(dal) } }
func GenerateOtaPackage(dal *models.Dal, task *models.ReleaseCreationTask, root_path string) (*models.RadioOtaRelease, error) { //0.parse request from json string which stored in DB. update_request, err := ParseRequest(task.UpdateRequest) if err != nil { return nil, err } update_request.Cps = SortCps(update_request) //1.create all paths zip_path := fmt.Sprintf("%s%s", root_path, ota_constant.ZIP_DIR_NAME) pathutil.MkDir(zip_path) template_path := ota_constant.MODEL_TO_TEMPLATE[update_request.Device.Model] update_pkg_path := fmt.Sprintf("%s%s", root_path, ota_constant.UPDATE_PKG_NAME) radio_ota_path := fmt.Sprintf("%s%s", root_path, ota_constant.RADIO_OTA_PACKAGE_NAME) radio_dtim_path := fmt.Sprintf("%s%s", root_path, ota_constant.RADIO_DTIM_NAME) radio_image_path := fmt.Sprintf("%s%s", root_path, ota_constant.RADIO_IMAGE_NAME) //2.copy template files err = file.CopyDir(template_path, zip_path) if err != nil { return nil, err } //3.create Radio.dtim, Radio.img err = file.WriteBytes2File(task.Data, radio_dtim_path) if err != nil { return nil, err } image_list_final := make([]string, 0, 5) image_list := GenerateImageList(update_request) for _, image_rel_path := range image_list { dest_path := root_path + image_rel_path _, err := file.CopyFile(ota_constant.CP_RELEASE_ROOT_FINAL+image_rel_path, dest_path) if err != nil { return nil, err } image_list_final = append(image_list_final, dest_path) } err = gzipCpImage(image_list_final) if err != nil { revel.INFO.Printf("zip image error : %s\n", err) return nil, err } for _, image_path := range image_list_final { file.CopyFile(image_path+".gz", image_path) } err = generateRadioImage(radio_dtim_path, radio_image_path, image_list_final) if err != nil { return nil, err } _, err = file.CopyFile(radio_image_path, fmt.Sprintf("%s%s", zip_path, ota_constant.RADIO_IMAGE_NAME)) // 4. archive all files err = archive.ArchiveZipFile(zip_path, update_pkg_path) if err != nil { return nil, err } //5.generate update.zip (updatetool + update_pkg.zip) params := make([]string, 5) params[0] = ota_constant.OTA_CMD_PARAM_PLATFORM_PREFIX + update_request.Device.Platform params[1] = ota_constant.OTA_CMD_PARAM_PRODUCT_PREFIX + update_request.Device.Model params[2] = ota_constant.OTA_CMD_PARAM_OEM_PREFIX params[3] = ota_constant.OTA_CMD_PARAM_OUTPUT_PREFIX + radio_ota_path params[4] = ota_constant.OTA_CMD_PARAM_INPUT_PREFIX + update_pkg_path err = generateOtaPackage(params) if err != nil { return nil, err } //6.insert db release := &models.RadioOtaRelease{} release.Flag = ota_constant.FLAG_AVAILABLE release.FingerPrint = task.FingerPrint release.ReleaseNote = "empty" release.CreatedTs = time.Now().Unix() release.ModifiedTs = release.CreatedTs release.Model = update_request.Device.Model release.Platform = update_request.Device.Platform release.Md5, err = file.Md5SumFile(radio_ota_path) release.Versions = GenerateOtaPackageVersions(update_request) release.Images = strings.Join(image_list, " ") if err != nil { return nil, err } release.Size, err = file.GetFileSize(radio_ota_path) if err != nil { return nil, err } release.Delete(dal) file.DeleteDir(fmt.Sprintf("%s%s", ota_constant.RADIO_OTA_RELEASE_ROOT, release.FingerPrint)) id, err := release.Save(dal) if id < 0 || err != nil { return nil, err } release.Id = id // 7.copy final file to public directory, checksum final_dir := fmt.Sprintf("%s%s/", ota_constant.RADIO_OTA_RELEASE_ROOT, release.FingerPrint) pathutil.MkDir(final_dir) final_path := fmt.Sprintf("%s%s", final_dir, ota_constant.RADIO_OTA_PACKAGE_NAME) checksum_path := fmt.Sprintf("%s%s", final_dir, ota_constant.CHECKSUM_TXT_NAME) _, err = file.CopyFile(radio_ota_path, final_path) if err != nil { return nil, err } RecordMd5(final_path, checksum_path) return release, nil }
func testMkDir() { path := "/home/manson/temp/test/tmp/dir" err := pathutil.MkDir(path) checkErr(err, "pathutil.MkDir") }