MCPcopy Create free account
hub / github.com/LanceLRQ/deer-executor / PackProblemsAsZip

Function PackProblemsAsZip

common/persistence/problems/pack_zip.go:73–142  ·  view source on GitHub ↗

PackProblemsAsZip 执行题目数据表打包操作(打包成zip版本)

(options *persistence.ProblemPackageOptions)

Source from the content-addressed store, hash-verified

71
72// PackProblemsAsZip 执行题目数据表打包操作(打包成zip版本)
73func PackProblemsAsZip(options *persistence.ProblemPackageOptions) error {
74 // 这边没法支持所有内容的校验了,只能给config签名。
75 if options.DigitalSign {
76 if options.DigitalPEM.PublicKey == nil || options.DigitalPEM.PrivateKey == nil {
77 return errors.Errorf("digital sign need public key and private key")
78 }
79 }
80
81 pubkeyFileName := path.Join(options.ConfigDir, ".gpg")
82 signFileName := path.Join(options.ConfigDir, ".sign")
83
84 // clean meta file
85 if _, err := os.Stat(pubkeyFileName); os.IsExist(err) {
86 _ = os.Remove(pubkeyFileName)
87 }
88 if _, err := os.Stat(signFileName); os.IsExist(err) {
89 _ = os.Remove(signFileName)
90 }
91
92 defer func() {
93 _ = os.Remove(pubkeyFileName)
94 _ = os.Remove(signFileName)
95 }()
96
97 // Cretea Public Key
98 if options.DigitalSign {
99 gpgFile, err := os.Create(pubkeyFileName)
100 if err != nil {
101 return err
102 }
103 if _, err = gpgFile.Write(options.DigitalPEM.PublicKeyRaw); err != nil {
104 return err
105 }
106 _ = gpgFile.Close()
107 }
108
109 // Create Signature (only for configFile)
110 fBody, err := os.Open(options.ConfigFile)
111 if err != nil {
112 return err
113 }
114
115 hash, err := persistence.SHA256Streams([]io.Reader{fBody})
116 if err != nil {
117 return err
118 }
119
120 // GPG signature
121 if options.DigitalSign {
122 hash, err = persistence.RSA2048Sign(hash, options.DigitalPEM.PrivateKey)
123 if err != nil {
124 return err
125 }
126 }
127
128 // Write to .sign file
129 signFile, err := os.Create(signFileName)
130 if err != nil {

Callers 1

BuildProblemPackageFunction · 0.92

Calls 4

SHA256StreamsFunction · 0.92
RSA2048SignFunction · 0.92
packZipFileFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected