2025-10-21 09:41:18 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/ncw/directio"
|
|
|
|
|
)
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
func report(err error, fd *os.File) bool {
|
2025-10-21 09:41:18 +02:00
|
|
|
slog.Debug("failed to check io", "error", err)
|
|
|
|
|
|
|
|
|
|
if fd != nil {
|
|
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
|
slog.Debug("failed to close filehandle", "error", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
// Calls runcheck* with timeout
|
|
|
|
|
func runExporter(file string, alloc *Alloc, timeout time.Duration, op int) bool {
|
2025-10-21 09:41:18 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
run := make(chan struct{}, 1)
|
|
|
|
|
var res bool
|
|
|
|
|
|
|
|
|
|
go func() {
|
2025-10-22 18:02:26 +02:00
|
|
|
switch op {
|
|
|
|
|
case O_R:
|
|
|
|
|
res = runcheck_r(file, alloc)
|
|
|
|
|
case O_W:
|
|
|
|
|
res = runcheck_w(file, alloc)
|
|
|
|
|
}
|
2025-10-21 09:41:18 +02:00
|
|
|
run <- struct{}{}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
2025-10-22 18:02:26 +02:00
|
|
|
return report(ctx.Err(), nil)
|
2025-10-21 09:41:18 +02:00
|
|
|
case <-run:
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks file io on the specified path:
|
|
|
|
|
//
|
2025-10-22 18:02:26 +02:00
|
|
|
// - opens it for reading
|
2025-10-21 09:41:18 +02:00
|
|
|
// - reads the block
|
|
|
|
|
// - closes file again
|
|
|
|
|
//
|
|
|
|
|
// Returns false if anything failed during that sequence,
|
|
|
|
|
// true otherwise.
|
2025-10-22 18:02:26 +02:00
|
|
|
func runcheck_r(file string, alloc *Alloc) bool {
|
|
|
|
|
// read
|
|
|
|
|
in, err := directio.OpenFile(file, os.O_RDONLY, 0640)
|
2025-10-21 09:41:18 +02:00
|
|
|
if err != nil {
|
2025-10-22 18:02:26 +02:00
|
|
|
report(err, nil)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
n, err := io.ReadFull(in, alloc.readBlock)
|
2025-10-21 09:41:18 +02:00
|
|
|
if err != nil {
|
2025-10-22 18:02:26 +02:00
|
|
|
return report(err, in)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n != len(alloc.writeBlock) {
|
2025-10-22 18:02:26 +02:00
|
|
|
return report(errors.New("failed to read block"), in)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
if err := in.Close(); err != nil {
|
|
|
|
|
return report(err, nil)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks file io on the specified path:
|
|
|
|
|
//
|
|
|
|
|
// - open the file (create if it doesnt exist)
|
|
|
|
|
// - truncate it if it already exists
|
|
|
|
|
// - write some data to it
|
|
|
|
|
// - closes the file
|
|
|
|
|
//
|
|
|
|
|
// Returns false if anything failed during that sequence,
|
|
|
|
|
// true otherwise.
|
|
|
|
|
func runcheck_w(file string, alloc *Alloc) bool {
|
|
|
|
|
// write
|
|
|
|
|
fd, err := directio.OpenFile(file, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0640)
|
2025-10-21 09:41:18 +02:00
|
|
|
if err != nil {
|
2025-10-22 18:02:26 +02:00
|
|
|
report(err, nil)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
for i := 0; i < len(alloc.writeBlock); i++ {
|
|
|
|
|
alloc.writeBlock[i] = 'A'
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
n, err := fd.Write(alloc.writeBlock)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return report(err, fd)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
if n != len(alloc.writeBlock) {
|
|
|
|
|
return report(errors.New("failed to write block"), fd)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 18:02:26 +02:00
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
|
return report(err, nil)
|
2025-10-21 09:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|