github.com/containerd/nerdctl@v1.7.7/pkg/rootlessutil/xdg_linux.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package rootlessutil
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"path/filepath"
    23  )
    24  
    25  func XDGRuntimeDir() (string, error) {
    26  	if xrd := os.Getenv("XDG_RUNTIME_DIR"); xrd != "" {
    27  		return xrd, nil
    28  	}
    29  	// Fall back to "/run/user/<euid>".
    30  	// Note that We cannot rely on os.Geteuid() because we might be inside UserNS.
    31  	if euid := os.Getenv("ROOTLESSKIT_PARENT_EUID"); euid != "" {
    32  		return "/run/user/" + euid, nil
    33  	}
    34  	return "", errors.New("environment variable XDG_RUNTIME_DIR is not set, see https://rootlesscontaine.rs/getting-started/common/login/")
    35  }
    36  
    37  func XDGConfigHome() (string, error) {
    38  	if xch := os.Getenv("XDG_CONFIG_HOME"); xch != "" {
    39  		return xch, nil
    40  	}
    41  	// Fall back to "~/.config"
    42  	// Note that We cannot rely on user.Current().HomeDir because we might be inside UserNS.
    43  	home := os.Getenv("HOME")
    44  	if home == "" {
    45  		return "", errors.New("environment variable HOME is not set")
    46  	}
    47  	return filepath.Join(home, ".config"), nil
    48  }
    49  
    50  func XDGDataHome() (string, error) {
    51  	if xdh := os.Getenv("XDG_DATA_HOME"); xdh != "" {
    52  		return xdh, nil
    53  	}
    54  	// Fall back to "~/.local/share"
    55  	// Note that We cannot rely on user.Current().HomeDir because we might be inside UserNS.
    56  	home := os.Getenv("HOME")
    57  	if home == "" {
    58  		return "", errors.New("environment variable HOME is not set")
    59  	}
    60  	return filepath.Join(home, ".local/share"), nil
    61  }