src/jdk.jfr/share/classes/jdk/jfr/internal/RepositoryChunk.java
changeset 58863 c16ac7a2eba4
parent 50113 caf115bb98ad
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    61         ZonedDateTime z = ZonedDateTime.now();
    61         ZonedDateTime z = ZonedDateTime.now();
    62         String fileName = Repository.REPO_DATE_FORMAT.format(
    62         String fileName = Repository.REPO_DATE_FORMAT.format(
    63                 LocalDateTime.ofInstant(startTime, z.getZone()));
    63                 LocalDateTime.ofInstant(startTime, z.getZone()));
    64         this.startTime = startTime;
    64         this.startTime = startTime;
    65         this.repositoryPath = path;
    65         this.repositoryPath = path;
    66         this.unFinishedFile = findFileName(repositoryPath, fileName, ".part");
    66         this.unFinishedFile = findFileName(repositoryPath, fileName, ".jfr");
    67         this.file = findFileName(repositoryPath, fileName, ".jfr");
    67         this.file = findFileName(repositoryPath, fileName, ".jfr");
    68         this.unFinishedRAF = SecuritySupport.createRandomAccessFile(unFinishedFile);
    68         this.unFinishedRAF = SecuritySupport.createRandomAccessFile(unFinishedFile);
    69         SecuritySupport.touch(file);
    69  //       SecuritySupport.touch(file);
    70     }
    70     }
    71 
    71 
    72     private static SafePath findFileName(SafePath directory, String name, String extension) throws Exception {
    72     private static SafePath findFileName(SafePath directory, String name, String extension) throws Exception {
    73         Path p = directory.toPath().resolve(name + extension);
    73         Path p = directory.toPath().resolve(name + extension);
    74         for (int i = 1; i < MAX_CHUNK_NAMES; i++) {
    74         for (int i = 1; i < MAX_CHUNK_NAMES; i++) {
   103     }
   103     }
   104 
   104 
   105     private static long finish(SafePath unFinishedFile, SafePath file) throws IOException {
   105     private static long finish(SafePath unFinishedFile, SafePath file) throws IOException {
   106         Objects.requireNonNull(unFinishedFile);
   106         Objects.requireNonNull(unFinishedFile);
   107         Objects.requireNonNull(file);
   107         Objects.requireNonNull(file);
   108         SecuritySupport.delete(file);
       
   109         SecuritySupport.moveReplace(unFinishedFile, file);
       
   110         return SecuritySupport.getFileSize(file);
   108         return SecuritySupport.getFileSize(file);
   111     }
   109     }
   112 
   110 
   113     public Instant getStartTime() {
   111     public Instant getStartTime() {
   114         return startTime;
   112         return startTime;
   121     private void delete(SafePath f) {
   119     private void delete(SafePath f) {
   122         try {
   120         try {
   123             SecuritySupport.delete(f);
   121             SecuritySupport.delete(f);
   124             Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Repository chunk " + f + " deleted");
   122             Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Repository chunk " + f + " deleted");
   125         } catch (IOException e) {
   123         } catch (IOException e) {
   126             Logger.log(LogTag.JFR, LogLevel.ERROR, ()  -> "Repository chunk " + f + " could not be deleted: " + e.getMessage());
   124             // Probably happens because file is being streamed
       
   125             // on Windows where files in use can't be removed.
       
   126             Logger.log(LogTag.JFR, LogLevel.DEBUG, ()  -> "Repository chunk " + f + " could not be deleted: " + e.getMessage());
   127             if (f != null) {
   127             if (f != null) {
   128                 SecuritySupport.deleteOnExit(f);
   128                 FilePurger.add(f);
   129             }
   129             }
   130         }
   130         }
   131     }
   131     }
   132 
   132 
   133     private void destroy() {
   133     private void destroy() {