Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java
41175 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.File;28import java.io.IOException;29import java.net.URI;30import java.nio.file.Files;31import java.nio.file.StandardCopyOption;32import java.util.HashSet;33import java.util.Map;34import java.util.Set;3536import com.sun.tools.sjavac.comp.CompilationService;37import com.sun.tools.sjavac.options.Options;38import com.sun.tools.sjavac.pubapi.PubApi;3940/**41* The copy file transform simply copies a matching file from -src to -d .42* Such files are typically images, xml documents and other data files.43*44* <p><b>This is NOT part of any supported API.45* If you write code that depends on this, you do so at your own risk.46* This code and its internal interfaces are subject to change or47* deletion without notice.</b>48*/49public class CopyFile implements Transformer {5051public void setExtra(String e) {52}5354public void setExtra(Options a) {55}5657public boolean transform(CompilationService compilationService,58Map<String,Set<URI>> pkgSrcs,59Set<URI> visibleSrcs,60Map<String,Set<String>> oldPackageDependents,61URI destRoot,62Map<String,Set<URI>> packageArtifacts,63Map<String,Map<String, Set<String>>> packageDependencies,64Map<String,Map<String, Set<String>>> packageCpDependencies,65Map<String, PubApi> packagePubapis,66Map<String, PubApi> dependencyPubapis,67int debugLevel,68boolean incremental,69int numCores)70{71boolean rc = true;72String dest_filename;73File dest;7475for (String pkgName : pkgSrcs.keySet()) {76String pkgNameF = Util.toFileSystemPath(pkgName);77for (URI u : pkgSrcs.get(pkgName)) {78File src = new File(u);79File destDir;80destDir = new File(destRoot.getPath()+File.separator+pkgNameF);81dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();82dest = new File(dest_filename);8384if (!destDir.isDirectory()) {85if (!destDir.mkdirs()) {86Log.error("Error: The copier could not create the directory "+87destDir.getPath());88return false;89}90}9192Set<URI> as = packageArtifacts.get(pkgName);93if (as == null) {94as = new HashSet<>();95packageArtifacts.put(pkgName, as);96}97as.add(dest.toURI());9899if (dest.exists() && dest.lastModified() > src.lastModified()) {100// A copied file exists, and its timestamp is newer than the source.101continue;102}103104Log.info("Copying "+pkgNameF+File.separator+src.getName());105106try {107Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);108}109catch(IOException e){110Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());111rc = false;112}113}114}115return rc;116}117}118119120