/
record.go
2097 lines (1736 loc) · 46.3 KB
/
record.go
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 ClusterHQ
*
* 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 record defines interfaces for syscall records, executor and encoder/decoder.
// It also has implementations of all syscall records, gob encoder/decoder, and a common executor and the stdout
// executor.
package record
import (
"encoding/binary"
"errors"
"fmt"
"hash"
"io"
"log"
"os"
"os/user"
"path"
"reflect"
"strconv"
"syscall"
"time"
dlhash "github.com/ClusterHQ/fli/dl/hash"
"github.com/pkg/xattr"
)
// IMPORTANT: Any new record type should be register with gob
// encoding/decoding (see gob_encdec.go)
const (
// DefaultCreateMode - create file with these default permissions
DefaultCreateMode os.FileMode = 0777
// XattrPrefix is a prefix that is used for safely storing file attributes and extended attributes
// as extended attributes in the user namespace.
XattrPrefix string = "user.com.clusterhq.attributes."
// User and group names that are used when an ID can not be mapped to a name
// or a name is unknown on a system.
fallbackUser string = "nobody"
fallbackGroup1 string = "nogroup"
fallbackGroup2 string = "nobody"
)
type (
// Hdr has common fields for all records. Note: Export Hdr and Chksum because of GOB.
Hdr struct {
Chksum []byte
}
// Mkdir represents a record for mkdir
// Note: All record type can be unexported, but GOB needs to register them, so they are exported.
// Note: All fields are exported because gob only encodes exported fields
Mkdir struct {
Hdr
Path string
Mode os.FileMode
}
// Pwrite represents a record for pwrite
Pwrite struct {
Hdr
Path string
Data []byte
Offset uint64
Mode os.FileMode // always zero, kept for compatibility
}
// Hardlink represents a record for link
Hardlink struct {
Hdr
OldName string
NewName string
}
// Symlink represents a record for creation of a symlink
Symlink struct {
Hdr
OldName string
NewName string
}
// Truncate represents a record for truncation of a file
Truncate struct {
Hdr
Path string
Size int64
}
// Chown represents a record for doing a chown on a file
// TODO: might get absorbed into SETATTR record later
Chown struct {
Hdr
Path string
UID string // XXX stored as a name, not ID
GID string // XXX stored as a name, not ID
}
// Create represents a record for create/open/mkfile
Create struct {
Hdr
Path string
Mode os.FileMode
}
// Remove represents a record for removal of file or directory
Remove struct {
Hdr
Path string
}
// Setxattr represents a record for setting extended attribute
Setxattr struct {
Hdr
Path string
Xattr string
Data []byte
}
// Rmxattr represents a record for removing extended attribute
Rmxattr struct {
Hdr
Path string
Xattr string
}
// Mknod represent a record for mknod
Mknod struct {
Hdr
Path string
Mode uint32
Dev int
}
// Rename represents a record for rename
Rename struct {
Hdr
OldPath string
NewPath string
}
// Chmod represents a record for chmod
Chmod struct {
Hdr
Path string
Mode os.FileMode
}
// Setmtime represents a record for modify time
Setmtime struct {
Hdr
Path string
MTime int64
}
// EOT represents a special record which does nothing to the file system but indicates the end of a transfer
EOT struct {
Hdr
Name string
}
// ExecType defines the execution type for each record
ExecType int
// Type defines the record type for each record
Type int
)
const (
// AsyncExec is executed asynchronously(e.x. pwrie)
AsyncExec ExecType = iota
// SyncExec is executed synchronously(e.x. create dir)
SyncExec
// DelayExec is executed at the end of all asynchronous records are executed(e.x. setmtime)
DelayExec
// TypeMkdir defines the type ID
TypeMkdir Type = iota
// TypePwrite defines the type ID
TypePwrite
// TypeHardlink defines the type ID
TypeHardlink
// TypeSymlink defines the type ID
TypeSymlink
// TypeTruncate defines the type ID
TypeTruncate
// TypeChown defines the type ID
TypeChown
// TypeCreate defines the type ID
TypeCreate
// TypeRemove defines the type ID
TypeRemove
// TypeSetxattr defines the type ID
TypeSetxattr
// TypeRmxattr defines the type ID
TypeRmxattr
// TypeRename defines the type ID
TypeRename
// TypeMknod defines the type ID
TypeMknod
// TypeChmod defines the type ID
TypeChmod
// TypeSetmtime defines the type ID
TypeSetmtime
// TypeEOT defines the type ID
TypeEOT
)
// Record defines the function signature for all the common methods implemented for each record type
type Record interface {
Exec(root string) error
String() string
// Key returns a string that can be used to group records that belong to the same object(for example, file path)
Key() string
// ExeType returns the execution type of a record
ExecType() ExecType
// Type returns the type of a record
Type() Type
// ToBinary is the binary encoder which writes the record in binary format
ToBinary(io.Writer) error
// FromBinary reads the record from an encode source and populates the record with the data read from the source
FromBinary(io.Reader) error
// Chksum returns the checksum of a record.
// Note: It doesn't set the record's checksum field, so it can be used to recalculate the checksum for
// verification.
Chksum(dlhash.Factory) ([]byte, error)
// SetChksum sets the record's checksum
SetChksum([]byte)
// GetChksum returns the record's checksum
GetChksum() []byte
}
// SafeRecord is a subtype of Record that has a method for
// faking security-sensitive operations.
type SafeRecord interface {
Record
SafeExec(root string) error
}
// Note: Make sure -
// 1. All syscall records implement interface Record or SafeRecord
// 2. Add to encoder/decoder(GOB, binary)
var (
_ SafeRecord = &Mkdir{}
_ Record = &Pwrite{}
_ Record = &Hardlink{}
_ Record = &Symlink{}
_ Record = &Truncate{}
_ SafeRecord = &Chown{}
_ SafeRecord = &Create{}
_ Record = &Remove{}
_ Record = &Rename{}
_ SafeRecord = &Mknod{}
_ SafeRecord = &Setxattr{}
_ SafeRecord = &Rmxattr{}
_ SafeRecord = &Chmod{}
_ Record = &Setmtime{}
_ Record = &EOT{}
)
// SetChksum sets the record's checksum
func (h *Hdr) SetChksum(chksum []byte) {
// Note: TO get around the different encoder/decoder returns different empty []byte
if len(chksum) == 0 {
h.Chksum = nil
} else {
h.Chksum = chksum
}
}
// GetChksum returns the record's checksum
func (h *Hdr) GetChksum() []byte {
// Note: TO get around the different encoder/decoder returns different empty []byte
if len(h.Chksum) == 0 {
h.Chksum = nil
}
return h.Chksum
}
// MKDIR-SPECIFIC INTERFACE IMPLEMENTATION//////////////////////////////////
// The functions below describe common mkdir() related functionality.
// If there are special mkdir() actions required for a certain distribution,
// the functions below will need to be overriden by the code that is
// exectuing for that distribution.
// NewMkdir returns an record object of type mkdir from the parameters
func NewMkdir(path string, mode os.FileMode) Record {
return &Mkdir{
Path: path,
Mode: mode,
}
}
// Exec is the implementation of Record interface
func (rec *Mkdir) Exec(root string) error {
return os.Mkdir(path.Join(root, rec.Path), rec.Mode)
}
// SafeExec is implementation of FakeRecord interface
func (rec *Mkdir) SafeExec(root string) error {
err := os.Mkdir(path.Join(root, rec.Path), DefaultCreateMode)
if err != nil {
return err
}
return xattr.Setxattr(path.Join(root, rec.Path), XattrPrefix+"mode", []byte(strconv.FormatUint(uint64(rec.Mode), 8)))
}
// String is the implementation of Record interface
func (rec *Mkdir) String() string {
return fmt.Sprintf("%T: path = %s, mode = %d", rec, rec.Path, rec.Mode)
}
// Key is implementation of Record interface
func (rec *Mkdir) Key() string {
return rec.Path
}
// ExecType is implementation of Record interface
func (rec *Mkdir) ExecType() ExecType {
return SyncExec
}
// Type is implementation of Record interface
func (rec *Mkdir) Type() Type {
return TypeMkdir
}
// ToBinary is implementation of Record interface
func (rec *Mkdir) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err
}
err = writeString(target, rec.Path)
if err != nil {
return err
}
err = writeUint64(target, uint64(rec.Mode))
return err
}
// FromBinary is implementation of Record interface
func (rec *Mkdir) FromBinary(src io.Reader) error {
chksum, err := readBytes(src)
if err != nil {
return err
}
rec.Hdr.Chksum = chksum
path, err := readString(src)
if err != nil {
return err
}
rec.Path = path
n, err := readUint64(src)
if err != nil {
return err
}
rec.Mode = os.FileMode(n)
return nil
}
// Chksum is implementation of Record interface
func (rec *Mkdir) Chksum(hf dlhash.Factory) ([]byte, error) {
h := hf.New()
err := hashString(h, rec.Path)
if err != nil {
return nil, err
}
err = hashUint64(h, uint64(rec.Mode))
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// PWRITE SPECIFIC ////////////////////////////////////////////////////////
// The functions below describe common pwrite() related functionality.
// If there are special pwrite() actions required for a certain
// distribution, the functions below will need to be overriden by the
// code that is exectuing for that distribution.
// NewPwrite returns an record object of type pwrite from the parameters
func NewPwrite(path string, data []byte, offset uint64) Record {
return &Pwrite{
Path: path,
Data: data,
Offset: offset,
Mode: 0,
}
}
// Exec is implementation of Record interface
func (rec *Pwrite) Exec(root string) error {
// Open the file for writing; Note: mode is only needed with O_CREATE
fp, err := os.OpenFile(path.Join(root, rec.Path), os.O_WRONLY, 0)
if err != nil {
return err
}
defer fp.Close()
_, err = fp.WriteAt(rec.Data, int64(rec.Offset))
if err != nil {
return err
}
return nil
}
func (rec *Pwrite) String() string {
return fmt.Sprintf("%T: file = %s, len = %d, offset = %d",
rec, rec.Path, len(rec.Data), rec.Offset)
}
// Key is implementation of Record interface
func (rec *Pwrite) Key() string {
return rec.Path
}
// ExecType is implementation of Record interface
func (rec *Pwrite) ExecType() ExecType {
return AsyncExec
}
// Type is implementation of Record interface
func (rec *Pwrite) Type() Type {
return TypePwrite
}
// ToBinary is implementation of Record interface
func (rec *Pwrite) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err
}
err = writeString(target, rec.Path)
if err != nil {
return err
}
err = writeBytes(target, rec.Data)
if err != nil {
return err
}
err = writeUint64(target, rec.Offset)
if err != nil {
return err
}
err = writeUint64(target, uint64(rec.Mode))
return err
}
// FromBinary is implementation of Record interface
func (rec *Pwrite) FromBinary(src io.Reader) error {
chksum, err := readBytes(src)
if err != nil {
return err
}
rec.Hdr.Chksum = chksum
path, err := readString(src)
if err != nil {
return err
}
rec.Path = path
data, err := readBytes(src)
if err != nil {
return err
}
rec.Data = data
n, err := readUint64(src)
if err != nil {
return err
}
rec.Offset = n
n, err = readUint64(src)
if err != nil {
return err
}
rec.Mode = os.FileMode(n)
return nil
}
// Chksum is implementation of Record interface
func (rec *Pwrite) Chksum(hf dlhash.Factory) ([]byte, error) {
h := hf.New()
err := hashString(h, rec.Path)
if err != nil {
return nil, err
}
err = hashBytes(h, rec.Data)
if err != nil {
return nil, err
}
err = hashUint64(h, rec.Offset)
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// CREATE-SPECIFIC ////////////////////////////////////////////////////////////////
// The functions below describe common create/mkfile-related functionality.
// If there are special create actions required for a certain distribution,
// the functions below will need to be overriden by the code that is exectuing
// for that distribution.
// NewCreate returns an record object of type create from the parameters
func NewCreate(path string, mode os.FileMode) Record {
return &Create{
Path: path,
Mode: mode,
}
}
// Exec is implementation of Record interface
// Creates a new file. Corresponds to 'touch' or 'mkfile', etc.
func (rec *Create) Exec(root string) error {
fp, err := os.OpenFile(path.Join(root, rec.Path), os.O_WRONLY|os.O_CREATE|os.O_EXCL, rec.Mode)
if err != nil {
return err
}
fp.Close()
return nil
}
// SafeExec is implementation of Record interface
// Creates a new file. Corresponds to 'touch' or 'mkfile', etc.
func (rec *Create) SafeExec(root string) error {
// this will create the file with the default mode
fp, err := os.Create(path.Join(root, rec.Path))
if err != nil {
return err
}
defer fp.Close()
return xattr.Setxattr(path.Join(root, rec.Path), XattrPrefix+"mode", []byte(strconv.FormatUint(uint64(rec.Mode), 8)))
}
func (rec *Create) String() string {
return fmt.Sprintf("%T: file = %s, mode = %d", rec, rec.Path, rec.Mode)
}
// Key is implementation of Record interface
func (rec *Create) Key() string {
return rec.Path
}
// ExecType is implementation of Record interface
func (rec *Create) ExecType() ExecType {
return SyncExec
}
// Type is implementation of Record interface
func (rec *Create) Type() Type {
return TypeCreate
}
// ToBinary is implementation of Record interface
func (rec *Create) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err
}
err = writeString(target, rec.Path)
if err != nil {
return err
}
err = writeUint64(target, uint64(rec.Mode))
return err
}
// FromBinary is implementation of Record interface
func (rec *Create) FromBinary(src io.Reader) error {
chksum, err := readBytes(src)
if err != nil {
return err
}
rec.Hdr.Chksum = chksum
path, err := readString(src)
if err != nil {
return err
}
rec.Path = path
n, err := readUint64(src)
if err != nil {
return err
}
rec.Mode = os.FileMode(n)
return nil
}
// Chksum is implementation of Record interface
func (rec *Create) Chksum(hf dlhash.Factory) ([]byte, error) {
h := hf.New()
err := hashString(h, rec.Path)
if err != nil {
return nil, err
}
err = hashUint64(h, uint64(rec.Mode))
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// REMOVE-SPECIFIC INTERFACE IMPLEMENTATION//////////////////////////////////////////
// The functions below describe common remove() related functionality.
// If there are special remove() actions required for a certain distribution,
// the functions below will need to be overriden by the code that is executing
// for that distribution.
// NewRemove returns an record object of type remove from the parameters
func NewRemove(path string) Record {
return &Remove{
Path: path,
}
}
// Exec is implementation of Record interface
func (rec *Remove) Exec(root string) error {
return os.Remove(path.Join(root, rec.Path))
}
func (rec *Remove) String() string {
return fmt.Sprintf("%T: filename = %s", rec, rec.Path)
}
// Key is implementation of Record interface
func (rec *Remove) Key() string {
return rec.Path
}
// ExecType is implementation of Record interface
func (rec *Remove) ExecType() ExecType {
return SyncExec
}
// Type is implementation of Record interface
func (rec *Remove) Type() Type {
return TypeRemove
}
// ToBinary is implementation of Record interface
func (rec *Remove) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err
}
err = writeString(target, rec.Path)
return err
}
// FromBinary is implementation of Record interface
func (rec *Remove) FromBinary(src io.Reader) error {
chksum, err := readBytes(src)
if err != nil {
return err
}
rec.Hdr.Chksum = chksum
path, err := readString(src)
if err != nil {
return err
}
rec.Path = path
return nil
}
// Chksum is implementation of Record interface
func (rec *Remove) Chksum(hf dlhash.Factory) ([]byte, error) {
h := hf.New()
err := hashString(h, rec.Path)
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// LINK-SPECIFIC INTERFACE IMPLEMENTATION///////////////////////////////////////////
// The functions below describe common link() related functionality.
// If there are special link() actions required for a certain distribution,
// the functions below will need to be overriden by the code that is exectuing
// for that distribution.
// NewHardlink returns an record object of type hardlink from the parameters.
func NewHardlink(oldname string, newname string) Record {
return &Hardlink{
OldName: oldname,
NewName: newname,
}
}
// Exec is implementation of Record interface
func (rec *Hardlink) Exec(root string) error {
return os.Link(path.Join(root, rec.OldName), path.Join(root, rec.NewName))
}
func (rec *Hardlink) String() string {
return fmt.Sprintf("%T: oldname = %s, newname = %s", rec, rec.OldName, rec.NewName)
}
// Key is implementation of Record interface
func (rec *Hardlink) Key() string {
return rec.OldName
}
// ExecType is implementation of Record interface
func (rec *Hardlink) ExecType() ExecType {
return SyncExec
}
// Type is implementation of Record interface
func (rec *Hardlink) Type() Type {
return TypeHardlink
}
// ToBinary is implementation of Record interface
func (rec *Hardlink) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err
}
err = writeString(target, rec.OldName)
if err != nil {
return err
}
err = writeString(target, rec.NewName)
return err
}
// FromBinary is implementation of Record interface
func (rec *Hardlink) FromBinary(src io.Reader) error {
chksum, err := readBytes(src)
if err != nil {
return err
}
rec.Hdr.Chksum = chksum
path, err := readString(src)
if err != nil {
return err
}
rec.OldName = path
path, err = readString(src)
if err != nil {
return err
}
rec.NewName = path
return nil
}
// Chksum is implementation of Record interface
func (rec *Hardlink) Chksum(hf dlhash.Factory) ([]byte, error) {
h := hf.New()
err := hashString(h, rec.OldName)
if err != nil {
return nil, err
}
err = hashString(h, rec.NewName)
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// SYMLINK-SPECIFIC INTERFACE IMPLEMENTATION////////////////////////////////////////
// The functions below describe common symlink-related functionality.
// If there are special symlink actions required for a certain distribution,
// the functions below will need to be overriden by the code that is executing
// for that distribution.
// NewSymlink returns an record object of type symlink from the parameters.
func NewSymlink(oldname string, newname string) Record {
return &Symlink{
OldName: oldname,
NewName: newname,
}
}
// Exec is implementation of Record interface
// Note: code executing this needs to have appropriate permissions (s.a. be run as superuser)
func (rec *Symlink) Exec(root string) error {
// NOTE: The target path to the symlink always be under the root. The differ should take care of this and the
// target path will be relative to the symlink. Hence we can create the symlink's to target link without
// altering the path.
return os.Symlink(rec.OldName, path.Join(root, rec.NewName))
}
func (rec *Symlink) String() string {
return fmt.Sprintf("%T: oldname = %s, newname = %s", rec, rec.OldName, rec.NewName)
}
// Key is implementation of Record interface
func (rec *Symlink) Key() string {
return rec.OldName
}
// ExecType is implementation of Record interface
func (rec *Symlink) ExecType() ExecType {
return SyncExec
}
// Type is implementation of Record interface
func (rec *Symlink) Type() Type {
return TypeSymlink
}
// ToBinary is implementation of Record interface
func (rec *Symlink) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err
}
err = writeString(target, rec.OldName)
if err != nil {
return err
}
err = writeString(target, rec.NewName)
return err
}
// FromBinary is implementation of Record interface
func (rec *Symlink) FromBinary(src io.Reader) error {
chksum, err := readBytes(src)
if err != nil {
return err
}
rec.Hdr.Chksum = chksum
path, err := readString(src)
if err != nil {
return err
}
rec.OldName = path
path, err = readString(src)
if err != nil {
return err
}
rec.NewName = path
return nil
}
// Chksum is implementation of Record interface
func (rec *Symlink) Chksum(hf dlhash.Factory) ([]byte, error) {
h := hf.New()
err := hashString(h, rec.OldName)
if err != nil {
return nil, err
}
err = hashString(h, rec.NewName)
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// TRUNCATE-SPECIFIC INTERFACE IMPLEMENTATION///////////////////////////////////////
// The functions below describe common truncate-related functionality.
// If there are special truncate actions required for a certain distribution,
// the functions below will need to be overridden by the code that is executing
// for that distribution.
// NewTruncate returns an record object of type truncate from the parameters.
func NewTruncate(filepath string, size int64) Record {
return &Truncate{
Path: filepath,
Size: size,
}
}
// Exec is implementation of Record interface
func (rec *Truncate) Exec(root string) error {
return os.Truncate(path.Join(root, rec.Path), rec.Size)
}
func (rec *Truncate) String() string {
return fmt.Sprintf("%T: filename = %s, size = %d", rec, rec.Path, rec.Size)
}
// Key is implementation of Record interface
func (rec *Truncate) Key() string {
return rec.Path
}
// ExecType is implementation of Record interface
func (rec *Truncate) ExecType() ExecType {
return SyncExec
}
// Type is implementation of Record interface
func (rec *Truncate) Type() Type {
return TypeTruncate
}
// ToBinary is implementation of Record interface
func (rec *Truncate) ToBinary(target io.Writer) error {
err := writeUint64(target, uint64(rec.Type()))
if err != nil {
return err
}
err = writeBytes(target, rec.Hdr.Chksum)
if err != nil {
return err