PackProblems 执行题目数据表打包操作
( configuration *commonStructs.JudgeConfiguration, options *persistence.ProblemPackageOptions, )
| 130 | |
| 131 | // PackProblems 执行题目数据表打包操作 |
| 132 | func PackProblems( |
| 133 | configuration *commonStructs.JudgeConfiguration, |
| 134 | options *persistence.ProblemPackageOptions, |
| 135 | ) error { |
| 136 | |
| 137 | if options.DigitalSign { |
| 138 | if options.DigitalPEM.PublicKey == nil || options.DigitalPEM.PrivateKey == nil { |
| 139 | return errors.Errorf("digital sign need public key and private key") |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fout, err := os.Create(options.OutFile) |
| 144 | if err != nil { |
| 145 | return errors.Errorf("create problem package file error: %s", err.Error()) |
| 146 | } |
| 147 | defer fout.Close() |
| 148 | |
| 149 | configBytes := utils.ObjectToJSONByte(configuration) |
| 150 | |
| 151 | bodyFile, err := mergeFilesBinary(options) |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | |
| 156 | bodyInfo, err := os.Stat(bodyFile) |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | certSize := 0 // 0 means disable cert |
| 162 | var publicKeyRaw []byte |
| 163 | if options.DigitalSign { |
| 164 | certSize = len(options.DigitalPEM.PublicKeyRaw) |
| 165 | publicKeyRaw = options.DigitalPEM.PublicKeyRaw |
| 166 | } |
| 167 | |
| 168 | pack := ProblemPackage{ |
| 169 | Version: 1, |
| 170 | CommitVersion: 1, |
| 171 | Configs: configBytes, |
| 172 | ConfigSize: uint32(len(configBytes)), |
| 173 | BodySize: uint32(bodyInfo.Size()), |
| 174 | CertSize: uint16(certSize), |
| 175 | Certificate: publicKeyRaw, |
| 176 | } |
| 177 | // Write Header |
| 178 | err = writeFileHeader(fout, pack) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | // Write Signature |
| 184 | fBody, err := os.Open(bodyFile) |
| 185 | if err != nil { |
| 186 | return err |
| 187 | } |
| 188 | |
| 189 | hash, err := persistence.SHA256Streams([]io.Reader{ |
no test coverage detected