const maxFileNameLength … const maxPathLength … type AtomicWriter … type FileProjection … // NewAtomicWriter creates a new AtomicWriter configured to write to the given // target directory, or returns an error if the target directory does not exist. func NewAtomicWriter(targetDir string, logContext string) (*AtomicWriter, error) { … } const dataDirName … const newDataDirName … // Write does an atomic projection of the given payload into the writer's target // directory. Input paths must not begin with '..'. // setPerms is an optional pointer to a function that caller can provide to set the // permissions of the newly created files before they are published. The function is // passed subPath which is the name of the timestamped directory that was created // under target directory. // // The Write algorithm is: // // 1. The payload is validated; if the payload is invalid, the function returns // // 2. The current timestamped directory is detected by reading the data directory // symlink // // 3. The old version of the volume is walked to determine whether any // portion of the payload was deleted and is still present on disk. // // 4. The data in the current timestamped directory is compared to the projected // data to determine if an update to data directory is required. // // 5. A new timestamped dir is created if an update is required. // // 6. The payload is written to the new timestamped directory. // // 7. Permissions are set (if setPerms is not nil) on the new timestamped directory and files. // // 8. A symlink to the new timestamped directory ..data_tmp is created that will // become the new data directory. // // 9. The new data directory symlink is renamed to the data directory; rename is atomic. // // 10. Symlinks and directory for new user-visible files are created (if needed). // // For example, consider the files: // <target-dir>/podName // <target-dir>/user/labels // <target-dir>/k8s/annotations // // The user visible files are symbolic links into the internal data directory: // <target-dir>/podName -> ..data/podName // <target-dir>/usr -> ..data/usr // <target-dir>/k8s -> ..data/k8s // // The data directory itself is a link to a timestamped directory with // the real data: // <target-dir>/..data -> ..2016_02_01_15_04_05.12345678/ // NOTE(claudiub): We need to create these symlinks AFTER we've finished creating and // linking everything else. On Windows, if a target does not exist, the created symlink // will not work properly if the target ends up being a directory. // // 11. Old paths are removed from the user-visible portion of the target directory. // // 12. The previous timestamped directory is removed, if it exists. func (w *AtomicWriter) Write(payload map[string]FileProjection, setPerms func(subPath string) error) error { … } // validatePayload returns an error if any path in the payload returns a copy of the payload with the paths cleaned. func validatePayload(payload map[string]FileProjection) (map[string]FileProjection, error) { … } // validatePath validates a single path, returning an error if the path is // invalid. paths may not: // // 1. be absolute // 2. contain '..' as an element // 3. start with '..' // 4. contain filenames larger than 255 characters // 5. be longer than 4096 characters func validatePath(targetPath string) error { … } // shouldWritePayload returns whether the payload should be written to disk. func shouldWritePayload(payload map[string]FileProjection, oldTsDir string) (bool, error) { … } // shouldWriteFile returns whether a new version of a file should be written to disk. func shouldWriteFile(path string, content []byte) (bool, error) { … } // pathsToRemove walks the current version of the data directory and // determines which paths should be removed (if any) after the payload is // written to the target directory. func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTSDir string) (sets.Set[string], error) { … } // newTimestampDir creates a new timestamp directory func (w *AtomicWriter) newTimestampDir() (string, error) { … } // writePayloadToDir writes the given payload to the given directory. The // directory must exist. func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir string) error { … } // createUserVisibleFiles creates the relative symlinks for all the // files configured in the payload. If the directory in a file path does not // exist, it is created. // // Viz: // For files: "bar", "foo/bar", "baz/bar", "foo/baz/blah" // the following symlinks are created: // bar -> ..data/bar // foo -> ..data/foo // baz -> ..data/baz func (w *AtomicWriter) createUserVisibleFiles(payload map[string]FileProjection) error { … } // removeUserVisiblePaths removes the set of paths from the user-visible // portion of the writer's target directory. func (w *AtomicWriter) removeUserVisiblePaths(paths sets.Set[string]) error { … }