Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.config; 16 : 17 : import static java.nio.file.Files.isExecutable; 18 : import static java.nio.file.Files.isRegularFile; 19 : 20 : import com.google.common.flogger.FluentLogger; 21 : import com.google.inject.Inject; 22 : import com.google.inject.Singleton; 23 : import java.nio.file.Path; 24 : import java.nio.file.Paths; 25 : import org.eclipse.jgit.lib.Config; 26 : 27 : @Singleton 28 : public class GitwebCgiConfig { 29 99 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 30 : 31 : public GitwebCgiConfig disabled() { 32 0 : return new GitwebCgiConfig(); 33 : } 34 : 35 : private final Path cgi; 36 : private final Path css; 37 : private final Path js; 38 : private final Path logoPng; 39 : 40 : @Inject 41 99 : GitwebCgiConfig(SitePaths sitePaths, @GerritServerConfig Config cfg) { 42 99 : if (GitwebConfig.isDisabled(cfg)) { 43 92 : cgi = null; 44 92 : css = null; 45 92 : js = null; 46 92 : logoPng = null; 47 92 : return; 48 : } 49 : 50 15 : String cfgCgi = cfg.getString("gitweb", null, "cgi"); 51 15 : Path pkgCgi = Paths.get("/usr/lib/cgi-bin/gitweb.cgi"); 52 15 : String[] resourcePaths = { 53 : "/usr/share/gitweb/static", "/usr/share/gitweb", "/var/www/static", "/var/www", 54 : }; 55 : Path cgi; 56 : 57 15 : if (cfgCgi != null) { 58 : // Use the CGI script configured by the administrator, failing if it 59 : // cannot be used as specified. 60 : // 61 0 : cgi = sitePaths.resolve(cfgCgi); 62 0 : if (!isRegularFile(cgi)) { 63 0 : throw new IllegalStateException("Cannot find gitweb.cgi: " + cgi); 64 : } 65 0 : if (!isExecutable(cgi)) { 66 0 : throw new IllegalStateException("Cannot execute gitweb.cgi: " + cgi); 67 : } 68 : 69 0 : if (!cgi.equals(pkgCgi)) { 70 : // Assume the administrator pointed us to the distribution, 71 : // which also has the corresponding CSS and logo file. 72 : // 73 0 : String absPath = cgi.getParent().toAbsolutePath().toString(); 74 0 : resourcePaths = new String[] {absPath + "/static", absPath}; 75 0 : } 76 : 77 15 : } else if (cfg.getString("gitweb", null, "url") != null) { 78 : // Use an externally managed gitweb instance, and not an internal one. 79 : // 80 0 : cgi = null; 81 0 : resourcePaths = new String[] {}; 82 : 83 15 : } else if (isRegularFile(pkgCgi) && isExecutable(pkgCgi)) { 84 : // Use the OS packaged CGI. 85 : // 86 0 : logger.atFine().log("Assuming gitweb at %s", pkgCgi); 87 0 : cgi = pkgCgi; 88 : 89 : } else { 90 15 : logger.atWarning().log("gitweb not installed (no %s found)", pkgCgi); 91 15 : cgi = null; 92 15 : resourcePaths = new String[] {}; 93 : } 94 : 95 15 : Path css = null; 96 15 : Path js = null; 97 15 : Path logo = null; 98 15 : for (String path : resourcePaths) { 99 0 : Path dir = Paths.get(path); 100 0 : css = dir.resolve("gitweb.css"); 101 0 : js = dir.resolve("gitweb.js"); 102 0 : logo = dir.resolve("git-logo.png"); 103 0 : if (isRegularFile(css) && isRegularFile(logo)) { 104 0 : break; 105 : } 106 : } 107 : 108 15 : this.cgi = cgi; 109 15 : this.css = css; 110 15 : this.js = js; 111 15 : this.logoPng = logo; 112 15 : } 113 : 114 0 : private GitwebCgiConfig() { 115 0 : this.cgi = null; 116 0 : this.css = null; 117 0 : this.js = null; 118 0 : this.logoPng = null; 119 0 : } 120 : 121 : /** Returns local path to the CGI executable; null if we shouldn't execute. */ 122 : public Path getGitwebCgi() { 123 99 : return cgi; 124 : } 125 : 126 : /** Returns local path of the {@code gitweb.css} matching the CGI. */ 127 : public Path getGitwebCss() { 128 0 : return css; 129 : } 130 : 131 : /** Returns local path of the {@code gitweb.js} for the CGI. */ 132 : public Path getGitwebJs() { 133 0 : return js; 134 : } 135 : 136 : /** Returns local path of the {@code git-logo.png} for the CGI. */ 137 : public Path getGitLogoPng() { 138 0 : return logoPng; 139 : } 140 : }