2016-01-13 19:38:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-01-14 17:17:35 +00:00
|
|
|
"math/rand"
|
2016-01-13 19:38:45 +00:00
|
|
|
"os"
|
2020-01-28 18:49:46 +00:00
|
|
|
"os/signal"
|
2016-01-13 19:38:45 +00:00
|
|
|
"path"
|
2018-01-21 13:07:44 +00:00
|
|
|
"strconv"
|
2016-01-13 19:38:45 +00:00
|
|
|
"strings"
|
2020-01-28 18:49:46 +00:00
|
|
|
"syscall"
|
2016-01-14 17:17:35 +00:00
|
|
|
"time"
|
2016-01-13 19:38:45 +00:00
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
2016-12-30 11:45:14 +00:00
|
|
|
"srs.epita.fr/fic-server/settings"
|
|
|
|
|
|
|
|
"gopkg.in/fsnotify.v1"
|
2016-01-13 19:38:45 +00:00
|
|
|
)
|
|
|
|
|
2016-01-23 11:28:31 +00:00
|
|
|
var TeamsDir string
|
2016-01-13 19:38:45 +00:00
|
|
|
var SubmissionDir string
|
|
|
|
|
2016-12-08 08:14:42 +00:00
|
|
|
func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
|
2016-01-13 19:38:45 +00:00
|
|
|
log.Println("Watch new directory:", pathname)
|
2016-12-08 08:14:42 +00:00
|
|
|
if err := watcher.Add(pathname); err != nil {
|
2016-01-13 19:38:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ds, err := ioutil.ReadDir(pathname); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
for _, d := range ds {
|
|
|
|
p := path.Join(pathname, d.Name())
|
2019-07-11 17:52:13 +00:00
|
|
|
if d.IsDir() && d.Name() != ".tmp" && d.Mode()&os.ModeSymlink == 0 {
|
2016-01-13 19:38:45 +00:00
|
|
|
if err := watchsubdir(watcher, p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-23 11:28:31 +00:00
|
|
|
} else if d.Mode().IsRegular() {
|
|
|
|
go treat(p)
|
2016-01-13 19:38:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 18:49:46 +00:00
|
|
|
func walkAndTreat(pathname string) error {
|
|
|
|
if ds, err := ioutil.ReadDir(pathname); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
for _, d := range ds {
|
|
|
|
p := path.Join(pathname, d.Name())
|
|
|
|
if d.IsDir() && d.Name() != ".tmp" && d.Mode()&os.ModeSymlink == 0 {
|
|
|
|
if err := walkAndTreat(p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if d.Mode().IsRegular() {
|
|
|
|
treat(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 15:48:45 +00:00
|
|
|
var ChStarted = false
|
2017-01-24 01:14:28 +00:00
|
|
|
var lastRegeneration time.Time
|
2018-12-09 18:46:18 +00:00
|
|
|
var skipInitialGeneration = false
|
2017-01-24 01:14:28 +00:00
|
|
|
|
2022-05-01 20:15:16 +00:00
|
|
|
func reloadSettings(config *settings.Settings) {
|
2019-02-04 17:14:46 +00:00
|
|
|
allowRegistration = config.AllowRegistration
|
|
|
|
canJoinTeam = config.CanJoinTeam
|
2020-05-16 01:51:36 +00:00
|
|
|
denyTeamCreation = config.DenyTeamCreation
|
2023-11-04 20:14:16 +00:00
|
|
|
canResetProgression = config.WorkInProgress && config.CanResetProgression
|
2019-01-17 11:03:18 +00:00
|
|
|
fic.HintCoefficient = config.HintCurCoefficient
|
|
|
|
fic.WChoiceCoefficient = config.WChoiceCurCoefficient
|
2019-01-17 12:26:49 +00:00
|
|
|
fic.ExerciceCurrentCoefficient = config.ExerciceCurCoefficient
|
2019-07-30 17:31:38 +00:00
|
|
|
ChStarted = config.Start.Unix() > 0 && time.Since(config.Start) >= 0
|
2023-07-10 07:17:02 +00:00
|
|
|
fic.PartialValidation = config.PartialValidation
|
|
|
|
fic.UnlockedChallengeDepth = config.UnlockedChallengeDepth
|
|
|
|
fic.UnlockedChallengeUpTo = config.UnlockedChallengeUpTo
|
2024-03-16 10:28:59 +00:00
|
|
|
fic.UnlockedStandaloneExercices = config.UnlockedStandaloneExercices
|
2024-03-17 09:17:39 +00:00
|
|
|
fic.UnlockedStandaloneExercicesByThemeStepValidation = config.UnlockedStandaloneExercicesByThemeStepValidation
|
|
|
|
fic.UnlockedStandaloneExercicesByStandaloneExerciceValidation = config.UnlockedStandaloneExercicesByStandaloneExerciceValidation
|
2023-07-10 07:17:02 +00:00
|
|
|
fic.DisplayAllFlags = config.DisplayAllFlags
|
|
|
|
|
|
|
|
fic.FirstBlood = config.FirstBlood
|
|
|
|
fic.SubmissionCostBase = config.SubmissionCostBase
|
|
|
|
fic.SubmissionUniqueness = config.SubmissionUniqueness
|
|
|
|
fic.GlobalScoreCoefficient = config.GlobalScoreCoefficient
|
|
|
|
fic.CountOnlyNotGoodTries = config.CountOnlyNotGoodTries
|
|
|
|
fic.DiscountedFactor = config.DiscountedFactor
|
2016-12-30 11:45:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 19:38:45 +00:00
|
|
|
func main() {
|
2017-10-17 04:47:10 +00:00
|
|
|
var dsn = flag.String("dsn", fic.DSNGenerator(), "DSN to connect to the MySQL server")
|
2023-07-10 07:17:02 +00:00
|
|
|
flag.StringVar(&generatorSocket, "generator", "./GENERATOR/generator.socket", "Path to the generator socket")
|
2022-06-08 00:51:17 +00:00
|
|
|
flag.StringVar(&settings.SettingsDir, "settings", "./SETTINGSDIST", "Base directory where load and save settings")
|
2016-01-13 19:38:45 +00:00
|
|
|
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
|
2016-12-30 11:45:14 +00:00
|
|
|
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
2017-11-22 01:39:52 +00:00
|
|
|
var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
|
2016-01-13 19:38:45 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2023-07-10 07:17:02 +00:00
|
|
|
log.SetPrefix("[checker] ")
|
2016-10-13 17:02:41 +00:00
|
|
|
|
2017-11-25 15:05:03 +00:00
|
|
|
settings.SettingsDir = path.Clean(settings.SettingsDir)
|
2016-01-13 19:38:45 +00:00
|
|
|
SubmissionDir = path.Clean(SubmissionDir)
|
2016-12-30 11:45:14 +00:00
|
|
|
TeamsDir = path.Clean(TeamsDir)
|
2016-01-13 19:38:45 +00:00
|
|
|
|
2016-01-14 17:17:35 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2016-01-13 19:38:45 +00:00
|
|
|
log.Println("Creating submission directory...")
|
2020-01-17 06:04:32 +00:00
|
|
|
if _, err := os.Stat(path.Join(SubmissionDir, ".tmp")); os.IsNotExist(err) {
|
2023-07-14 14:49:57 +00:00
|
|
|
if err := os.MkdirAll(path.Join(SubmissionDir, ".tmp"), 0700); err != nil {
|
2016-01-13 19:38:45 +00:00
|
|
|
log.Fatal("Unable to create submission directory: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Opening DB...")
|
2017-10-17 04:47:10 +00:00
|
|
|
if err := fic.DBInit(*dsn); err != nil {
|
2016-01-13 19:38:45 +00:00
|
|
|
log.Fatal("Cannot open the database: ", err)
|
|
|
|
}
|
|
|
|
defer fic.DBClose()
|
|
|
|
|
2016-12-30 11:45:14 +00:00
|
|
|
// Load configuration
|
2017-11-25 15:05:03 +00:00
|
|
|
settings.LoadAndWatchSettings(path.Join(settings.SettingsDir, settings.SettingsFile), reloadSettings)
|
2016-12-30 11:45:14 +00:00
|
|
|
|
2016-01-13 19:38:45 +00:00
|
|
|
log.Println("Registering directory events...")
|
2016-12-08 08:14:42 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
2016-01-13 19:38:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-12-08 08:14:42 +00:00
|
|
|
defer watcher.Close()
|
2016-01-13 19:38:45 +00:00
|
|
|
|
|
|
|
if err := watchsubdir(watcher, SubmissionDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-07-10 07:17:02 +00:00
|
|
|
// Register SIGUSR1 and SIGTERM
|
2020-01-28 18:49:46 +00:00
|
|
|
interrupt1 := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt1, syscall.SIGUSR1)
|
2023-07-10 07:17:02 +00:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, syscall.SIGTERM)
|
2020-01-28 18:49:46 +00:00
|
|
|
|
2017-11-22 01:57:24 +00:00
|
|
|
watchedNotify := fsnotify.Create
|
|
|
|
|
2023-07-10 07:17:02 +00:00
|
|
|
loop:
|
2016-01-13 19:38:45 +00:00
|
|
|
for {
|
|
|
|
select {
|
2023-07-10 07:17:02 +00:00
|
|
|
case <-interrupt:
|
|
|
|
break loop
|
2020-01-28 18:49:46 +00:00
|
|
|
case <-interrupt1:
|
|
|
|
log.Println("SIGUSR1 received, retreating all files in queue...")
|
|
|
|
walkAndTreat(SubmissionDir)
|
|
|
|
log.Println("SIGUSR1 treated.")
|
2016-12-08 08:14:42 +00:00
|
|
|
case ev := <-watcher.Events:
|
2019-07-11 17:52:13 +00:00
|
|
|
if d, err := os.Lstat(ev.Name); err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
|
2016-01-13 19:38:45 +00:00
|
|
|
// Register new subdirectory
|
2017-04-05 00:02:10 +00:00
|
|
|
if err := watchsubdir(watcher, ev.Name); err != nil {
|
|
|
|
log.Println(err)
|
2016-01-13 19:38:45 +00:00
|
|
|
}
|
2022-06-08 15:13:41 +00:00
|
|
|
} else if err == nil && ev.Op&watchedNotify == watchedNotify && d.Mode().IsRegular() {
|
2018-01-16 22:59:51 +00:00
|
|
|
if *debugINotify {
|
|
|
|
log.Println("Treating event:", ev, "for", ev.Name)
|
|
|
|
}
|
2016-01-23 11:28:31 +00:00
|
|
|
go treat(ev.Name)
|
2022-06-08 15:13:41 +00:00
|
|
|
} else if err == nil && ev.Op&fsnotify.Write == fsnotify.Write {
|
2017-11-22 01:57:24 +00:00
|
|
|
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
|
|
|
|
watchedNotify = fsnotify.Write
|
2023-10-23 08:03:32 +00:00
|
|
|
go treat(ev.Name)
|
2022-06-08 15:13:41 +00:00
|
|
|
} else if err == nil && *debugINotify {
|
2017-11-22 01:39:52 +00:00
|
|
|
log.Println("Skipped event:", ev, "for", ev.Name)
|
2016-01-13 19:38:45 +00:00
|
|
|
}
|
2016-12-08 08:14:42 +00:00
|
|
|
case err := <-watcher.Errors:
|
2016-01-13 19:38:45 +00:00
|
|
|
log.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-23 11:28:31 +00:00
|
|
|
|
|
|
|
func treat(raw_path string) {
|
|
|
|
// Extract
|
|
|
|
spath := strings.Split(strings.TrimPrefix(raw_path, SubmissionDir), "/")
|
|
|
|
|
2016-03-06 17:59:33 +00:00
|
|
|
if len(spath) == 3 {
|
|
|
|
if spath[1] == "_registration" {
|
2017-12-21 21:18:18 +00:00
|
|
|
treatRegistration(raw_path, spath[2])
|
2018-01-21 13:18:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:39:41 +00:00
|
|
|
var teamid int64
|
|
|
|
var err error
|
2018-01-21 13:18:26 +00:00
|
|
|
|
2019-01-16 17:39:41 +00:00
|
|
|
if teamid, err = strconv.ParseInt(spath[1], 10, 64); err != nil {
|
|
|
|
if lnk, err := os.Readlink(path.Join(TeamsDir, spath[1])); err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("[ERR] Unable to readlink %q: %s\n", path.Join(TeamsDir, spath[1]), err)
|
2019-01-16 16:16:11 +00:00
|
|
|
return
|
|
|
|
} else if teamid, err = strconv.ParseInt(lnk, 10, 64); err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("[ERR] Error during ParseInt team %q: %s\n", lnk, err)
|
2019-01-16 16:16:11 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-16 17:39:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:35:07 +00:00
|
|
|
var team *fic.Team
|
2019-01-16 17:39:41 +00:00
|
|
|
if team, err = fic.GetTeam(teamid); err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("[ERR] Unable to retrieve team %d: %s\n", teamid, err)
|
2018-01-21 13:18:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch spath[2] {
|
|
|
|
case "name":
|
2016-03-06 17:59:33 +00:00
|
|
|
treatRename(raw_path, team)
|
2020-01-20 14:56:02 +00:00
|
|
|
case "issue":
|
|
|
|
treatIssue(raw_path, team)
|
2018-01-21 13:18:26 +00:00
|
|
|
case "hint":
|
2016-12-09 10:49:29 +00:00
|
|
|
treatOpeningHint(raw_path, team)
|
2018-12-02 22:18:32 +00:00
|
|
|
case "choices":
|
|
|
|
treatWantChoices(raw_path, team)
|
2023-11-04 20:14:16 +00:00
|
|
|
case "reset_progress":
|
|
|
|
treatResetProgress(raw_path, team)
|
2023-04-06 13:27:10 +00:00
|
|
|
case ".locked":
|
|
|
|
treatLocked(raw_path, team)
|
2018-01-21 13:18:26 +00:00
|
|
|
default:
|
2016-03-06 17:59:33 +00:00
|
|
|
treatSubmission(raw_path, team, spath[2])
|
|
|
|
}
|
2016-01-23 11:28:31 +00:00
|
|
|
} else {
|
|
|
|
log.Println("Invalid new file:", raw_path)
|
|
|
|
}
|
|
|
|
}
|