View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.utils;
21  
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  
26  /**
27   * Class which provides OS related utilities.
28   */
29  public final class OsSpecificUtil {
30  
31      /**
32       * Hiding public and default constructor.
33       */
34      private OsSpecificUtil() {
35  
36      }
37  
38      /**
39       * Updates the specified directory by resolving symbolic links, ensuring it exists,
40       * and creating any necessary parent directories. If the provided path is a symbolic
41       * link, it resolves it to the actual directory, throwing an IOException if the
42       * resolved path is not a directory. Creates directories if they do not exist.
43       *
44       * @param directory The path to the directory to be updated.
45       * @throws IOException If an I/O error occurs or if the resolved symbolic link is
46       *                     not a directory.
47       */
48      public static void updateDirectory(Path directory) throws IOException {
49          Path targetDirectory = directory;
50  
51          if (Files.isSymbolicLink(directory)) {
52              final Path actualDir = directory.toRealPath();
53  
54              if (Files.isDirectory(actualDir)) {
55                  targetDirectory = actualDir;
56              }
57              else {
58                  throw new IOException("Resolved symbolic link "
59                          + directory + " is not a directory.");
60              }
61          }
62          Files.createDirectories(targetDirectory);
63      }
64  }