• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12301186252

12 Dec 2024 05:01PM UTC coverage: 48.92% (-9.7%) from 58.642%
12301186252

push

github

web-flow
Merge pull request #9309 from yyforyongyu/fix-unit-test

chainntnfs: fix `TestHistoricalConfDetailsTxIndex`

99121 of 202617 relevant lines covered (48.92%)

1.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.86
/chanbackup/backupfile.go
1
package chanbackup
2

3
import (
4
        "fmt"
5
        "os"
6
        "path/filepath"
7

8
        "github.com/lightningnetwork/lnd/keychain"
9
)
10

11
const (
12
        // DefaultBackupFileName is the default name of the auto updated static
13
        // channel backup fie.
14
        DefaultBackupFileName = "channel.backup"
15

16
        // DefaultTempBackupFileName is the default name of the temporary SCB
17
        // file that we'll use to atomically update the primary back up file
18
        // when new channel are detected.
19
        DefaultTempBackupFileName = "temp-dont-use.backup"
20
)
21

22
var (
23
        // ErrNoBackupFileExists is returned if caller attempts to call
24
        // UpdateAndSwap with the file name not set.
25
        ErrNoBackupFileExists = fmt.Errorf("back up file name not set")
26

27
        // ErrNoTempBackupFile is returned if caller attempts to call
28
        // UpdateAndSwap with the temp back up file name not set.
29
        ErrNoTempBackupFile = fmt.Errorf("temp backup file not set")
30
)
31

32
// MultiFile represents a file on disk that a caller can use to read the packed
33
// multi backup into an unpacked one, and also atomically update the contents
34
// on disk once new channels have been opened, and old ones closed. This struct
35
// relies on an atomic file rename property which most widely use file systems
36
// have.
37
type MultiFile struct {
38
        // fileName is the file name of the main back up file.
39
        fileName string
40

41
        // tempFileName is the name of the file that we'll use to stage a new
42
        // packed multi-chan backup, and the rename to the main back up file.
43
        tempFileName string
44

45
        // tempFile is an open handle to the temp back up file.
46
        tempFile *os.File
47
}
48

49
// NewMultiFile create a new multi-file instance at the target location on the
50
// file system.
51
func NewMultiFile(fileName string) *MultiFile {
3✔
52

3✔
53
        // We'll our temporary backup file in the very same directory as the
3✔
54
        // main backup file.
3✔
55
        backupFileDir := filepath.Dir(fileName)
3✔
56
        tempFileName := filepath.Join(
3✔
57
                backupFileDir, DefaultTempBackupFileName,
3✔
58
        )
3✔
59

3✔
60
        return &MultiFile{
3✔
61
                fileName:     fileName,
3✔
62
                tempFileName: tempFileName,
3✔
63
        }
3✔
64
}
3✔
65

66
// UpdateAndSwap will attempt write a new temporary backup file to disk with
67
// the newBackup encoded, then atomically swap (via rename) the old file for
68
// the new file by updating the name of the new file to the old.
69
func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
3✔
70
        // If the main backup file isn't set, then we can't proceed.
3✔
71
        if b.fileName == "" {
3✔
72
                return ErrNoBackupFileExists
×
73
        }
×
74

75
        log.Infof("Updating backup file at %v", b.fileName)
3✔
76

3✔
77
        // If the old back up file still exists, then we'll delete it before
3✔
78
        // proceeding.
3✔
79
        if _, err := os.Stat(b.tempFileName); err == nil {
3✔
80
                log.Infof("Found old temp backup @ %v, removing before swap",
×
81
                        b.tempFileName)
×
82

×
83
                err = os.Remove(b.tempFileName)
×
84
                if err != nil {
×
85
                        return fmt.Errorf("unable to remove temp "+
×
86
                                "backup file: %v", err)
×
87
                }
×
88
        }
89

90
        // Now that we know the staging area is clear, we'll create the new
91
        // temporary back up file.
92
        var err error
3✔
93
        b.tempFile, err = os.Create(b.tempFileName)
3✔
94
        if err != nil {
6✔
95
                return fmt.Errorf("unable to create temp file: %w", err)
3✔
96
        }
3✔
97

98
        // With the file created, we'll write the new packed multi backup and
99
        // remove the temporary file all together once this method exits.
100
        _, err = b.tempFile.Write([]byte(newBackup))
3✔
101
        if err != nil {
3✔
102
                return fmt.Errorf("unable to write backup to temp file: %w",
×
103
                        err)
×
104
        }
×
105
        if err := b.tempFile.Sync(); err != nil {
3✔
106
                return fmt.Errorf("unable to sync temp file: %w", err)
×
107
        }
×
108
        defer os.Remove(b.tempFileName)
3✔
109

3✔
110
        log.Infof("Swapping old multi backup file from %v to %v",
3✔
111
                b.tempFileName, b.fileName)
3✔
112

3✔
113
        // Before we rename the swap (atomic name swap), we'll make
3✔
114
        // sure to close the current file as some OSes don't support
3✔
115
        // renaming a file that's already open (Windows).
3✔
116
        if err := b.tempFile.Close(); err != nil {
3✔
117
                return fmt.Errorf("unable to close file: %w", err)
×
118
        }
×
119

120
        // Finally, we'll attempt to atomically rename the temporary file to
121
        // the main back up file. If this succeeds, then we'll only have a
122
        // single file on disk once this method exits.
123
        return os.Rename(b.tempFileName, b.fileName)
3✔
124
}
125

126
// ExtractMulti attempts to extract the packed multi backup we currently point
127
// to into an unpacked version. This method will fail if no backup file
128
// currently exists as the specified location.
129
func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) {
3✔
130
        var err error
3✔
131

3✔
132
        // We'll return an error if the main file isn't currently set.
3✔
133
        if b.fileName == "" {
3✔
134
                return nil, ErrNoBackupFileExists
×
135
        }
×
136

137
        // Now that we've confirmed the target file is populated, we'll read
138
        // all the contents of the file. This function ensures that file is
139
        // always closed, even if we can't read the contents.
140
        multiBytes, err := os.ReadFile(b.fileName)
3✔
141
        if err != nil {
6✔
142
                return nil, err
3✔
143
        }
3✔
144

145
        // Finally, we'll attempt to unpack the file and return the unpack
146
        // version to the caller.
147
        packedMulti := PackedMulti(multiBytes)
3✔
148
        return packedMulti.Unpack(keyChain)
3✔
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc