< prev index next >

test/jdk/java/io/File/GetXSpace.java

Print this page




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4057701 6286712 6364377
  27  * @run build GetXSpace
  28  * @run shell GetXSpace.sh
  29  * @summary Basic functionality of File.get-X-Space methods.
  30  * @key randomness
  31  */
  32 
  33 import java.io.BufferedReader;
  34 import java.io.File;
  35 import java.io.FilePermission;
  36 import java.io.InputStreamReader;
  37 import java.io.IOException;


  38 import java.security.Permission;
  39 import java.util.ArrayList;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 
  43 import static java.lang.System.out;
  44 
  45 public class GetXSpace {
  46 
  47     private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
  48                                               new DenyRead() };
  49 
  50     private static final String osName = System.getProperty("os.name");



  51     // FileSystem Total Used Available Use% MountedOn
  52     private static final Pattern dfPattern = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
  53 
  54     private static int fail = 0;
  55     private static int pass = 0;
  56     private static Throwable first;
  57 
  58     static void pass() {
  59         pass++;
  60     }
  61 
  62     static void fail(String p) {
  63         setFirst(p);
  64         System.err.format("FAILED: %s%n", p);
  65         fail++;
  66     }
  67 
  68     static void fail(String p, long exp, String cmp, long got) {
  69         String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
  70         setFirst(s);
  71         System.err.format("FAILED: %s%n", s);
  72         fail++;


 112         }
 113     }
 114 
 115     private static ArrayList<Space> space(String f) throws IOException {
 116         ArrayList<Space> al = new ArrayList<>();
 117 
 118         String cmd = "df -k -P" + (f == null ? "" : " " + f);
 119         StringBuilder sb = new StringBuilder();
 120         Process p = Runtime.getRuntime().exec(cmd);
 121         try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
 122             String s;
 123             int i = 0;
 124             while ((s = in.readLine()) != null) {
 125                 // skip header
 126                 if (i++ == 0) continue;
 127                 sb.append(s).append("\n");
 128             }
 129         }
 130         out.println(sb);
 131 
 132         Matcher m = dfPattern.matcher(sb);
 133         int j = 0;
 134         while (j < sb.length()) {
 135             if (m.find(j)) {
 136                 // swap can change while this test is running
 137                 if (!m.group(1).equals("swap")) {
 138                     String name = f;
 139                     if (name == null) {
 140                         // cygwin's df lists windows path as FileSystem (1st group)
 141                         name = osName.startsWith("Windows") ? m.group(1) : m.group(4);
 142                     }
 143                     al.add(new Space(m.group(2), m.group(3), name));;
 144                 }
 145                 j = m.end() + 1;
 146             } else {
 147                 throw new RuntimeException("unrecognized df output format: "
 148                                            + "charAt(" + j + ") = '"
 149                                            + sb.charAt(j) + "'");
 150             }
 151         }
 152 
 153         if (al.size() == 0) {
 154             // df did not produce output
 155             String name = (f == null ? "" : f);
 156             al.add(new Space("0", "0", name));
 157         }
 158         return al;
 159     }
 160 
 161     private static void tryCatch(Space s) {


 184             } catch (SecurityException x) {
 185                 out.format(fmt, "getUsableSpace", x);
 186                 pass();
 187             }
 188         }
 189     }
 190 
 191     private static void compare(Space s) {
 192         File f = new File(s.name());
 193         long ts = f.getTotalSpace();
 194         long fs = f.getFreeSpace();
 195         long us = f.getUsableSpace();
 196 
 197         out.format("%s:%n", s.name());
 198         String fmt = "  %-4s total= %12d free = %12d usable = %12d%n";
 199         out.format(fmt, "df", s.total(), 0, s.free());
 200         out.format(fmt, "getX", ts, fs, us);
 201 
 202         // if the file system can dynamically change size, this check will fail
 203         if (ts != s.total()) {

















 204             fail(s.name(), s.total(), "!=", ts);

 205         } else {
 206             pass();
 207         }
 208 
 209         // unix df returns statvfs.f_bavail
 210         long tsp = (!osName.startsWith("Windows") ? us : fs);
 211         if (!s.woomFree(tsp)) {
 212             fail(s.name(), s.free(), "??", tsp);
 213         } else {
 214             pass();
 215         }
 216 
 217         if (fs > s.total()) {
 218             fail(s.name(), s.total(), ">", fs);
 219         } else {
 220             pass();
 221         }
 222 
 223         if (us > s.total()) {
 224             fail(s.name(), s.total(), ">", us);
 225         } else {
 226             pass();
 227         }
 228     }
 229 
 230     private static String FILE_PREFIX = "/getSpace.";




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4057701 6286712 6364377
  27  * @run build GetXSpace
  28  * @run shell GetXSpace.sh
  29  * @summary Basic functionality of File.get-X-Space methods.
  30  * @key randomness
  31  */
  32 
  33 import java.io.BufferedReader;
  34 import java.io.File;
  35 import java.io.FilePermission;
  36 import java.io.InputStreamReader;
  37 import java.io.IOException;
  38 import java.nio.file.Files;
  39 import java.nio.file.FileStore;
  40 import java.security.Permission;
  41 import java.util.ArrayList;
  42 import java.util.regex.Matcher;
  43 import java.util.regex.Pattern;
  44 
  45 import static java.lang.System.out;
  46 
  47 public class GetXSpace {
  48 
  49     private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
  50                                               new DenyRead() };
  51 
  52     private static final String OS_NAME = System.getProperty("os.name");
  53     private static final boolean IS_MAC = OS_NAME.startsWith("Mac");
  54     private static final boolean IS_WIN = OS_NAME.startsWith("Windows");
  55 
  56     // FileSystem Total Used Available Use% MountedOn
  57     private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
  58 
  59     private static int fail = 0;
  60     private static int pass = 0;
  61     private static Throwable first;
  62 
  63     static void pass() {
  64         pass++;
  65     }
  66 
  67     static void fail(String p) {
  68         setFirst(p);
  69         System.err.format("FAILED: %s%n", p);
  70         fail++;
  71     }
  72 
  73     static void fail(String p, long exp, String cmp, long got) {
  74         String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
  75         setFirst(s);
  76         System.err.format("FAILED: %s%n", s);
  77         fail++;


 117         }
 118     }
 119 
 120     private static ArrayList<Space> space(String f) throws IOException {
 121         ArrayList<Space> al = new ArrayList<>();
 122 
 123         String cmd = "df -k -P" + (f == null ? "" : " " + f);
 124         StringBuilder sb = new StringBuilder();
 125         Process p = Runtime.getRuntime().exec(cmd);
 126         try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
 127             String s;
 128             int i = 0;
 129             while ((s = in.readLine()) != null) {
 130                 // skip header
 131                 if (i++ == 0) continue;
 132                 sb.append(s).append("\n");
 133             }
 134         }
 135         out.println(sb);
 136 
 137         Matcher m = DF_PATTERN.matcher(sb);
 138         int j = 0;
 139         while (j < sb.length()) {
 140             if (m.find(j)) {
 141                 // swap can change while this test is running
 142                 if (!m.group(1).equals("swap")) {
 143                     String name = f;
 144                     if (name == null) {
 145                         // cygwin's df lists windows path as FileSystem (1st group)
 146                         name = IS_WIN ? m.group(1) : m.group(4);
 147                     }
 148                     al.add(new Space(m.group(2), m.group(3), name));;
 149                 }
 150                 j = m.end() + 1;
 151             } else {
 152                 throw new RuntimeException("unrecognized df output format: "
 153                                            + "charAt(" + j + ") = '"
 154                                            + sb.charAt(j) + "'");
 155             }
 156         }
 157 
 158         if (al.size() == 0) {
 159             // df did not produce output
 160             String name = (f == null ? "" : f);
 161             al.add(new Space("0", "0", name));
 162         }
 163         return al;
 164     }
 165 
 166     private static void tryCatch(Space s) {


 189             } catch (SecurityException x) {
 190                 out.format(fmt, "getUsableSpace", x);
 191                 pass();
 192             }
 193         }
 194     }
 195 
 196     private static void compare(Space s) {
 197         File f = new File(s.name());
 198         long ts = f.getTotalSpace();
 199         long fs = f.getFreeSpace();
 200         long us = f.getUsableSpace();
 201 
 202         out.format("%s:%n", s.name());
 203         String fmt = "  %-4s total= %12d free = %12d usable = %12d%n";
 204         out.format(fmt, "df", s.total(), 0, s.free());
 205         out.format(fmt, "getX", ts, fs, us);
 206 
 207         // if the file system can dynamically change size, this check will fail
 208         if (ts != s.total()) {
 209             long blockSize = 1;
 210             long numBlocks = 0;
 211             try {
 212                 FileStore fileStore = Files.getFileStore(f.toPath());
 213                 blockSize = fileStore.getBlockSize();
 214                 numBlocks = fileStore.getTotalSpace()/blockSize;
 215             } catch (IOException e) {
 216                 throw new RuntimeException(e);
 217             }
 218 
 219 
 220             // On macOS, the number of 1024 byte blocks might be incorrectly
 221             // calculated by 'df' using integer division by 2 of the number of
 222             // 512 byte blocks, resulting in a size smaller than the actual
 223             // value when the number of blocks is odd.
 224             if (!IS_MAC || blockSize != 512 || numBlocks % 2 == 0
 225                 || ts - s.total() != 512) {
 226                 fail(s.name(), s.total(), "!=", ts);
 227             }
 228         } else {
 229             pass();
 230         }
 231 
 232         // unix df returns statvfs.f_bavail
 233         long tsp = (!IS_WIN ? us : fs);
 234         if (!s.woomFree(tsp)) {
 235             fail(s.name(), s.free(), "??", tsp);
 236         } else {
 237             pass();
 238         }
 239 
 240         if (fs > s.total()) {
 241             fail(s.name(), s.total(), ">", fs);
 242         } else {
 243             pass();
 244         }
 245 
 246         if (us > s.total()) {
 247             fail(s.name(), s.total(), ">", us);
 248         } else {
 249             pass();
 250         }
 251     }
 252 
 253     private static String FILE_PREFIX = "/getSpace.";


< prev index next >