1 /* 2 * Copyright (c) 2008, 2016, 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 UnixException(int errno)41 UnixException(int errno) { 42 this.errno = errno; 43 this.msg = null; 44 } 45 UnixException(String msg)46 UnixException(String msg) { 47 this.errno = 0; 48 this.msg = msg; 49 } 50 errno()51 int errno() { 52 return errno; 53 } 54 setError(int errno)55 void setError(int errno) { 56 this.errno = errno; 57 this.msg = null; 58 } 59 errorString()60 String errorString() { 61 if (msg != null) { 62 return msg; 63 } else { 64 return Util.toString(UnixNativeDispatcher.strerror(errno())); 65 } 66 } 67 68 @Override getMessage()69 public String getMessage() { 70 return errorString(); 71 } 72 73 @Override fillInStackTrace()74 public Throwable fillInStackTrace() { 75 // This is an internal exception; the stack trace is irrelevant. 76 return this; 77 } 78 79 /** 80 * Map well known errors to specific exceptions where possible; otherwise 81 * return more general FileSystemException. 82 */ translateToIOException(String file, String other)83 private IOException translateToIOException(String file, String other) { 84 // created with message rather than errno 85 if (msg != null) 86 return new IOException(msg); 87 88 // handle specific cases 89 if (errno() == UnixConstants.EACCES) 90 return new AccessDeniedException(file, other, null); 91 if (errno() == UnixConstants.ENOENT) 92 return new NoSuchFileException(file, other, null); 93 if (errno() == UnixConstants.EEXIST) 94 return new FileAlreadyExistsException(file, other, null); 95 if (errno() == UnixConstants.ELOOP) 96 return new FileSystemException(file, other, errorString() 97 + " or unable to access attributes of symbolic link"); 98 99 // fallback to the more general exception 100 return new FileSystemException(file, other, errorString()); 101 } 102 rethrowAsIOException(String file)103 void rethrowAsIOException(String file) throws IOException { 104 IOException x = translateToIOException(file, null); 105 throw x; 106 } 107 rethrowAsIOException(UnixPath file, UnixPath other)108 void rethrowAsIOException(UnixPath file, UnixPath other) throws IOException { 109 String a = (file == null) ? null : file.getPathForExceptionMessage(); 110 String b = (other == null) ? null : other.getPathForExceptionMessage(); 111 IOException x = translateToIOException(a, b); 112 throw x; 113 } 114 rethrowAsIOException(UnixPath file)115 void rethrowAsIOException(UnixPath file) throws IOException { 116 rethrowAsIOException(file, null); 117 } 118 asIOException(UnixPath file)119 IOException asIOException(UnixPath file) { 120 return translateToIOException(file.getPathForExceptionMessage(), null); 121 } 122 } 123