LCOV - code coverage report
Current view: top level - common - FileUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 31 83.9 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2014 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.common;
      16             : 
      17             : import java.io.File;
      18             : import java.io.FileNotFoundException;
      19             : import java.io.IOException;
      20             : import java.nio.file.Files;
      21             : import java.nio.file.Path;
      22             : import java.util.Arrays;
      23             : import org.eclipse.jgit.lib.Constants;
      24             : import org.eclipse.jgit.storage.file.FileBasedConfig;
      25             : import org.eclipse.jgit.util.IO;
      26             : 
      27             : public class FileUtil {
      28             :   public static boolean modified(FileBasedConfig cfg) throws IOException {
      29             :     byte[] curVers;
      30             :     try {
      31          15 :       curVers = IO.readFully(cfg.getFile());
      32          15 :     } catch (FileNotFoundException notFound) {
      33          15 :       return true;
      34          15 :     }
      35             : 
      36          15 :     byte[] newVers = Constants.encode(cfg.toText());
      37          15 :     return !Arrays.equals(curVers, newVers);
      38             :   }
      39             : 
      40             :   public static void mkdir(File path) {
      41           0 :     if (!path.isDirectory() && !path.mkdir()) {
      42           0 :       throw new Die("Cannot make directory " + path);
      43             :     }
      44           0 :   }
      45             : 
      46             :   public static void chmod(int mode, Path path) {
      47          15 :     chmod(mode, path.toFile());
      48          15 :   }
      49             : 
      50             :   public static void chmod(int mode, File path) {
      51          15 :     path.setReadable(false, false /* all */);
      52          15 :     path.setWritable(false, false /* all */);
      53          15 :     path.setExecutable(false, false /* all */);
      54             : 
      55          15 :     path.setReadable((mode & 0400) == 0400, true /* owner only */);
      56          15 :     path.setWritable((mode & 0200) == 0200, true /* owner only */);
      57          15 :     if (path.isDirectory() || (mode & 0100) == 0100) {
      58          15 :       path.setExecutable(true, true /* owner only */);
      59             :     }
      60             : 
      61          15 :     if ((mode & 0044) == 0044) {
      62          15 :       path.setReadable(true, false /* all */);
      63             :     }
      64          15 :     if ((mode & 0011) == 0011) {
      65          15 :       path.setExecutable(true, false /* all */);
      66             :     }
      67          15 :   }
      68             : 
      69             :   /**
      70             :    * Get the last modified time of a path.
      71             :    *
      72             :    * <p>Equivalent to {@code File#lastModified()}, returning 0 on errors, including file not found.
      73             :    * Callers that prefer exceptions can use {@link Files#getLastModifiedTime(Path,
      74             :    * java.nio.file.LinkOption...)}.
      75             :    *
      76             :    * @param p path.
      77             :    * @return last modified time, in milliseconds since epoch.
      78             :    */
      79             :   public static long lastModified(Path p) {
      80             :     try {
      81           1 :       return Files.getLastModifiedTime(p).toMillis();
      82         103 :     } catch (IOException e) {
      83         103 :       return 0;
      84             :     }
      85             :   }
      86             : 
      87             :   public static Path mkdirsOrDie(Path p, String errMsg) {
      88             :     try {
      89         138 :       if (!Files.isDirectory(p)) {
      90         138 :         Files.createDirectories(p);
      91             :       }
      92         138 :       return p;
      93           0 :     } catch (IOException e) {
      94           0 :       throw new Die(errMsg + ": " + p, e);
      95             :     }
      96             :   }
      97             : 
      98             :   private FileUtil() {}
      99             : }

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