9
9
"encoding/base64"
10
10
"errors"
11
11
"fmt"
12
+ "io"
12
13
"io/fs"
14
+ "log"
13
15
"path"
14
16
"strings"
15
17
"time"
@@ -101,15 +103,15 @@ func (c WaveClient) Read(ctx context.Context, conn *connparse.Connection, data w
101
103
}
102
104
103
105
func (c WaveClient ) ReadTarStream (ctx context.Context , conn * connparse.Connection , opts * wshrpc.FileCopyOpts ) <- chan wshrpc.RespOrErrorUnion [iochantypes.Packet ] {
104
- pathPrefix , err := cleanPath (conn .Path )
105
- if err != nil {
106
- return wshutil.SendErrCh [iochantypes.Packet ](fmt .Errorf ("error cleaning path: %w" , err ))
107
- }
106
+ log .Printf ("ReadTarStream: conn: %v, opts: %v\n " , conn , opts )
108
107
list , err := c .ListEntries (ctx , conn , nil )
109
108
if err != nil {
110
109
return wshutil.SendErrCh [iochantypes.Packet ](fmt .Errorf ("error listing blockfiles: %w" , err ))
111
110
}
112
111
112
+ pathPrefix := getPathPrefix (conn )
113
+ schemeAndHost := conn .GetSchemeAndHost () + "/"
114
+
113
115
timeout := time .Millisecond * 100
114
116
if opts .Timeout > 0 {
115
117
timeout = time .Duration (opts .Timeout ) * time .Millisecond
@@ -127,6 +129,7 @@ func (c WaveClient) ReadTarStream(ctx context.Context, conn *connparse.Connectio
127
129
rtn <- wshutil.RespErr [iochantypes.Packet ](readerCtx .Err ())
128
130
return
129
131
}
132
+ file .Mode = 0644
130
133
131
134
if err = writeHeader (fileutil .ToFsFileInfo (file ), file .Path ); err != nil {
132
135
rtn <- wshutil.RespErr [iochantypes.Packet ](fmt .Errorf ("error writing tar header: %w" , err ))
@@ -136,7 +139,11 @@ func (c WaveClient) ReadTarStream(ctx context.Context, conn *connparse.Connectio
136
139
continue
137
140
}
138
141
139
- _ , dataBuf , err := filestore .WFS .ReadFile (ctx , conn .Host , file .Path )
142
+ log .Printf ("ReadTarStream: reading file: %s\n " , file .Path )
143
+
144
+ internalPath := strings .TrimPrefix (file .Path , schemeAndHost )
145
+
146
+ _ , dataBuf , err := filestore .WFS .ReadFile (ctx , conn .Host , internalPath )
140
147
if err != nil {
141
148
rtn <- wshutil.RespErr [iochantypes.Packet ](fmt .Errorf ("error reading blockfile: %w" , err ))
142
149
return
@@ -168,10 +175,14 @@ func (c WaveClient) ListEntriesStream(ctx context.Context, conn *connparse.Conne
168
175
}
169
176
170
177
func (c WaveClient ) ListEntries (ctx context.Context , conn * connparse.Connection , opts * wshrpc.FileListOpts ) ([]* wshrpc.FileInfo , error ) {
178
+ log .Printf ("ListEntries: conn: %v, opts: %v\n " , conn , opts )
171
179
zoneId := conn .Host
172
180
if zoneId == "" {
173
181
return nil , fmt .Errorf ("zoneid not found in connection" )
174
182
}
183
+ if opts == nil {
184
+ opts = & wshrpc.FileListOpts {}
185
+ }
175
186
prefix , err := cleanPath (conn .Path )
176
187
if err != nil {
177
188
return nil , fmt .Errorf ("error cleaning path: %w" , err )
@@ -432,13 +443,19 @@ func (c WaveClient) CopyRemote(ctx context.Context, srcConn, destConn *connparse
432
443
if zoneId == "" {
433
444
return fmt .Errorf ("zoneid not found in connection" )
434
445
}
446
+ destPrefix := getPathPrefix (destConn )
447
+ destPrefix = strings .TrimPrefix (destPrefix , destConn .GetSchemeAndHost ()+ "/" )
448
+ log .Printf ("CopyRemote: srcConn: %v, destConn: %v, destPrefix: %s\n " , srcConn , destConn , destPrefix )
435
449
readCtx , cancel := context .WithCancelCause (ctx )
436
450
ioch := srcClient .ReadTarStream (readCtx , srcConn , opts )
437
451
err := tarcopy .TarCopyDest (readCtx , cancel , ioch , func (next * tar.Header , reader * tar.Reader ) error {
438
452
if next .Typeflag == tar .TypeDir {
439
453
return nil
440
454
}
441
- fileName , err := cleanPath (path .Join (destConn .Path , next .Name ))
455
+ fileName , err := cleanPath (path .Join (destPrefix , next .Name ))
456
+ if err != nil {
457
+ return fmt .Errorf ("error cleaning path: %w" , err )
458
+ }
442
459
_ , err = filestore .WFS .Stat (ctx , zoneId , fileName )
443
460
if err != nil {
444
461
if ! errors .Is (err , fs .ErrNotExist ) {
@@ -449,12 +466,15 @@ func (c WaveClient) CopyRemote(ctx context.Context, srcConn, destConn *connparse
449
466
return fmt .Errorf ("error making blockfile: %w" , err )
450
467
}
451
468
}
469
+ log .Printf ("CopyRemote: writing file: %s; size: %d\n " , fileName , next .Size )
452
470
dataBuf := make ([]byte , next .Size )
453
- n , err : = reader .Read (dataBuf )
471
+ _ , err = reader .Read (dataBuf )
454
472
if err != nil {
455
- return fmt .Errorf ("error reading tar data: %w" , err )
473
+ if ! errors .Is (err , io .EOF ) {
474
+ return fmt .Errorf ("error reading tar data: %w" , err )
475
+ }
456
476
}
457
- err = filestore .WFS .WriteFile (ctx , zoneId , fileName , dataBuf [: n ] )
477
+ err = filestore .WFS .WriteFile (ctx , zoneId , fileName , dataBuf )
458
478
if err != nil {
459
479
return fmt .Errorf ("error writing to blockfile: %w" , err )
460
480
}
@@ -535,3 +555,13 @@ func cleanPath(path string) (string, error) {
535
555
func (c WaveClient ) GetConnectionType () string {
536
556
return connparse .ConnectionTypeWave
537
557
}
558
+
559
+ func getPathPrefix (conn * connparse.Connection ) string {
560
+ fullUri := conn .GetFullURI ()
561
+ pathPrefix := fullUri
562
+ lastSlash := strings .LastIndex (fullUri , "/" )
563
+ if lastSlash > 10 && lastSlash < len (fullUri )- 1 {
564
+ pathPrefix = fullUri [:lastSlash + 1 ]
565
+ }
566
+ return pathPrefix
567
+ }
0 commit comments