forked from mtusi2026/mtusi2026lab6
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
// Package fault implements explicit, file-controlled barriers for the isolated lab.
|
|
package fault
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func Hit(ctx context.Context, point string, id uuid.UUID) {
|
|
if os.Getenv("LAB5_FAULTS") != "1" {
|
|
return
|
|
}
|
|
p := filepath.Join("/faults", point+"."+id.String())
|
|
b, e := os.ReadFile(p)
|
|
if e != nil {
|
|
p = filepath.Join("/faults", point+".all")
|
|
b, e = os.ReadFile(p)
|
|
if e != nil {
|
|
return
|
|
}
|
|
}
|
|
hit, e := os.OpenFile(p+".hit", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
|
|
if e != nil {
|
|
return
|
|
}
|
|
_, _ = hit.WriteString(id.String() + "\n")
|
|
_ = hit.Close()
|
|
switch strings.TrimSpace(string(b)) {
|
|
case "crash":
|
|
_ = os.Remove(p)
|
|
os.Exit(86)
|
|
case "pause":
|
|
for {
|
|
if _, e = os.Stat(p); os.IsNotExist(e) {
|
|
return
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func Error(point string, id uuid.UUID) error {
|
|
if os.Getenv("LAB5_FAULTS") != "1" {
|
|
return nil
|
|
}
|
|
b, e := os.ReadFile(filepath.Join("/faults", point+"."+id.String()))
|
|
if e != nil {
|
|
return nil
|
|
}
|
|
v := strings.TrimSpace(string(b))
|
|
if v == "temporary" || v == "permanent" {
|
|
return errors.New(v)
|
|
}
|
|
return nil
|
|
}
|