Home | History | Annotate | Download | only in fs
      1 /*
      2  * Copyright (c) 2008, 2013, 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 sun.nio.fs;
     27 
     28 import java.nio.file.*;
     29 import java.io.IOException;
     30 
     31 /**
     32  * Internal exception thrown by native methods when error detected.
     33  */
     34 
     35 class UnixException extends Exception {
     36     static final long serialVersionUID = 7227016794320723218L;
     37 
     38     private int errno;
     39     private String msg;
     40 
     41     UnixException(int errno) {
     42         this.errno = errno;
     43         this.msg = null;
     44     }
     45 
     46     UnixException(String msg) {
     47         this.errno = 0;
     48         this.msg = msg;
     49     }
     50 
     51     int errno() {
     52         return errno;
     53     }
     54 
     55     void setError(int errno) {
     56         this.errno = errno;
     57         this.msg = null;
     58     }
     59 
     60     String errorString() {
     61         if (msg != null) {
     62             return msg;
     63         } else {
     64             return Util.toString(UnixNativeDispatcher.strerror(errno()));
     65         }
     66     }
     67 
     68     @Override
     69     public String getMessage() {
     70         return errorString();
     71     }
     72 
     73     /**
     74      * Map well known errors to specific exceptions where possible; otherwise
     75      * return more general FileSystemException.
     76      */
     77     private IOException translateToIOException(String file, String other) {
     78         // created with message rather than errno
     79         if (msg != null)
     80             return new IOException(msg);
     81 
     82         // handle specific cases
     83         if (errno() == UnixConstants.EACCES)
     84             return new AccessDeniedException(file, other, null);
     85         if (errno() == UnixConstants.ENOENT)
     86             return new NoSuchFileException(file, other, null);
     87         if (errno() == UnixConstants.EEXIST)
     88             return new FileAlreadyExistsException(file, other, null);
     89 
     90         // fallback to the more general exception
     91         return new FileSystemException(file, other, errorString());
     92     }
     93 
     94     void rethrowAsIOException(String file) throws IOException {
     95         IOException x = translateToIOException(file, null);
     96         throw x;
     97     }
     98 
     99     void rethrowAsIOException(UnixPath file, UnixPath other) throws IOException {
    100         String a = (file == null) ? null : file.getPathForExceptionMessage();
    101         String b = (other == null) ? null : other.getPathForExceptionMessage();
    102         IOException x = translateToIOException(a, b);
    103         throw x;
    104     }
    105 
    106     void rethrowAsIOException(UnixPath file) throws IOException {
    107         rethrowAsIOException(file, null);
    108     }
    109 
    110     IOException asIOException(UnixPath file) {
    111         return translateToIOException(file.getPathForExceptionMessage(), null);
    112     }
    113 }
    114