// SPDX-License-Identifier: GPL-3.0-or-later package build import ( "fmt" "os" "path/filepath" "sort" "strings" ) // motorinoMarker is the file motorino leaves in a folder it emptied with the // clean option. A folder that has it, or has nothing in it, may be emptied again; // any other folder never is. It is only written when the option is used, so the // folders of a build without it hold the site and nothing else. const motorinoMarker = ".motorino-out" // writeOutput writes the files (path with slashes, relative to out) into out and // returns the files that were already in out and are not among them: the pages // of an earlier build that this one no longer makes. With clean set it first // empties out, but only a folder that is new, empty or marked by an earlier clean // build, and never the folder of the site or one that holds it. It does not // touch the folder before every file is known, so a content error leaves it as // it was. func writeOutput(out, siteDir string, clean bool, files map[string][]byte) ([]string, error) { if clean { if err := emptyOutput(out, siteDir); err != nil { return nil, err } files = withMarker(files) } for rel, data := range files { if err := writeFile(out, rel, data); err != nil { return nil, err } } return staleFiles(out, files) } // withMarker returns the files and the marker. func withMarker(files map[string][]byte) map[string][]byte { marked := make(map[string][]byte, len(files)+1) for rel, data := range files { marked[rel] = data } marked[motorinoMarker] = []byte("This folder is written by motorino and emptied by motorino build -clean and motorino gemini -clean.\n") return marked } // emptyOutput removes what is in out, after checking that it may be removed. func emptyOutput(out, siteDir string) error { absOut, err := filepath.Abs(out) if err != nil { return err } absSite, err := filepath.Abs(siteDir) if err != nil { return err } if rel, err := filepath.Rel(absOut, absSite); err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { return fmt.Errorf("refusing to empty %s: it is the folder of the site or holds it", out) } entries, err := os.ReadDir(out) if os.IsNotExist(err) || (err == nil && len(entries) == 0) { return nil } if err != nil { return fmt.Errorf("read %s: %w", out, err) } if info, err := os.Lstat(filepath.Join(out, motorinoMarker)); err != nil || !info.Mode().IsRegular() { return fmt.Errorf("refusing to empty %s: it has files and no %s, which motorino leaves in a folder it emptied; empty it yourself this first time", out, motorinoMarker) } for _, entry := range entries { if err := os.RemoveAll(filepath.Join(out, entry.Name())); err != nil { return fmt.Errorf("empty %s: %w", out, err) } } return nil } // staleFiles lists the files under out that are not among the written ones, with // slashes, sorted. The marker is never one of them. func staleFiles(out string, written map[string][]byte) ([]string, error) { var stale []string err := filepath.WalkDir(out, func(path string, entry os.DirEntry, err error) error { if err != nil || entry.IsDir() { return err } rel, err := filepath.Rel(out, path) if err != nil { return err } rel = filepath.ToSlash(rel) if _, ok := written[rel]; !ok && rel != motorinoMarker { stale = append(stale, rel) } return nil }) if err != nil { return nil, fmt.Errorf("read %s: %w", out, err) } sort.Strings(stale) return stale, nil }