1 /*
2 * Copyright 2007-2017 Content Management AG
3 * All rights reserved.
4 *
5 * author: Max Kellermann <mk@cm4all.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "Error.hxx"
34 #include "util/StringFormat.hxx"
35
36 #include <cassert>
37 #include <string.h>
38 #include <sys/time.h>
39
40 extern "C" {
41 #include <nfsc/libnfs.h>
42 }
43
44 static StringBuffer<256>
FormatNfsClientError(struct nfs_context * nfs,const char * msg)45 FormatNfsClientError(struct nfs_context *nfs, const char *msg) noexcept
46 {
47 assert(msg != nullptr);
48
49 const char *msg2 = nfs_get_error(nfs);
50 return StringFormat<256>("%s: %s", msg, msg2);
51 }
52
NfsClientError(struct nfs_context * nfs,const char * msg)53 NfsClientError::NfsClientError(struct nfs_context *nfs, const char *msg) noexcept
54 :std::runtime_error(FormatNfsClientError(nfs, msg).c_str()),
55 code(0) {}
56
57 static StringBuffer<256>
FormatNfsClientError(int err,struct nfs_context * nfs,void * data,const char * msg)58 FormatNfsClientError(int err, struct nfs_context *nfs, void *data,
59 const char *msg) noexcept
60 {
61 assert(msg != nullptr);
62 assert(err < 0);
63
64 const char *msg2 = (const char *)data;
65 if (data == nullptr || *(const char *)data == 0) {
66 msg2 = nfs_get_error(nfs);
67 if (msg2 == nullptr)
68 msg2 = strerror(-err);
69 }
70
71 return StringFormat<256>("%s: %s", msg, msg2);
72 }
73
NfsClientError(int err,struct nfs_context * nfs,void * data,const char * msg)74 NfsClientError::NfsClientError(int err, struct nfs_context *nfs, void *data,
75 const char *msg) noexcept
76 :std::runtime_error(FormatNfsClientError(err, nfs, data, msg).c_str()),
77 code(-err) {}
78