< 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.security.Permission;
  40 import java.util.ArrayList;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 
  44 import static java.lang.System.out;
  45 
  46 public class GetXSpace {
  47 
  48     private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
  49                                               new DenyRead() };
  50 
  51     private static final String OS_NAME = System.getProperty("os.name");
  52     private static final boolean IS_MAC = OS_NAME.startsWith("Mac");
  53     private static final boolean IS_WIN = OS_NAME.startsWith("Windows");
  54 
  55     // FileSystem Total Used Available Use% MountedOn
  56     private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
  57 
  58     private static int fail = 0;
  59     private static int pass = 0;
  60     private static Throwable first;
  61 
  62     static void pass() {
  63         pass++;
  64     }
  65 
  66     static void fail(String p) {
  67         setFirst(p);
  68         System.err.format("FAILED: %s%n", p);
  69         fail++;
  70     }
  71 
  72     static void fail(String p, long exp, String cmp, long got) {
  73         String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
  74         setFirst(s);
  75         System.err.format("FAILED: %s%n", s);
  76         fail++;


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


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


< prev index next >