|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.skaffold.filesystem; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.util.Locale; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Properties; |
| 26 | +import java.util.logging.Logger; |
| 27 | + |
| 28 | +/** |
| 29 | + * Obtains an OS-specific user cache directory based on the XDG Base Directory Specification. |
| 30 | + * |
| 31 | + * <p>Specifically, from the specification: |
| 32 | + * |
| 33 | + * <ul> |
| 34 | + * <li>This directory is defined by the environment variable {@code $XDG_CACHE_HOME}. |
| 35 | + * <li>If {@code $XDG_CACHE_HOME} is either not set or empty, a default equal to {@code |
| 36 | + * $HOME/.cache} should be used. |
| 37 | + * </ul> |
| 38 | + * |
| 39 | + * @see <a |
| 40 | + * href="https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html">https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html</a> |
| 41 | + */ |
| 42 | +public class UserCacheHome { |
| 43 | + |
| 44 | + private static final Logger logger = Logger.getLogger(UserCacheHome.class.getName()); |
| 45 | + |
| 46 | + public static Path getCacheHome() { |
| 47 | + return getCacheHome(System.getProperties(), System.getenv()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns {@code $XDG_CACHE_HOME}, if available, or resolves the OS-specific user cache home |
| 52 | + * based. |
| 53 | + * |
| 54 | + * <p>For Linus, this is {@code $HOME/.cache/}. |
| 55 | + * |
| 56 | + * <p>For Windows, this is {@code %LOCALAPPDATA%}. |
| 57 | + * |
| 58 | + * <p>For macOS, this is {@code $HOME/Library/Application Support/}. |
| 59 | + */ |
| 60 | + @VisibleForTesting |
| 61 | + static Path getCacheHome(Properties properties, Map<String, String> environment) { |
| 62 | + // Use environment variable $XDG_CACHE_HOME if set and not empty. |
| 63 | + String xdgCacheHome = environment.get("XDG_CACHE_HOME"); |
| 64 | + if (xdgCacheHome != null && !xdgCacheHome.trim().isEmpty()) { |
| 65 | + return Paths.get(xdgCacheHome); |
| 66 | + } |
| 67 | + |
| 68 | + String userHome = properties.getProperty("user.home"); |
| 69 | + |
| 70 | + Path userHomeDirectory = Paths.get(userHome); |
| 71 | + Path xdgPath = userHomeDirectory.resolve(".cache"); |
| 72 | + |
| 73 | + String rawOsName = properties.getProperty("os.name"); |
| 74 | + String osName = rawOsName.toLowerCase(Locale.ENGLISH); |
| 75 | + |
| 76 | + if (osName.contains("linux")) { |
| 77 | + return xdgPath; |
| 78 | + |
| 79 | + } else if (osName.contains("windows")) { |
| 80 | + // Use %LOCALAPPDATA% for Windows. |
| 81 | + String localAppDataEnv = environment.get("LOCALAPPDATA"); |
| 82 | + if (localAppDataEnv == null || localAppDataEnv.trim().isEmpty()) { |
| 83 | + logger.warning("LOCALAPPDATA environment is invalid or missing"); |
| 84 | + return xdgPath; |
| 85 | + } |
| 86 | + Path localAppData = Paths.get(localAppDataEnv); |
| 87 | + if (!Files.exists(localAppData)) { |
| 88 | + logger.warning(localAppData + " does not exist"); |
| 89 | + return xdgPath; |
| 90 | + } |
| 91 | + return localAppData; |
| 92 | + |
| 93 | + } else if (osName.contains("mac") || osName.contains("darwin")) { |
| 94 | + // Use '~/Library/Application Support/' for macOS. |
| 95 | + Path applicationSupport = userHomeDirectory.resolve("Library").resolve("Application Support"); |
| 96 | + if (!Files.exists(applicationSupport)) { |
| 97 | + logger.warning(applicationSupport + " does not exist"); |
| 98 | + return xdgPath; |
| 99 | + } |
| 100 | + return applicationSupport; |
| 101 | + } |
| 102 | + |
| 103 | + throw new IllegalStateException("Unknown OS: " + rawOsName); |
| 104 | + } |
| 105 | + |
| 106 | + private UserCacheHome() {} |
| 107 | +} |
0 commit comments