001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.utils;
021
022import java.io.IOException;
023import java.nio.file.Files;
024import java.nio.file.Path;
025
026/**
027 * Class which provides OS related utilities.
028 */
029public final class OsSpecificUtil {
030
031    /**
032     * Hiding public and default constructor.
033     */
034    private OsSpecificUtil() {
035
036    }
037
038    /**
039     * Updates the specified directory by resolving symbolic links, ensuring it exists,
040     * and creating any necessary parent directories. If the provided path is a symbolic
041     * link, it resolves it to the actual directory, throwing an IOException if the
042     * resolved path is not a directory. Creates directories if they do not exist.
043     *
044     * @param directory The path to the directory to be updated.
045     * @throws IOException If an I/O error occurs or if the resolved symbolic link is
046     *                     not a directory.
047     */
048    public static void updateDirectory(Path directory) throws IOException {
049        Path targetDirectory = directory;
050
051        if (Files.isSymbolicLink(directory)) {
052            final Path actualDir = directory.toRealPath();
053
054            if (Files.isDirectory(actualDir)) {
055                targetDirectory = actualDir;
056            }
057            else {
058                throw new IOException("Resolved symbolic link "
059                        + directory + " is not a directory.");
060            }
061        }
062        Files.createDirectories(targetDirectory);
063    }
064}