Skip to content

Commit 44bd15c

Browse files
committed
refactor: extract saving file into own method
1 parent ed649ea commit 44bd15c

1 file changed

Lines changed: 40 additions & 36 deletions

File tree

internal/certificatetransparency/logmetrics.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -206,52 +206,56 @@ func (m *LogMetrics) LoadCTIndex(ctIndexFilePath string) {
206206
// permanent index file. This prevents the last good index file from being clobbered if the program was shutdown/killed
207207
// in-between the write operation.
208208
func (m *LogMetrics) SaveCertIndexesAtInterval(interval time.Duration, ctIndexFilePath string) {
209-
210209
tempFilePath := fmt.Sprintf("%s.tmp", ctIndexFilePath)
211210

212211
ticker := time.NewTicker(interval)
213212
defer ticker.Stop()
214213

215214
for range ticker.C {
216-
// Get the index data
217-
ctIndex := m.GetAllCTIndexes()
218-
bytes, cerr := json.MarshalIndent(ctIndex, "", " ")
219-
if cerr != nil {
220-
log.Panic(cerr)
221-
}
215+
m.SaveCertIndexes(tempFilePath, ctIndexFilePath)
216+
}
217+
}
222218

223-
// Save data to a temporary file first
224-
file, openErr := os.OpenFile(tempFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
225-
if openErr != nil {
226-
log.Println("Could not save CT index to temporary file: ", openErr)
227-
continue
228-
}
219+
// SaveCertIndexes saves the index of CTLogs to a file.
220+
func (m *LogMetrics) SaveCertIndexes(tempFilePath, ctIndexFilePath string) {
221+
// Get the index data
222+
ctIndex := m.GetAllCTIndexes()
223+
bytes, cerr := json.MarshalIndent(ctIndex, "", " ")
224+
if cerr != nil {
225+
log.Panic(cerr)
226+
}
229227

230-
truncateErr := file.Truncate(0)
231-
if truncateErr != nil {
232-
log.Println("Error truncating CT index temp file: ", truncateErr)
233-
continue
234-
}
235-
// TODO: check for short writes
236-
_, writeErr := file.Write(bytes)
237-
if writeErr != nil {
238-
log.Println("Error writing to CT index temp file: ", writeErr)
239-
continue
240-
}
241-
syncErr := file.Sync()
242-
if syncErr != nil {
243-
log.Println("Error syncing CT index temp file: ", syncErr)
244-
continue
245-
}
228+
// Save data to a temporary file first
229+
file, openErr := os.OpenFile(tempFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
230+
if openErr != nil {
231+
log.Println("Could not save CT index to temporary file: ", openErr)
232+
return
233+
}
246234

247-
file.Close()
235+
truncateErr := file.Truncate(0)
236+
if truncateErr != nil {
237+
log.Println("Error truncating CT index temp file: ", truncateErr)
238+
return
239+
}
240+
// TODO: check for short writes
241+
_, writeErr := file.Write(bytes)
242+
if writeErr != nil {
243+
log.Println("Error writing to CT index temp file: ", writeErr)
244+
return
245+
}
246+
syncErr := file.Sync()
247+
if syncErr != nil {
248+
log.Println("Error syncing CT index temp file: ", syncErr)
249+
return
250+
}
248251

249-
// Atomically move the temp file to the permanent file
250-
renameErr := os.Rename(tempFilePath, ctIndexFilePath)
251-
if renameErr != nil {
252-
log.Println("Error renaming CT index temp file: ", renameErr)
253-
continue
254-
}
252+
file.Close()
253+
254+
// Atomically move the temp file to the permanent file
255+
renameErr := os.Rename(tempFilePath, ctIndexFilePath)
256+
if renameErr != nil {
257+
log.Println("Error renaming CT index temp file: ", renameErr)
258+
return
255259
}
256260
}
257261

0 commit comments

Comments
 (0)