src/share/classes/org/openjdk/jigsaw/ModuleFile.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.openjdk.jigsaw;
  27 
  28 import java.io.*;

  29 import java.security.*;
  30 import java.util.*;
  31 import java.util.jar.*;
  32 import java.util.zip.*;
  33 
  34 import static org.openjdk.jigsaw.FileConstants.ModuleFile.*;
  35 
  36 public final class ModuleFile {
  37     /**
  38      * Return the subdir of a section in an extracted module file.
  39      */
  40     public static String getSubdirOfSection(SectionType type) {
  41         switch (type) {
  42         case MODULE_INFO:
  43         case SIGNATURE:
  44             return ".";
  45         case CLASSES:
  46         case RESOURCES:
  47             return "classes";
  48         case NATIVE_LIBS:


 492         }
 493 
 494         // Track files installed outside the module library. For later removal.
 495         // files are relative to the modules directory.
 496         private PrintWriter filesWriter;
 497 
 498         private void trackFiles(SectionType type, File file)
 499             throws IOException
 500         {
 501             if (file == null || file.toPath().startsWith(destination.toPath()))
 502                 return;
 503 
 504             // Lazy construction, not all modules will need this.
 505             if (filesWriter == null)
 506                 filesWriter = new PrintWriter(computeRealPath("files"), "UTF-8");
 507 
 508             filesWriter.println(Files.convertSeparator(relativize(destination, file)));
 509             filesWriter.flush();
 510         }
 511 
 512         void remove() throws IOException {
 513             ModuleFile.Reader.remove(destination);
 514         }
 515 
 516         // Removes a module, given its module install directory
 517         static void remove(File moduleDir) throws IOException {

 518             // Firstly remove any files installed outside of the module dir
 519             File files = new File(moduleDir, "files");
 520             if (files.exists()) {
 521                 try (FileInputStream fis = new FileInputStream(files);
 522                      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
 523                      BufferedReader in = new BufferedReader(isr)) {
 524                     String filename;
 525                     while ((filename = in.readLine()) != null)

 526                         Files.delete(new File(moduleDir,
 527                                               Files.platformSeparator(filename)));


 528                 }
 529             }




 530 
 531             Files.deleteTree(moduleDir);

 532         }
 533 
 534         // Returns the absolute path of the given section type.
 535         private File getDirOfSection(SectionType type) {
 536             if (type == SectionType.NATIVE_LIBS)
 537                 return natlibs; 
 538             else if (type == SectionType.NATIVE_CMDS)
 539                 return natcmds;
 540             else if (type == SectionType.CONFIG)
 541                 return configs;
 542 
 543             // resolve sub dir section paths against the modules directory
 544             return new File(destination, ModuleFile.getSubdirOfSection(type));
 545         }
 546 
 547         private File computeRealPath(String path) throws IOException {
 548             return resolveAndNormalize(destination, path);
 549         }
 550 
 551         private File computeRealPath(SectionType type, String storedpath)




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.openjdk.jigsaw;
  27 
  28 import java.io.*;
  29 import java.nio.charset.Charset;
  30 import java.security.*;
  31 import java.util.*;
  32 import java.util.jar.*;
  33 import java.util.zip.*;
  34 
  35 import static org.openjdk.jigsaw.FileConstants.ModuleFile.*;
  36 
  37 public final class ModuleFile {
  38     /**
  39      * Return the subdir of a section in an extracted module file.
  40      */
  41     public static String getSubdirOfSection(SectionType type) {
  42         switch (type) {
  43         case MODULE_INFO:
  44         case SIGNATURE:
  45             return ".";
  46         case CLASSES:
  47         case RESOURCES:
  48             return "classes";
  49         case NATIVE_LIBS:


 493         }
 494 
 495         // Track files installed outside the module library. For later removal.
 496         // files are relative to the modules directory.
 497         private PrintWriter filesWriter;
 498 
 499         private void trackFiles(SectionType type, File file)
 500             throws IOException
 501         {
 502             if (file == null || file.toPath().startsWith(destination.toPath()))
 503                 return;
 504 
 505             // Lazy construction, not all modules will need this.
 506             if (filesWriter == null)
 507                 filesWriter = new PrintWriter(computeRealPath("files"), "UTF-8");
 508 
 509             filesWriter.println(Files.convertSeparator(relativize(destination, file)));
 510             filesWriter.flush();
 511         }
 512 
 513         List<IOException> remove() {
 514             return ModuleFile.Reader.remove(destination);
 515         }
 516 
 517         // Removes a module, given its module install directory
 518         static List<IOException> remove(File moduleDir) {
 519             List<IOException> excs = new ArrayList<>();
 520             // Firstly remove any files installed outside of the module dir
 521             File files = new File(moduleDir, "files");
 522             if (files.exists()) {
 523                 try {
 524                     List<String> filenames =
 525                           java.nio.file.Files.readAllLines(files.toPath(),
 526                                                            Charset.forName("UTF-8"));
 527                     for (String fn : filenames) {
 528                         try {
 529                             Files.delete(new File(moduleDir,
 530                                                   Files.platformSeparator(fn)));
 531                         } catch (IOException x) {
 532                             excs.add(x);
 533                         }
 534                     }
 535                 } catch (IOException x) {
 536                     excs.add(x);
 537                 }
 538             }
 539 
 540             excs.addAll(Files.deleteTreeUnchecked(moduleDir.toPath()));
 541             return excs;
 542         }
 543 
 544         // Returns the absolute path of the given section type.
 545         private File getDirOfSection(SectionType type) {
 546             if (type == SectionType.NATIVE_LIBS)
 547                 return natlibs;
 548             else if (type == SectionType.NATIVE_CMDS)
 549                 return natcmds;
 550             else if (type == SectionType.CONFIG)
 551                 return configs;
 552 
 553             // resolve sub dir section paths against the modules directory
 554             return new File(destination, ModuleFile.getSubdirOfSection(type));
 555         }
 556 
 557         private File computeRealPath(String path) throws IOException {
 558             return resolveAndNormalize(destination, path);
 559         }
 560 
 561         private File computeRealPath(SectionType type, String storedpath)