package main // A path in the module's folder the watcher does not know (design 08 sign-in-page ยง12; lead answer L-1): the folder's // `unknown` is `module`, so the node-engine does nothing inside it and the watcher reacts. It leaves such a path as it // is and writes it to its journal once per path: at its start for what is there, and at each file event of the kernel // that makes one. import ( "os" "path/filepath" "regexp" "strings" ) // keptFile is a kept event's name in pending/, or one being written beside it. var keptFile = regexp.MustCompile(`^[0-9]{10}\.json(\.new)?$`) // knownPath says whether path is one of the folder's own: watch.sock, check-address, pending/ and its files, // names.json, sequence, or a file being written beside one of them. func (f Folder) knownPath(path string) bool { rel, err := filepath.Rel(f.Dir, path) if err != nil || strings.HasPrefix(rel, "..") { return false } dir, name := filepath.Split(rel) switch dir { case "": name = strings.TrimSuffix(name, ".new") return name == socketName || name == checkName || name == pendingName || name == namesName || name == sequenceName case pendingName + "/": return keptFile.MatchString(name) } return false } // unknownAtStart is every path in the folder and in pending/ the watcher does not know. func (f Folder) unknownAtStart() []string { var out []string for _, dir := range []string{f.Dir, filepath.Join(f.Dir, pendingName)} { entries, _ := os.ReadDir(dir) for _, e := range entries { if p := filepath.Join(dir, e.Name()); !f.knownPath(p) { out = append(out, p) } } } return out } // strange writes a path the watcher does not know to its journal, once per path, and leaves it as it is. func (w *Watcher) strange(path string) { if w.Folder.knownPath(path) || w.told[path] { return } if w.told == nil { w.told = map[string]bool{} } w.told[path] = true journal("%s is not the module's: left as it is", path) }