-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathobject_put.go
More file actions
281 lines (247 loc) · 8.27 KB
/
object_put.go
File metadata and controls
281 lines (247 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/***************************************************************
*
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
package main
import (
"bufio"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"hash"
"hash/crc32"
"io"
"os"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/pelicanplatform/pelican/client"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/param"
)
var (
putCmd = &cobra.Command{
Use: "put {source ...} {destination}",
Short: "Send a file to a Pelican federation",
Run: putMain,
}
)
func init() {
flagSet := putCmd.Flags()
flagSet.StringP("token", "t", "", "Token file to use for transfer")
flagSet.BoolP("recursive", "r", false, "Recursively upload a collection. Forces methods to only be http to get the freshest collection contents")
flagSet.String("checksum-algorithm", "", "Checksum algorithm to use for upload and validation")
flagSet.Bool("require-checksum", false, "Require the server to return a checksum for the uploaded file (uses crc32c algorithm if no specific algorithm is specified)")
flagSet.String("checksums", "", "Verify files against a checksums manifest. The format is ALGORITHM:FILENAME")
flagSet.String("transfer-stats", "", "File to write transfer stats to")
objectCmd.AddCommand(putCmd)
}
type manifestEntry struct {
checksum string
filePath string
}
func parseManifest(filePath string) ([]manifestEntry, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var entries []manifestEntry
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.TrimSpace(line) == "" {
continue
}
parts := strings.Fields(line)
if len(parts) != 2 {
return nil, errors.Errorf("invalid manifest line format: %s", line)
}
entries = append(entries, manifestEntry{checksum: parts[0], filePath: parts[1]})
}
return entries, scanner.Err()
}
func verifyFileChecksum(filePath, expectedChecksum string, alg client.ChecksumType) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
var h hash.Hash
switch alg {
case client.AlgMD5:
h = md5.New()
case client.AlgSHA1:
h = sha1.New()
case client.AlgCRC32:
h = crc32.NewIEEE()
case client.AlgCRC32C:
crc32cTable := crc32.MakeTable(crc32.Castagnoli)
h = crc32.New(crc32cTable)
default:
return errors.Errorf("unsupported checksum algorithm: %v", alg)
}
if _, err := io.Copy(h, file); err != nil {
return err
}
computedChecksum := hex.EncodeToString(h.Sum(nil))
if !strings.EqualFold(computedChecksum, expectedChecksum) {
return errors.Errorf("checksum mismatch: expected %s, got %s", expectedChecksum, computedChecksum)
}
return nil
}
func putMain(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
err := config.InitClient()
if err != nil {
log.Errorln(err)
if client.IsRetryable(err) {
log.Errorln("Errors are retryable")
os.Exit(11)
} else {
os.Exit(1)
}
}
var options []client.TransferOption
// Set the progress bars to the command line option
tokenLocation, _ := cmd.Flags().GetString("token")
// Add checksum options if requested
checksumAlgorithm, _ := cmd.Flags().GetString("checksum-algorithm")
requireChecksum, _ := cmd.Flags().GetBool("require-checksum")
if requireChecksum || checksumAlgorithm != "" {
options = append(options, client.WithRequireChecksum())
}
if checksumAlgorithm != "" {
checksumType := client.ChecksumFromHttpDigest(checksumAlgorithm)
if checksumType == client.AlgUnknown {
log.Errorln("Unknown checksum algorithm:", checksumAlgorithm)
var validAlgorithms []string
for _, alg := range client.KnownChecksumTypes() {
validAlgorithms = append(validAlgorithms, client.HttpDigestFromChecksum(alg))
}
log.Errorln("Valid algorithms are:", strings.Join(validAlgorithms, ", "))
os.Exit(1)
}
options = append(options, client.WithRequestChecksums([]client.ChecksumType{checksumType}))
}
pb := newProgressBar()
defer pb.shutdown()
// Check if the program was executed from a terminal
// https://rosettacode.org/wiki/Check_output_device_is_a_terminal#Go
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode()&os.ModeCharDevice) != 0 && param.Logging_LogLocation.GetString() == "" && !param.Logging_DisableProgressBars.GetBool() {
pb.launchDisplay(ctx)
}
if len(args) < 2 {
log.Errorln("No Source or Destination")
if err := cmd.Help(); err != nil {
log.Errorln("Failed to print out help:", err)
}
os.Exit(1)
}
source := args[:len(args)-1]
dest := args[len(args)-1]
checksumsFile, _ := cmd.Flags().GetString("checksums")
if checksumsFile != "" {
parts := strings.SplitN(checksumsFile, ":", 2)
if len(parts) != 2 {
log.Errorln("invalid format for --checksums. Expected ALGORITHM:FILENAME")
os.Exit(1)
}
algName, manifestPath := parts[0], parts[1]
checksumType := client.ChecksumFromHttpDigest(algName)
if checksumType == client.AlgUnknown {
log.Errorln("Unknown checksum algorithm:", algName)
var validAlgorithms []string
for _, alg := range client.KnownChecksumTypes() {
validAlgorithms = append(validAlgorithms, client.HttpDigestFromChecksum(alg))
}
log.Errorln("Valid algorithms are:", strings.Join(validAlgorithms, ", "))
os.Exit(1)
}
log.Debugln("Parsing manifest file:", manifestPath)
manifestEntries, err := parseManifest(manifestPath)
if err != nil {
log.Errorf("failed to parse manifest file %s: %v", manifestPath, err)
os.Exit(1)
}
manifestMap := make(map[string]string)
for _, entry := range manifestEntries {
manifestMap[entry.filePath] = entry.checksum
}
for _, src := range source {
expectedChecksum, ok := manifestMap[src]
if !ok {
log.Errorf("source file %s not found in checksums manifest", src)
os.Exit(1)
}
if err := verifyFileChecksum(src, expectedChecksum, checksumType); err != nil {
log.Errorf("checksum validation failed for %s: %v", src, err)
os.Exit(1)
}
log.Infof("Checksum verified for %s", src)
}
}
log.Debugln("Sources:", source)
log.Debugln("Destination:", dest)
var result error
lastSrc := ""
options = append(options, client.WithCallback(pb.callback), client.WithTokenLocation(tokenLocation))
finalResults := make([][]client.TransferResults, 0)
for _, src := range source {
isRecursive, _ := cmd.Flags().GetBool("recursive")
transferResults, result := client.DoPut(ctx, src, dest, isRecursive, options...)
if result != nil {
lastSrc = src
break
}
finalResults = append(finalResults, transferResults)
}
// Exit with failure
if result != nil {
// Print the list of errors
if errors.Is(result, config.ErrIncorrectPassword) {
fmt.Fprintln(os.Stderr, "Failed to access local credential file - entered incorrect local decryption password")
fmt.Fprintln(os.Stderr, "If you have forgotten your password, you can reset the local state (deleting all on-disk credentials)")
fmt.Fprintf(os.Stderr, "by running '%s credentials reset-local'\n", os.Args[0])
os.Exit(1)
}
errMsg := result.Error()
var te *client.TransferErrors
if errors.As(result, &te) {
errMsg = te.UserError()
}
log.Errorln("Failure putting " + lastSrc + ": " + errMsg)
if client.ShouldRetry(result) {
log.Errorln("Errors are retryable")
os.Exit(11)
}
os.Exit(1)
}
transferStatsFile, _ := cmd.Flags().GetString("transfer-stats")
if transferStatsFile != "" {
transferStats, err := json.MarshalIndent(finalResults, "", " ")
if err != nil {
log.Errorln("Failed to marshal transfer results:", err)
}
err = os.WriteFile(transferStatsFile, transferStats, 0644)
if err != nil {
log.Errorln("Failed to write transfer stats to file:", err)
}
}
}