|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 Red Hat, Inc. and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *******************************************************************************/ |
| 10 | +package org.eclipse.jdt.ls.core.internal.managers; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.attribute.BasicFileAttributeView; |
| 17 | +import java.nio.file.attribute.BasicFileAttributes; |
| 18 | +import java.time.Instant; |
| 19 | + |
| 20 | +import org.eclipse.core.internal.preferences.EclipsePreferences; |
| 21 | +import org.eclipse.core.resources.IFile; |
| 22 | +import org.eclipse.core.resources.IFolder; |
| 23 | +import org.eclipse.core.resources.IProject; |
| 24 | +import org.eclipse.core.resources.IProjectDescription; |
| 25 | +import org.eclipse.core.resources.IResource; |
| 26 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 27 | +import org.eclipse.core.runtime.CoreException; |
| 28 | +import org.eclipse.core.runtime.IPath; |
| 29 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 30 | +import org.eclipse.core.runtime.Status; |
| 31 | +import org.eclipse.jdt.core.IJavaProject; |
| 32 | + |
| 33 | +class LinkResourceUtil { |
| 34 | + |
| 35 | + private static final IPath METADATA_FOLDER_PATH = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".projects"); |
| 36 | + |
| 37 | + private static boolean isNewer(Path file, Instant instant) throws CoreException { |
| 38 | + try { |
| 39 | + BasicFileAttributes attributes = Files.getFileAttributeView(file, BasicFileAttributeView.class).readAttributes(); |
| 40 | + return attributes.creationTime().toInstant().isAfter(instant); |
| 41 | + } catch (IOException ex) { |
| 42 | + throw new CoreException(Status.error(ex.getMessage(), ex)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Get the redirected path of the input path. The path will be redirected to |
| 48 | + * the workspace's metadata folder ({@link LinkResourceUtil#METADATA_FOLDER_PATH}). |
| 49 | + * @param projectName name of the project. |
| 50 | + * @param path path needs to be redirected. |
| 51 | + * @return the redirected path. |
| 52 | + */ |
| 53 | + private static IPath getMetaDataFilePath(String projectName, IPath path) { |
| 54 | + if (path.segmentCount() == 1) { |
| 55 | + return METADATA_FOLDER_PATH.append(projectName).append(path); |
| 56 | + } |
| 57 | + |
| 58 | + String lastSegment = path.lastSegment(); |
| 59 | + if (IProjectDescription.DESCRIPTION_FILE_NAME.equals(lastSegment)) { |
| 60 | + return METADATA_FOLDER_PATH.append(projectName).append(lastSegment); |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + private static void linkFolderIfNewer(IFolder settingsFolder, Instant instant) throws CoreException { |
| 67 | + if (settingsFolder.isLinked()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + if (settingsFolder.exists() && !isNewer(settingsFolder.getLocation().toPath(), instant)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + if (!settingsFolder.exists()) { |
| 74 | + // not existing yet, create link |
| 75 | + File diskFolder = getMetaDataFilePath(settingsFolder.getProject().getName(), settingsFolder.getProjectRelativePath()).toFile(); |
| 76 | + diskFolder.mkdirs(); |
| 77 | + settingsFolder.createLink(diskFolder.toURI(), IResource.NONE, new NullProgressMonitor()); |
| 78 | + } else if (isNewer(settingsFolder.getLocation().toPath(), instant)) { |
| 79 | + // already existing but not existing before import: move then link |
| 80 | + File sourceFolder = settingsFolder.getLocation().toFile(); |
| 81 | + File targetFolder = getMetaDataFilePath(settingsFolder.getProject().getName(), settingsFolder.getProjectRelativePath()).toFile(); |
| 82 | + File parentTargetFolder = targetFolder.getParentFile(); |
| 83 | + if (!parentTargetFolder.isDirectory()) { |
| 84 | + parentTargetFolder.mkdirs(); |
| 85 | + } |
| 86 | + sourceFolder.renameTo(targetFolder); |
| 87 | + settingsFolder.createLink(targetFolder.toURI(), IResource.REPLACE, new NullProgressMonitor()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static void linkFileIfNewer(IFile metadataFile, Instant instant, String ifEmpty) throws CoreException { |
| 92 | + if (metadataFile.isLinked()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + if (metadataFile.exists() && !isNewer(metadataFile.getLocation().toPath(), instant)) { |
| 96 | + return; |
| 97 | + } |
| 98 | + File targetFile = getMetaDataFilePath(metadataFile.getProject().getName(), metadataFile.getProjectRelativePath()).toFile(); |
| 99 | + if (!targetFile.exists()) { |
| 100 | + try { |
| 101 | + targetFile.getParentFile().mkdirs(); |
| 102 | + targetFile.createNewFile(); |
| 103 | + } catch (IOException ex) { |
| 104 | + throw new CoreException(Status.error(targetFile + " cannot be created", ex)); //$NON-NLS-1$ |
| 105 | + } |
| 106 | + } |
| 107 | + if (metadataFile.exists()) { |
| 108 | + metadataFile.getLocation().toFile().renameTo(targetFile); |
| 109 | + } else { |
| 110 | + try { |
| 111 | + Files.writeString(targetFile.toPath(), ifEmpty != null ? ifEmpty : ""); |
| 112 | + } catch (IOException ex) { |
| 113 | + throw new CoreException(Status.error(ex.getMessage(), ex)); |
| 114 | + } |
| 115 | + } |
| 116 | + metadataFile.createLink(targetFile.toURI(), IResource.REPLACE, new NullProgressMonitor()); |
| 117 | + } |
| 118 | + |
| 119 | + public static void linkMetadataResourcesIfNewer(IProject project, Instant instant) throws CoreException { |
| 120 | + linkFileIfNewer(project.getFile(IProjectDescription.DESCRIPTION_FILE_NAME), instant, null); |
| 121 | + linkFolderIfNewer(project.getFolder(EclipsePreferences.DEFAULT_PREFERENCES_DIRNAME), instant); |
| 122 | + linkFileIfNewer(project.getFile(IJavaProject.CLASSPATH_FILE_NAME), instant, null); |
| 123 | + linkFileIfNewer(project.getFile(".factorypath"), instant, "<factorypath/>"); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments