You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

directory.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "fmt"
  6. "os"
  7. )
  8. // PrepareAppDataPath creates app data directory if necessary
  9. func PrepareAppDataPath() error {
  10. // FIXME: There are too many calls to MkdirAll in old code. It is incorrect.
  11. // For example, if someDir=/mnt/vol1/gitea-home/data, if the mount point /mnt/vol1 is not mounted when Gitea runs,
  12. // then gitea will make new empty directories in /mnt/vol1, all are stored in the root filesystem.
  13. // The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories.
  14. // For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK).
  15. // Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future.
  16. st, err := os.Stat(AppDataPath)
  17. if os.IsNotExist(err) {
  18. err = os.MkdirAll(AppDataPath, os.ModePerm)
  19. if err != nil {
  20. return fmt.Errorf("unable to create the APP_DATA_PATH directory: %q, Error: %w", AppDataPath, err)
  21. }
  22. return nil
  23. }
  24. if err != nil {
  25. return fmt.Errorf("unable to use APP_DATA_PATH %q. Error: %w", AppDataPath, err)
  26. }
  27. if !st.IsDir() /* also works for symlink */ {
  28. return fmt.Errorf("the APP_DATA_PATH %q is not a directory (or symlink to a directory) and can't be used", AppDataPath)
  29. }
  30. return nil
  31. }