Skip to content

Commit b52aa4f

Browse files
committed
scanner.Scan() have limit 64k bytes string length, fix #1305
Signed-off-by: Slach <[email protected]>
1 parent 9c2d3e2 commit b52aa4f

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

pkg/backup/restore.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,16 +567,29 @@ func (b *Backuper) restoreRBACResolveAllConflicts(ctx context.Context, backupNam
567567
return openErr
568568
}
569569

570-
scanner := bufio.NewScanner(file)
571-
for scanner.Scan() {
572-
line := scanner.Text()
570+
reader := bufio.NewReader(file)
571+
for {
572+
line, readErr := reader.ReadString('\n')
573+
if readErr != nil && readErr != io.EOF {
574+
return readErr
575+
}
576+
line = strings.TrimSuffix(line, "\n")
577+
if line == "" {
578+
if readErr == io.EOF {
579+
break
580+
}
581+
continue
582+
}
573583
data := keeper.DumpNode{}
574584
jsonErr := json.Unmarshal([]byte(line), &data)
575585
if jsonErr != nil {
576586
//convert from old format
577587
dataString := keeper.DumpNodeString{}
578588
if jsonErr = json.Unmarshal([]byte(line), &dataString); jsonErr != nil {
579589
log.Error().Msgf("can't %s json.Unmarshal error: %v line: %s", fPath, line, jsonErr)
590+
if readErr == io.EOF {
591+
break
592+
}
580593
continue
581594
}
582595
data.Path = dataString.Path
@@ -588,10 +601,9 @@ func (b *Backuper) restoreRBACResolveAllConflicts(ctx context.Context, backupNam
588601
}
589602
log.Debug().Msgf("%s:%s b.resolveRBACConflictIfExist(%s) no error", fPath, data.Path, string(data.Value))
590603
}
591-
592-
}
593-
if scanErr := scanner.Err(); scanErr != nil {
594-
return scanErr
604+
if readErr == io.EOF {
605+
break
606+
}
595607
}
596608

597609
if closeErr := file.Close(); closeErr != nil {
@@ -958,15 +970,28 @@ func (b *Backuper) restoreNamedCollections(backupName string) error {
958970
if openErr != nil {
959971
return openErr
960972
}
961-
scanner := bufio.NewScanner(file)
962-
for scanner.Scan() {
963-
line := scanner.Bytes()
973+
reader := bufio.NewReader(file)
974+
for {
975+
line, readErr := reader.ReadString('\n')
976+
if readErr != nil && readErr != io.EOF {
977+
return errors.Wrapf(readErr, "read error on %s", jsonlFile)
978+
}
979+
line = strings.TrimSuffix(line, "\n")
980+
if line == "" {
981+
if readErr == io.EOF {
982+
break
983+
}
984+
continue
985+
}
964986
var node keeper.DumpNode
965-
if unmarshalErr := json.Unmarshal(line, &node); unmarshalErr != nil {
987+
if unmarshalErr := json.Unmarshal([]byte(line), &node); unmarshalErr != nil {
966988
return errors.Wrapf(unmarshalErr, "failed to unmarshal from %s", jsonlFile)
967989
}
968990
var sqlQuery string
969991
if len(node.Value) == 0 {
992+
if readErr == io.EOF {
993+
break
994+
}
970995
continue
971996
}
972997
if isEncrypted {
@@ -981,6 +1006,9 @@ func (b *Backuper) restoreNamedCollections(backupName string) error {
9811006
sqlQuery = strings.TrimSpace(sqlQuery)
9821007
if sqlQuery == "" {
9831008
log.Warn().Msgf("Empty SQL content in line from: %s", jsonlFile)
1009+
if readErr == io.EOF {
1010+
break
1011+
}
9841012
continue
9851013
}
9861014

@@ -1009,9 +1037,9 @@ func (b *Backuper) restoreNamedCollections(backupName string) error {
10091037
}
10101038

10111039
log.Info().Msgf("Restored SQL named collection from jsonl: %s", collectionName)
1012-
}
1013-
if err := scanner.Err(); err != nil {
1014-
return errors.Wrapf(err, "scanner error on %s", jsonlFile)
1040+
if readErr == io.EOF {
1041+
break
1042+
}
10151043
}
10161044
if err := file.Close(); err != nil {
10171045
log.Warn().Msgf("can't close %s error: %v", jsonlFile, err)

0 commit comments

Comments
 (0)