1 /*
2 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Additionally licensed with:
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 package org.spf4j.unix;
33
34 import java.io.IOException;
35 import java.nio.file.AccessDeniedException;
36 import java.nio.file.FileAlreadyExistsException;
37 import java.nio.file.FileSystemException;
38 import java.nio.file.NoSuchFileException;
39 import javax.annotation.Nonnull;
40
41 /**
42 * Internal exception thrown by native methods when error detected.
43 */
44 public final class UnixException extends Exception {
45
46 private static final long serialVersionUID = 1L;
47
48 private final int errno;
49 private final String msg;
50
51 public UnixException(final String msg, final int errNo) {
52 this.errno = errNo;
53 this.msg = msg;
54 }
55
56 public int errno() {
57 return errno;
58 }
59
60 public String errorString() {
61 if (msg != null) {
62 return msg;
63 } else {
64 return "No message available, strerror invocation not implemented yet";
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 return more general FileSystemException.
75 */
76 @Nonnull
77 public IOException translateToIOException(final String file, final String other) {
78 // created with message rather than errno
79 if (msg != null) {
80 return new IOException(msg);
81 }
82
83 // handle specific cases
84 if (errno() == UnixConstants.EACCES) {
85 return new AccessDeniedException(file, other, null);
86 }
87 if (errno() == UnixConstants.ENOENT) {
88 return new NoSuchFileException(file, other, null);
89 }
90 if (errno() == UnixConstants.EEXIST) {
91 return new FileAlreadyExistsException(file, other, null);
92 }
93
94 // fallback to the more general exception
95 return new FileSystemException(file, other, errorString());
96 }
97
98 public void rethrowAsIOException(final String file) throws IOException {
99 IOException x = translateToIOException(file, null);
100 throw x;
101 }
102
103 }