github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/directory.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package setting
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  )
    12  
    13  // PrepareAppDataPath creates app data directory if necessary
    14  func PrepareAppDataPath() error {
    15  	// FIXME: There are too many calls to MkdirAll in old code. It is incorrect.
    16  	// For example, if someDir=/mnt/vol1/gitbundle-home/data, if the mount point /mnt/vol1 is not mounted when GitBundle runs,
    17  	// then gitbundle will make new empty directories in /mnt/vol1, all are stored in the root filesystem.
    18  	// The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories.
    19  	// For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK).
    20  	// 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.
    21  
    22  	st, err := os.Stat(AppDataPath)
    23  
    24  	if os.IsNotExist(err) {
    25  		err = os.MkdirAll(AppDataPath, os.ModePerm)
    26  		if err != nil {
    27  			return fmt.Errorf("unable to create the APP_DATA_PATH directory: %q, Error: %v", AppDataPath, err)
    28  		}
    29  		return nil
    30  	}
    31  
    32  	if err != nil {
    33  		return fmt.Errorf("unable to use APP_DATA_PATH %q. Error: %v", AppDataPath, err)
    34  	}
    35  
    36  	if !st.IsDir() /* also works for symlink */ {
    37  		return fmt.Errorf("the APP_DATA_PATH %q is not a directory (or symlink to a directory) and can't be used", AppDataPath)
    38  	}
    39  
    40  	return nil
    41  }