From 2c5fc2012a8931fc12329b3be47de4e6bb3b3d7b Mon Sep 17 00:00:00 2001 From: repolevedavaj Date: Thu, 22 Jun 2023 19:34:54 +0200 Subject: [PATCH] Do not store reference to directory to be cached --- .../plugins/jobcacher/ArbitraryFileCache.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java b/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java index 09dfb3b..0a5524e 100644 --- a/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java +++ b/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java @@ -170,12 +170,13 @@ public String getTitle() { @Override public Saver cache(ObjectPath cachesRoot, ObjectPath fallbackCachesRoot, Run build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException { - FilePath resolvedPath = resolvePath(workspace, initialEnvironment); + String expandedPath = initialEnvironment.expand(path); + FilePath resolvedPath = workspace.child(expandedPath); ExistingCache existingCache = resolveExistingValidCache(cachesRoot, fallbackCachesRoot, workspace, listener); if (existingCache == null) { logMessage("Skip restoring cache as no up-to-date cache exists", listener); - return new SaverImpl(resolvedPath); + return new SaverImpl(expandedPath); } logMessage("Restoring cache...", listener); @@ -191,13 +192,7 @@ public Saver cache(ObjectPath cachesRoot, ObjectPath fallbackCachesRoot, Run includes = new ArrayList(); - List excludes = new ArrayList(); + List includes = new ArrayList<>(); + List excludes = new ArrayList<>(); for (String decidingFilePattern : cacheValidityDecidingFile.split(",")) { if (decidingFilePattern.startsWith("!")) { @@ -343,22 +338,23 @@ private class SaverImpl extends Saver { private static final long serialVersionUID = 1L; - private final FilePath resolvedPath; + private final String expandedPath; - public SaverImpl(FilePath resolvedPath) { - this.resolvedPath = resolvedPath; + public SaverImpl(String expandedPath) { + this.expandedPath = expandedPath; } @Override public long calculateSize(ObjectPath objectPath, Run build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException { - return resolvedPath.act(new DirectorySize(includes, excludes)); + return workspace.child(expandedPath).act(new DirectorySize(includes, excludes)); } @Override public void save(ObjectPath cachesRoot, Run build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException { + FilePath resolvedPath = workspace.child(expandedPath); if (!resolvedPath.exists()) { logMessage("Cannot create cache as the path does not exist", listener); - if (isPathOutsideWorkspace(workspace) && isMaybeInsideDockerContainer(workspace)) { + if (isPathOutsideWorkspace(resolvedPath, workspace) && isMaybeInsideDockerContainer(workspace)) { logMessage("Note that paths outside the workspace while using the Docker Pipeline plugin are not supported", listener); } return; @@ -395,7 +391,7 @@ public void save(ObjectPath cachesRoot, Run build, FilePath workspace, Lau } } - private boolean isPathOutsideWorkspace(FilePath workspace) { + private boolean isPathOutsideWorkspace(FilePath resolvedPath, FilePath workspace) { return !StringUtils.startsWith(resolvedPath.getRemote(), workspace.getRemote()); }