Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle with read the empty file #4258

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions core/filex/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func firstLine(file *os.File) (string, error) {
for {
buf := make([]byte, bufSize)
n, err := file.ReadAt(buf, offset)

kevwan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && err != io.EOF {
return "", err
}
Expand All @@ -45,6 +46,10 @@ func firstLine(file *os.File) (string, error) {
}
}

if err == io.EOF {
return string(append(first, buf[:n]...)), nil
}

first = append(first, buf[:n]...)
offset += bufSize
}
Expand All @@ -56,31 +61,44 @@ func lastLine(filename string, file *os.File) (string, error) {
return "", err
}

bf := int64(bufSize)
var last []byte
offset := info.Size()
for {
offset -= bufSize
if offset < 0 {
if offset < bufSize {
bf = offset
offset = 0
} else {
offset -= bf
}
buf := make([]byte, bufSize)

buf := make([]byte, bf)
n, err := file.ReadAt(buf, offset)
if err != nil && err != io.EOF {
return "", err
}

if n == 0 {
return "", nil
}

if buf[n-1] == '\n' {
buf = buf[:n-1]
n--
} else {
buf = buf[:n]
}

for n--; n >= 0; n-- {
if buf[n] == '\n' {
return string(append(buf[n+1:], last...)), nil
}
}

last = append(buf, last...)

if offset == 0 {
return string(last), nil
}
}
}
41 changes: 41 additions & 0 deletions core/filex/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ last line`
second line
last line
`
emptyContent = ``
)

func TestFirstLine(t *testing.T) {
Expand Down Expand Up @@ -79,6 +80,26 @@ func TestFirstLineError(t *testing.T) {
assert.Error(t, err)
}

func TestFirstLineEmptyFile(t *testing.T) {
filename, err := fs.TempFilenameWithText(emptyContent)
assert.Nil(t, err)
defer os.Remove(filename)

val, err := FirstLine(filename)
assert.Nil(t, err)
assert.Equal(t, "", val)
}

func TestFirstLineWithoutNewline(t *testing.T) {
filename, err := fs.TempFilenameWithText(longLine)
assert.Nil(t, err)
defer os.Remove(filename)

val, err := FirstLine(filename)
assert.Nil(t, err)
assert.Equal(t, longLine, val)
}

func TestLastLine(t *testing.T) {
filename, err := fs.TempFilenameWithText(text)
assert.Nil(t, err)
Expand All @@ -99,6 +120,16 @@ func TestLastLineWithLastNewline(t *testing.T) {
assert.Equal(t, longLine, val)
}

func TestLastLineWithoutLastNewline(t *testing.T) {
filename, err := fs.TempFilenameWithText(longLine)
assert.Nil(t, err)
defer os.Remove(filename)

val, err := LastLine(filename)
assert.Nil(t, err)
assert.Equal(t, longLine, val)
}

func TestLastLineShort(t *testing.T) {
filename, err := fs.TempFilenameWithText(shortText)
assert.Nil(t, err)
Expand All @@ -123,3 +154,13 @@ func TestLastLineError(t *testing.T) {
_, err := LastLine("/tmp/does-not-exist")
assert.Error(t, err)
}

func TestLastLineEmptyFile(t *testing.T) {
filename, err := fs.TempFilenameWithText(emptyContent)
assert.Nil(t, err)
defer os.Remove(filename)

val, err := LastLine(filename)
assert.Nil(t, err)
assert.Equal(t, "", val)
}