• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #9662

pending completion
#9662

push

travis_ci

web-flow
Fix error update alias in PB_Tree WrappedSegment

4 of 4 new or added lines in 1 file covered. (100.0%)

79101 of 165738 relevant lines covered (47.73%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.96
/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.iotdb.commons.utils;
20

21
import org.apache.iotdb.commons.file.SystemFileFactory;
22

23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

26
import java.io.BufferedInputStream;
27
import java.io.BufferedOutputStream;
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.FileOutputStream;
31
import java.io.IOException;
32
import java.nio.file.DirectoryNotEmptyException;
33
import java.nio.file.Files;
34
import java.nio.file.NoSuchFileException;
35
import java.util.Arrays;
36
import java.util.Objects;
37

38
public class FileUtils {
39
  private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
1✔
40

41
  private static final int BUFFER_SIZE = 1024;
42

43
  private FileUtils() {}
44

45
  public static boolean deleteFileIfExist(File file) {
46
    try {
47
      Files.deleteIfExists(file.toPath());
1✔
48
      return true;
1✔
49
    } catch (IOException e) {
×
50
      logger.error(e.getMessage(), e);
×
51
      return false;
×
52
    }
53
  }
54

55
  public static void deleteDirectory(File folder) {
56
    if (folder.isDirectory()) {
1✔
57
      for (File file : folder.listFiles()) {
1✔
58
        deleteDirectory(file);
1✔
59
      }
60
    }
61
    try {
62
      Files.delete(folder.toPath());
1✔
63
    } catch (NoSuchFileException | DirectoryNotEmptyException e) {
1✔
64
      logger.warn("{}: {}", e.getMessage(), Arrays.toString(folder.list()), e);
1✔
65
    } catch (Exception e) {
×
66
      logger.warn("{}: {}", e.getMessage(), folder.getName(), e);
×
67
    }
1✔
68
  }
1✔
69

70
  public static void deleteDirectoryAndEmptyParent(File folder) {
71
    deleteDirectory(folder);
1✔
72
    final File parentFolder = folder.getParentFile();
1✔
73
    if (parentFolder.isDirectory()
1✔
74
        && Objects.requireNonNull(parentFolder.listFiles()).length == 0) {
1✔
75
      if (!parentFolder.delete()) {
1✔
76
        logger.warn("Delete folder failed: {}", parentFolder.getAbsolutePath());
×
77
      }
78
    }
79
  }
1✔
80

81
  public static boolean copyDir(File sourceDir, File targetDir) throws IOException {
82
    if (!sourceDir.exists() || !sourceDir.isDirectory()) {
1✔
83
      logger.error(
×
84
          "Failed to copy folder, because source folder [{}] doesn't exist.",
85
          sourceDir.getAbsolutePath());
×
86
      return false;
×
87
    }
88
    if (!targetDir.exists()) {
1✔
89
      if (!targetDir.mkdirs()) {
1✔
90
        logger.error(
×
91
            "Failed to copy folder, because failed to create target folder[{}].",
92
            targetDir.getAbsolutePath());
×
93
        return false;
×
94
      }
95
    } else if (!targetDir.isDirectory()) {
×
96
      logger.error(
×
97
          "Failed to copy folder, because target folder [{}] already exist.",
98
          targetDir.getAbsolutePath());
×
99
      return false;
×
100
    }
101
    File[] files = sourceDir.listFiles();
1✔
102
    if (files == null || files.length == 0) {
1✔
103
      return true;
×
104
    }
105
    boolean result = true;
1✔
106
    for (File file : files) {
1✔
107
      if (!file.exists()) {
1✔
108
        continue;
×
109
      }
110
      File targetFile = new File(targetDir, file.getName());
1✔
111
      if (file.isDirectory()) {
1✔
112
        result &= copyDir(file.getAbsoluteFile(), targetFile);
×
113
      } else {
114
        // copy file
115
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
1✔
116
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile))) {
1✔
117
          byte[] bytes = new byte[BUFFER_SIZE];
1✔
118
          int size = 0;
1✔
119
          while ((size = in.read(bytes)) > 0) {
1✔
120
            out.write(bytes, 0, size);
1✔
121
          }
122
        }
123
      }
124
    }
125
    return result;
1✔
126
  }
127

128
  /**
129
   * Calculate the directory size including sub dir.
130
   *
131
   * @param path
132
   * @return
133
   */
134
  public static long getDirSize(String path) {
135
    long sum = 0;
×
136
    File file = SystemFileFactory.INSTANCE.getFile(path);
×
137
    if (file.isDirectory()) {
×
138
      String[] list = file.list();
×
139
      for (String item : list) {
×
140
        String subPath = path + File.separator + item;
×
141
        sum += getDirSize(subPath);
×
142
      }
143
    } else {
×
144
      // this is a file.
145
      sum += file.length();
×
146
    }
147
    return sum;
×
148
  }
149

150
  public static void recursiveDeleteFolder(String path) throws IOException {
151
    File file = new File(path);
1✔
152
    if (file.isDirectory()) {
1✔
153
      File[] files = file.listFiles();
1✔
154
      if (files == null || files.length == 0) {
1✔
155
        org.apache.commons.io.FileUtils.deleteDirectory(file);
1✔
156
      } else {
157
        for (File f : files) {
1✔
158
          recursiveDeleteFolder(f.getAbsolutePath());
1✔
159
        }
160
        org.apache.commons.io.FileUtils.deleteDirectory(file);
1✔
161
      }
162
    } else {
1✔
163
      org.apache.commons.io.FileUtils.delete(file);
1✔
164
    }
165
  }
1✔
166

167
  /** Add a prefix to a relative path file */
168
  public static String addPrefix2FilePath(String prefix, String file) {
169
    if (!new File(file).isAbsolute() && prefix != null && prefix.length() > 0) {
×
170
      if (!prefix.endsWith(File.separator)) {
×
171
        file = prefix + File.separatorChar + file;
×
172
      } else {
173
        file = prefix + file;
×
174
      }
175
    }
176
    return file;
×
177
  }
178
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc