LCOV - code coverage report
Current view: top level - server/config - SitePaths.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 50 50 100.0 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 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 com.google.common.collect.Iterables;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.inject.Inject;
      20             : import com.google.inject.Singleton;
      21             : import java.io.IOException;
      22             : import java.nio.file.DirectoryStream;
      23             : import java.nio.file.Files;
      24             : import java.nio.file.NoSuchFileException;
      25             : import java.nio.file.Path;
      26             : 
      27             : /** Important paths within a {@link SitePath}. */
      28             : @Singleton
      29             : public final class SitePaths {
      30             :   public static final String CSS_FILENAME = "GerritSite.css";
      31             :   public static final String HEADER_FILENAME = "GerritSiteHeader.html";
      32             :   public static final String FOOTER_FILENAME = "GerritSiteFooter.html";
      33             :   public static final String THEME_FILENAME = "gerrit-theme.html";
      34             :   public static final String THEME_JS_FILENAME = "gerrit-theme.js";
      35             : 
      36             :   public final Path site_path;
      37             :   public final Path bin_dir;
      38             :   public final Path etc_dir;
      39             :   public final Path lib_dir;
      40             :   public final Path tmp_dir;
      41             :   public final Path logs_dir;
      42             :   public final Path plugins_dir;
      43             :   public final Path db_dir;
      44             :   public final Path data_dir;
      45             :   public final Path mail_dir;
      46             :   public final Path hooks_dir;
      47             :   public final Path static_dir;
      48             :   public final Path index_dir;
      49             : 
      50             :   public final Path gerrit_sh;
      51             :   public final Path gerrit_service;
      52             :   public final Path gerrit_socket;
      53             :   public final Path gerrit_war;
      54             : 
      55             :   public final Path gerrit_config;
      56             :   public final Path secure_config;
      57             :   public final Path notedb_config;
      58             : 
      59             :   public final Path jgit_config;
      60             : 
      61             :   public final Path ssl_keystore;
      62             :   public final Path ssh_key;
      63             :   public final Path ssh_rsa;
      64             :   public final Path ssh_ecdsa_256;
      65             :   public final Path ssh_ecdsa_384;
      66             :   public final Path ssh_ecdsa_521;
      67             :   public final Path ssh_ed25519;
      68             :   public final Path peer_keys;
      69             : 
      70             :   public final Path site_css;
      71             :   public final Path site_header;
      72             :   public final Path site_footer;
      73             :   public final Path site_theme; // For PolyGerrit UI only.
      74             :   public final Path site_theme_js; // For PolyGerrit UI only.
      75             :   public final Path site_gitweb;
      76             : 
      77             :   /** {@code true} if {@link #site_path} has not been initialized. */
      78             :   public final boolean isNew;
      79             : 
      80             :   @Inject
      81         153 :   public SitePaths(@SitePath Path sitePath) throws IOException {
      82         153 :     site_path = sitePath;
      83         153 :     Path p = sitePath;
      84             : 
      85         153 :     bin_dir = p.resolve("bin");
      86         153 :     etc_dir = p.resolve("etc");
      87         153 :     lib_dir = p.resolve("lib");
      88         153 :     tmp_dir = p.resolve("tmp");
      89         153 :     plugins_dir = p.resolve("plugins");
      90         153 :     db_dir = p.resolve("db");
      91         153 :     data_dir = p.resolve("data");
      92         153 :     logs_dir = p.resolve("logs");
      93         153 :     mail_dir = etc_dir.resolve("mail");
      94         153 :     hooks_dir = p.resolve("hooks");
      95         153 :     static_dir = p.resolve("static");
      96         153 :     index_dir = p.resolve("index");
      97             : 
      98         153 :     gerrit_sh = bin_dir.resolve("gerrit.sh");
      99         153 :     gerrit_service = bin_dir.resolve("gerrit.service");
     100         153 :     gerrit_socket = bin_dir.resolve("gerrit.socket");
     101         153 :     gerrit_war = bin_dir.resolve("gerrit.war");
     102             : 
     103         153 :     gerrit_config = etc_dir.resolve("gerrit.config");
     104         153 :     secure_config = etc_dir.resolve("secure.config");
     105         153 :     notedb_config = etc_dir.resolve("notedb.config");
     106             : 
     107         153 :     jgit_config = etc_dir.resolve("jgit.config");
     108             : 
     109         153 :     ssl_keystore = etc_dir.resolve("keystore");
     110         153 :     ssh_key = etc_dir.resolve("ssh_host_key");
     111         153 :     ssh_rsa = etc_dir.resolve("ssh_host_rsa_key");
     112         153 :     ssh_ecdsa_256 = etc_dir.resolve("ssh_host_ecdsa_key");
     113         153 :     ssh_ecdsa_384 = etc_dir.resolve("ssh_host_ecdsa_384_key");
     114         153 :     ssh_ecdsa_521 = etc_dir.resolve("ssh_host_ecdsa_521_key");
     115         153 :     ssh_ed25519 = etc_dir.resolve("ssh_host_ed25519_key");
     116         153 :     peer_keys = etc_dir.resolve("peer_keys");
     117             : 
     118         153 :     site_css = etc_dir.resolve(CSS_FILENAME);
     119         153 :     site_header = etc_dir.resolve(HEADER_FILENAME);
     120         153 :     site_footer = etc_dir.resolve(FOOTER_FILENAME);
     121         153 :     site_gitweb = etc_dir.resolve("gitweb_config.perl");
     122             : 
     123             :     // For PolyGerrit UI.
     124         153 :     site_theme = static_dir.resolve(THEME_FILENAME);
     125         153 :     site_theme_js = static_dir.resolve(THEME_JS_FILENAME);
     126             : 
     127             :     boolean isNew;
     128         153 :     try (DirectoryStream<Path> files = Files.newDirectoryStream(site_path)) {
     129         153 :       isNew = Iterables.isEmpty(files);
     130           1 :     } catch (NoSuchFileException e) {
     131           1 :       isNew = true;
     132         153 :     }
     133         153 :     this.isNew = isNew;
     134         153 :   }
     135             : 
     136             :   /**
     137             :    * Resolve an absolute or relative path.
     138             :    *
     139             :    * <p>Relative paths are resolved relative to the {@link #site_path}.
     140             :    *
     141             :    * @param path the path string to resolve. May be null.
     142             :    * @return the resolved path; null if {@code path} was null or empty.
     143             :    */
     144             :   @Nullable
     145             :   public Path resolve(String path) {
     146         147 :     if (path != null && !path.isEmpty()) {
     147          17 :       Path loc = site_path.resolve(path).normalize();
     148             :       try {
     149          17 :         return loc.toRealPath();
     150          17 :       } catch (IOException e) {
     151          17 :         return loc.toAbsolutePath();
     152             :       }
     153             :     }
     154         140 :     return null;
     155             :   }
     156             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750