1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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 jdk.jfr.internal;
  27 
  28 import java.io.IOException;
  29 import java.io.RandomAccessFile;
  30 import java.nio.channels.ReadableByteChannel;
  31 import java.nio.file.Path;
  32 import java.time.Instant;
  33 import java.time.LocalDateTime;
  34 import java.time.ZonedDateTime;
  35 import java.util.Comparator;
  36 import java.util.Objects;
  37 
  38 import jdk.jfr.internal.SecuritySupport.SafePath;
  39 
  40 final class RepositoryChunk {
  41     private static final int MAX_CHUNK_NAMES = 100;
  42 
  43     static final Comparator<RepositoryChunk> END_TIME_COMPARATOR = new Comparator<RepositoryChunk>() {
  44         @Override
  45         public int compare(RepositoryChunk c1, RepositoryChunk c2) {
  46             return c1.endTime.compareTo(c2.endTime);
  47         }
  48     };
  49 
  50     private final SafePath repositoryPath;
  51     private final SafePath unFinishedFile;
  52     private final SafePath file;
  53     private final Instant startTime;
  54     private final RandomAccessFile unFinishedRAF;
  55 
  56     private Instant endTime = null; // unfinished
  57     private int refCount = 0;
  58     private long size;
  59 
  60     RepositoryChunk(SafePath path, Instant startTime) throws Exception {
  61         ZonedDateTime z = ZonedDateTime.now();
  62         String fileName = Repository.REPO_DATE_FORMAT.format(
  63                 LocalDateTime.ofInstant(startTime, z.getZone()));
  64         this.startTime = startTime;
  65         this.repositoryPath = path;
  66         this.unFinishedFile = findFileName(repositoryPath, fileName, ".part");
  67         this.file = findFileName(repositoryPath, fileName, ".jfr");
  68         this.unFinishedRAF = SecuritySupport.createRandomAccessFile(unFinishedFile);
  69         SecuritySupport.touch(file);
  70     }
  71 
  72     private static SafePath findFileName(SafePath directory, String name, String extension) throws Exception {
  73         Path p = directory.toPath().resolve(name + extension);
  74         for (int i = 1; i < MAX_CHUNK_NAMES; i++) {
  75             SafePath s = new SafePath(p);
  76             if (!SecuritySupport.exists(s)) {
  77                 return s;
  78             }
  79             String extendedName = String.format("%s_%02d%s", name, i, extension);
  80             p = directory.toPath().resolve(extendedName);
  81         }
  82         p = directory.toPath().resolve(name + "_" + System.currentTimeMillis() + extension);
  83         return SecuritySupport.toRealPath(new SafePath(p));
  84     }
  85 
  86     public SafePath getUnfishedFile() {
  87         return unFinishedFile;
  88     }
  89 
  90     void finish(Instant endTime) {
  91         try {
  92             finishWithException(endTime);
  93         } catch (IOException e) {
  94             Logger.log(LogTag.JFR, LogLevel.ERROR, "Could not finish chunk. " + e.getMessage());
  95         }
  96     }
  97 
  98     private void finishWithException(Instant endTime) throws IOException {
  99         unFinishedRAF.close();
 100         this.size = finish(unFinishedFile, file);
 101         this.endTime = endTime;
 102         Logger.log(LogTag.JFR_SYSTEM, LogLevel.DEBUG, () -> "Chunk finished: " + file);
 103     }
 104 
 105     private static long finish(SafePath unFinishedFile, SafePath file) throws IOException {
 106         Objects.requireNonNull(unFinishedFile);
 107         Objects.requireNonNull(file);
 108         SecuritySupport.delete(file);
 109         SecuritySupport.moveReplace(unFinishedFile, file);
 110         return SecuritySupport.getFileSize(file);
 111     }
 112 
 113     public Instant getStartTime() {
 114         return startTime;
 115     }
 116 
 117     public Instant getEndTime() {
 118         return endTime;
 119     }
 120 
 121     private void delete(SafePath f) {
 122         try {
 123             SecuritySupport.delete(f);
 124             Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Repository chunk " + f + " deleted");
 125         } catch (IOException e) {
 126             Logger.log(LogTag.JFR, LogLevel.ERROR, ()  -> "Repository chunk " + f + " could not be deleted: " + e.getMessage());
 127             if (f != null) {
 128                 SecuritySupport.deleteOnExit(f);
 129             }
 130         }
 131     }
 132 
 133     private void destroy() {
 134         if (!isFinished()) {
 135             finish(Instant.MIN);
 136         }
 137         if (file != null) {
 138             delete(file);
 139         }
 140         try {
 141             unFinishedRAF.close();
 142         } catch (IOException e) {
 143             Logger.log(LogTag.JFR, LogLevel.ERROR, () -> "Could not close random access file: " + unFinishedFile.toString() + ". File will not be deleted due to: " + e.getMessage());
 144         }
 145     }
 146 
 147     public synchronized void use() {
 148         ++refCount;
 149         Logger.log(LogTag.JFR_SYSTEM, LogLevel.DEBUG, () -> "Use chunk " + toString() + " ref count now " + refCount);
 150     }
 151 
 152     public synchronized void release() {
 153         --refCount;
 154         Logger.log(LogTag.JFR_SYSTEM, LogLevel.DEBUG, () -> "Release chunk " + toString() + " ref count now " + refCount);
 155         if (refCount == 0) {
 156             destroy();
 157         }
 158     }
 159 
 160     @Override
 161     @SuppressWarnings("deprecation")
 162     protected void finalize() {
 163         boolean destroy = false;
 164         synchronized (this) {
 165             if (refCount > 0) {
 166                 destroy = true;
 167             }
 168         }
 169         if (destroy) {
 170             destroy();
 171         }
 172     }
 173 
 174     public long getSize() {
 175         return size;
 176     }
 177 
 178     public boolean isFinished() {
 179         return endTime != null;
 180     }
 181 
 182     @Override
 183     public String toString() {
 184         if (isFinished()) {
 185             return file.toString();
 186         }
 187         return unFinishedFile.toString();
 188     }
 189 
 190     ReadableByteChannel newChannel() throws IOException {
 191         if (!isFinished()) {
 192             throw new IOException("Chunk not finished");
 193         }
 194         return ((SecuritySupport.newFileChannelToRead(file)));
 195     }
 196 
 197     public boolean inInterval(Instant startTime, Instant endTime) {
 198         if (startTime != null && getEndTime().isBefore(startTime)) {
 199             return false;
 200         }
 201         if (endTime != null && getStartTime().isAfter(endTime)) {
 202             return false;
 203         }
 204         return true;
 205     }
 206 
 207     public SafePath getFile() {
 208         return file;
 209     }
 210 }