]> git.r.bdr.sh - rbdr/mobius/blame_incremental - hotline/file_transfer.go
Refactoring, cleanup, test backfilling
[rbdr/mobius] / hotline / file_transfer.go
... / ...
CommitLineData
1package hotline
2
3import (
4 "bufio"
5 "crypto/rand"
6 "encoding/binary"
7 "errors"
8 "fmt"
9 "io"
10 "io/fs"
11 "log/slog"
12 "math"
13 "os"
14 "path/filepath"
15 "strings"
16 "sync"
17)
18
19// File transfer types
20const (
21 FileDownload = iota
22 FileUpload
23 FolderDownload
24 FolderUpload
25 bannerDownload
26)
27
28type FileTransfer struct {
29 FileName []byte
30 FilePath []byte
31 refNum [4]byte
32 Type int
33 TransferSize []byte
34 FolderItemCount []byte
35 fileResumeData *FileResumeData
36 options []byte
37 bytesSentCounter *WriteCounter
38 ClientConn *ClientConn
39}
40
41// WriteCounter counts the number of bytes written to it.
42type WriteCounter struct {
43 mux sync.Mutex
44 Total int64 // Total # of bytes written
45}
46
47// Write implements the io.Writer interface.
48//
49// Always completes and never returns an error.
50func (wc *WriteCounter) Write(p []byte) (int, error) {
51 wc.mux.Lock()
52 defer wc.mux.Unlock()
53 n := len(p)
54 wc.Total += int64(n)
55 return n, nil
56}
57
58func (cc *ClientConn) newFileTransfer(transferType int, fileName, filePath, size []byte) *FileTransfer {
59 ft := &FileTransfer{
60 FileName: fileName,
61 FilePath: filePath,
62 Type: transferType,
63 TransferSize: size,
64 ClientConn: cc,
65 bytesSentCounter: &WriteCounter{},
66 }
67
68 _, _ = rand.Read(ft.refNum[:])
69
70 cc.transfersMU.Lock()
71 defer cc.transfersMU.Unlock()
72 cc.transfers[transferType][ft.refNum] = ft
73
74 cc.Server.mux.Lock()
75 defer cc.Server.mux.Unlock()
76 cc.Server.fileTransfers[ft.refNum] = ft
77
78 return ft
79}
80
81// String returns a string representation of a file transfer and its progress for display in the GetInfo window
82// Example:
83// MasterOfOrionII1.4.0. 0% 197.9M
84func (ft *FileTransfer) String() string {
85 trunc := fmt.Sprintf("%.21s", ft.FileName)
86 return fmt.Sprintf("%-21s %.3s%% %6s\n", trunc, ft.percentComplete(), ft.formattedTransferSize())
87}
88
89func (ft *FileTransfer) percentComplete() string {
90 ft.bytesSentCounter.mux.Lock()
91 defer ft.bytesSentCounter.mux.Unlock()
92 return fmt.Sprintf(
93 "%v",
94 math.RoundToEven(float64(ft.bytesSentCounter.Total)/float64(binary.BigEndian.Uint32(ft.TransferSize))*100),
95 )
96}
97
98func (ft *FileTransfer) formattedTransferSize() string {
99 sizeInKB := float32(binary.BigEndian.Uint32(ft.TransferSize)) / 1024
100 if sizeInKB >= 1024 {
101 return fmt.Sprintf("%.1fM", sizeInKB/1024)
102 } else {
103 return fmt.Sprintf("%.0fK", sizeInKB)
104 }
105}
106
107func (ft *FileTransfer) ItemCount() int {
108 return int(binary.BigEndian.Uint16(ft.FolderItemCount))
109}
110
111type folderUpload struct {
112 DataSize [2]byte
113 IsFolder [2]byte
114 PathItemCount [2]byte
115 FileNamePath []byte
116}
117
118//func (fu *folderUpload) Write(p []byte) (int, error) {
119// if len(p) < 7 {
120// return 0, errors.New("buflen too short")
121// }
122// copy(fu.DataSize[:], p[0:2])
123// copy(fu.IsFolder[:], p[2:4])
124// copy(fu.PathItemCount[:], p[4:6])
125//
126// fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes TODO: wat
127// n, err := io.ReadFull(rwc, fu.FileNamePath)
128// if err != nil {
129// return 0, err
130// }
131//
132// return n + 6, nil
133//}
134
135func (fu *folderUpload) FormattedPath() string {
136 pathItemLen := binary.BigEndian.Uint16(fu.PathItemCount[:])
137
138 var pathSegments []string
139 pathData := fu.FileNamePath
140
141 // TODO: implement scanner interface instead?
142 for i := uint16(0); i < pathItemLen; i++ {
143 segLen := pathData[2]
144 pathSegments = append(pathSegments, string(pathData[3:3+segLen]))
145 pathData = pathData[3+segLen:]
146 }
147
148 return filepath.Join(pathSegments...)
149}
150
151func DownloadHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fs FileStore, rLogger *slog.Logger, preserveForks bool) error {
152 //s.Stats.DownloadCounter += 1
153 //s.Stats.DownloadsInProgress += 1
154 //defer func() {
155 // s.Stats.DownloadsInProgress -= 1
156 //}()
157
158 var dataOffset int64
159 if fileTransfer.fileResumeData != nil {
160 dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:]))
161 }
162
163 fw, err := newFileWrapper(fs, fullPath, 0)
164 if err != nil {
165 //return err
166 }
167
168 rLogger.Info("File download started", "filePath", fullPath)
169
170 // if file transfer options are included, that means this is a "quick preview" request from a 1.5+ client
171 if fileTransfer.options == nil {
172 _, err = io.Copy(rwc, fw.ffo)
173 if err != nil {
174 //return err
175 }
176 }
177
178 file, err := fw.dataForkReader()
179 if err != nil {
180 //return err
181 }
182
183 br := bufio.NewReader(file)
184 if _, err := br.Discard(int(dataOffset)); err != nil {
185 //return err
186 }
187
188 if _, err = io.Copy(rwc, io.TeeReader(br, fileTransfer.bytesSentCounter)); err != nil {
189 return err
190 }
191
192 // if the client requested to resume transfer, do not send the resource fork header, or it will be appended into the fileWrapper data
193 if fileTransfer.fileResumeData == nil {
194 err = binary.Write(rwc, binary.BigEndian, fw.rsrcForkHeader())
195 if err != nil {
196 return err
197 }
198 }
199
200 rFile, err := fw.rsrcForkFile()
201 if err != nil {
202 return nil
203 }
204
205 if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil {
206 return err
207 }
208
209 return nil
210}
211
212func UploadHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fileStore FileStore, rLogger *slog.Logger, preserveForks bool) error {
213 var file *os.File
214
215 // A file upload has two possible cases:
216 // 1) Upload a new file
217 // 2) Resume a partially transferred file
218 // We have to infer which case applies by inspecting what is already on the filesystem
219
220 // Check for existing file. If found, do not proceed. This is an invalid scenario, as the file upload transaction
221 // handler should have returned an error to the client indicating there was an existing file present.
222 _, err := os.Stat(fullPath)
223 if err == nil {
224 return fmt.Errorf("existing file found: %s", fullPath)
225 }
226 if errors.Is(err, fs.ErrNotExist) {
227 // If not found, open or create a new .incomplete file
228 file, err = os.OpenFile(fullPath+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
229 if err != nil {
230 return err
231 }
232 }
233
234 f, err := newFileWrapper(fileStore, fullPath, 0)
235 if err != nil {
236 return err
237 }
238
239 rLogger.Info("File upload started", "dstFile", fullPath)
240
241 rForkWriter := io.Discard
242 iForkWriter := io.Discard
243 if preserveForks {
244 rForkWriter, err = f.rsrcForkWriter()
245 if err != nil {
246 return err
247 }
248
249 iForkWriter, err = f.infoForkWriter()
250 if err != nil {
251 return err
252 }
253 }
254
255 if err := receiveFile(rwc, file, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil {
256 rLogger.Error(err.Error())
257 }
258
259 if err := file.Close(); err != nil {
260 return err
261 }
262
263 if err := fileStore.Rename(fullPath+".incomplete", fullPath); err != nil {
264 return err
265 }
266
267 rLogger.Info("File upload complete", "dstFile", fullPath)
268
269 return nil
270}
271
272func DownloadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fileStore FileStore, rLogger *slog.Logger, preserveForks bool) error {
273 // Folder Download flow:
274 // 1. Get filePath from the transfer
275 // 2. Iterate over files
276 // 3. For each fileWrapper:
277 // Send fileWrapper header to client
278 // The client can reply in 3 ways:
279 //
280 // 1. If type is an odd number (unknown type?), or fileWrapper download for the current fileWrapper is completed:
281 // client sends []byte{0x00, 0x03} to tell the server to continue to the next fileWrapper
282 //
283 // 2. If download of a fileWrapper is to be resumed:
284 // client sends:
285 // []byte{0x00, 0x02} // download folder action
286 // [2]byte // Resume data size
287 // []byte fileWrapper resume data (see myField_FileResumeData)
288 //
289 // 3. Otherwise, download of the fileWrapper is requested and client sends []byte{0x00, 0x01}
290 //
291 // When download is requested (case 2 or 3), server replies with:
292 // [4]byte - fileWrapper size
293 // []byte - Flattened File Object
294 //
295 // After every fileWrapper download, client could request next fileWrapper with:
296 // []byte{0x00, 0x03}
297 //
298 // This notifies the server to send the next item header
299
300 basePathLen := len(fullPath)
301
302 rLogger.Info("Start folder download", "path", fullPath)
303
304 nextAction := make([]byte, 2)
305 if _, err := io.ReadFull(rwc, nextAction); err != nil {
306 return err
307 }
308
309 i := 0
310 err := filepath.Walk(fullPath+"/", func(path string, info os.FileInfo, err error) error {
311 //s.Stats.DownloadCounter += 1
312 i += 1
313
314 if err != nil {
315 return err
316 }
317
318 // skip dot files
319 if strings.HasPrefix(info.Name(), ".") {
320 return nil
321 }
322
323 hlFile, err := newFileWrapper(fileStore, path, 0)
324 if err != nil {
325 return err
326 }
327
328 subPath := path[basePathLen+1:]
329 rLogger.Debug("Sending fileheader", "i", i, "path", path, "fullFilePath", fullPath, "subPath", subPath, "IsDir", info.IsDir())
330
331 if i == 1 {
332 return nil
333 }
334
335 fileHeader := NewFileHeader(subPath, info.IsDir())
336 if _, err := io.Copy(rwc, &fileHeader); err != nil {
337 return fmt.Errorf("error sending file header: %w", err)
338 }
339
340 // Read the client's Next Action request
341 if _, err := io.ReadFull(rwc, nextAction); err != nil {
342 return err
343 }
344
345 rLogger.Debug("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
346
347 var dataOffset int64
348
349 switch nextAction[1] {
350 case dlFldrActionResumeFile:
351 // get size of resumeData
352 resumeDataByteLen := make([]byte, 2)
353 if _, err := io.ReadFull(rwc, resumeDataByteLen); err != nil {
354 return err
355 }
356
357 resumeDataLen := binary.BigEndian.Uint16(resumeDataByteLen)
358 resumeDataBytes := make([]byte, resumeDataLen)
359 if _, err := io.ReadFull(rwc, resumeDataBytes); err != nil {
360 return err
361 }
362
363 var frd FileResumeData
364 if err := frd.UnmarshalBinary(resumeDataBytes); err != nil {
365 return err
366 }
367 dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
368 case dlFldrActionNextFile:
369 // client asked to skip this file
370 return nil
371 }
372
373 if info.IsDir() {
374 return nil
375 }
376
377 rLogger.Info("File download started",
378 "fileName", info.Name(),
379 "TransferSize", fmt.Sprintf("%x", hlFile.ffo.TransferSize(dataOffset)),
380 )
381
382 // Send file size to client
383 if _, err := rwc.Write(hlFile.ffo.TransferSize(dataOffset)); err != nil {
384 rLogger.Error(err.Error())
385 return fmt.Errorf("error sending file size: %w", err)
386 }
387
388 // Send ffo bytes to client
389 _, err = io.Copy(rwc, hlFile.ffo)
390 if err != nil {
391 return fmt.Errorf("error sending flat file object: %w", err)
392 }
393
394 file, err := fileStore.Open(path)
395 if err != nil {
396 return fmt.Errorf("error opening file: %w", err)
397 }
398
399 // wr := bufio.NewWriterSize(rwc, 1460)
400 if _, err = io.Copy(rwc, io.TeeReader(file, fileTransfer.bytesSentCounter)); err != nil {
401 return fmt.Errorf("error sending file: %w", err)
402 }
403
404 if nextAction[1] != 2 && hlFile.ffo.FlatFileHeader.ForkCount[1] == 3 {
405 err = binary.Write(rwc, binary.BigEndian, hlFile.rsrcForkHeader())
406 if err != nil {
407 return fmt.Errorf("error sending resource fork header: %w", err)
408 }
409
410 rFile, err := hlFile.rsrcForkFile()
411 if err != nil {
412 return fmt.Errorf("error opening resource fork: %w", err)
413 }
414
415 if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil {
416 return fmt.Errorf("error sending resource fork: %w", err)
417 }
418 }
419
420 // Read the client's Next Action request. This is always 3, I think?
421 if _, err := io.ReadFull(rwc, nextAction); err != nil && err != io.EOF {
422 return fmt.Errorf("error reading client next action: %w", err)
423 }
424
425 return nil
426 })
427
428 if err != nil {
429 return err
430 }
431
432 return nil
433}
434
435func UploadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fileStore FileStore, rLogger *slog.Logger, preserveForks bool) error {
436
437 // Check if the target folder exists. If not, create it.
438 if _, err := fileStore.Stat(fullPath); os.IsNotExist(err) {
439 if err := fileStore.Mkdir(fullPath, 0777); err != nil {
440 return err
441 }
442 }
443
444 // Begin the folder upload flow by sending the "next file action" to client
445 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
446 return err
447 }
448
449 fileSize := make([]byte, 4)
450
451 for i := 0; i < fileTransfer.ItemCount(); i++ {
452 //s.Stats.UploadCounter += 1
453
454 var fu folderUpload
455 // TODO: implement io.Writer on folderUpload and replace this
456 if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil {
457 return err
458 }
459 if _, err := io.ReadFull(rwc, fu.IsFolder[:]); err != nil {
460 return err
461 }
462 if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil {
463 return err
464 }
465 fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes TODO: wat
466 if _, err := io.ReadFull(rwc, fu.FileNamePath); err != nil {
467 return err
468 }
469
470 if fu.IsFolder == [2]byte{0, 1} {
471 if _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath())); os.IsNotExist(err) {
472 if err := os.Mkdir(filepath.Join(fullPath, fu.FormattedPath()), 0777); err != nil {
473 return err
474 }
475 }
476
477 // Tell client to send next file
478 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
479 return err
480 }
481 } else {
482 nextAction := dlFldrActionSendFile
483
484 // Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip.
485 _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath()))
486 if err != nil && !errors.Is(err, fs.ErrNotExist) {
487 return err
488 }
489 if err == nil {
490 nextAction = dlFldrActionNextFile
491 }
492
493 // Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload.
494 incompleteFile, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath()+incompleteFileSuffix))
495 if err != nil && !errors.Is(err, fs.ErrNotExist) {
496 return err
497 }
498 if err == nil {
499 nextAction = dlFldrActionResumeFile
500 }
501
502 if _, err := rwc.Write([]byte{0, uint8(nextAction)}); err != nil {
503 return err
504 }
505
506 switch nextAction {
507 case dlFldrActionNextFile:
508 continue
509 case dlFldrActionResumeFile:
510 offset := make([]byte, 4)
511 binary.BigEndian.PutUint32(offset, uint32(incompleteFile.Size()))
512
513 file, err := os.OpenFile(fullPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
514 if err != nil {
515 return err
516 }
517
518 fileResumeData := NewFileResumeData([]ForkInfoList{*NewForkInfoList(offset)})
519
520 b, _ := fileResumeData.BinaryMarshal()
521
522 bs := make([]byte, 2)
523 binary.BigEndian.PutUint16(bs, uint16(len(b)))
524
525 if _, err := rwc.Write(append(bs, b...)); err != nil {
526 return err
527 }
528
529 if _, err := io.ReadFull(rwc, fileSize); err != nil {
530 return err
531 }
532
533 if err := receiveFile(rwc, file, io.Discard, io.Discard, fileTransfer.bytesSentCounter); err != nil {
534 rLogger.Error(err.Error())
535 }
536
537 err = os.Rename(fullPath+"/"+fu.FormattedPath()+".incomplete", fullPath+"/"+fu.FormattedPath())
538 if err != nil {
539 return err
540 }
541
542 case dlFldrActionSendFile:
543 if _, err := io.ReadFull(rwc, fileSize); err != nil {
544 return err
545 }
546
547 filePath := filepath.Join(fullPath, fu.FormattedPath())
548
549 hlFile, err := newFileWrapper(fileStore, filePath, 0)
550 if err != nil {
551 return err
552 }
553
554 rLogger.Info("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize))
555
556 incWriter, err := hlFile.incFileWriter()
557 if err != nil {
558 return err
559 }
560
561 rForkWriter := io.Discard
562 iForkWriter := io.Discard
563 if preserveForks {
564 iForkWriter, err = hlFile.infoForkWriter()
565 if err != nil {
566 return err
567 }
568
569 rForkWriter, err = hlFile.rsrcForkWriter()
570 if err != nil {
571 return err
572 }
573 }
574 if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil {
575 return err
576 }
577
578 if err := os.Rename(filePath+".incomplete", filePath); err != nil {
579 return err
580 }
581 }
582
583 // Tell client to send next fileWrapper
584 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
585 return err
586 }
587 }
588 }
589 rLogger.Info("Folder upload complete")
590 return nil
591}