libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008-2011, 2013 Peter Miller 00004 * Written by Peter Miller <pmiller@opensource.org.au> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or (at 00009 * your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <libexplain/ac/errno.h> 00021 #include <libexplain/ac/sys/stat.h> 00022 00023 #include <libexplain/buffer/eacces.h> 00024 #include <libexplain/buffer/efault.h> 00025 #include <libexplain/buffer/eloop.h> 00026 #include <libexplain/buffer/enametoolong.h> 00027 #include <libexplain/buffer/enoent.h> 00028 #include <libexplain/buffer/enomem.h> 00029 #include <libexplain/buffer/enotdir.h> 00030 #include <libexplain/buffer/errno/generic.h> 00031 #include <libexplain/buffer/errno/lstat.h> 00032 #include <libexplain/buffer/errno/path_resolution.h> 00033 #include <libexplain/buffer/pathname.h> 00034 #include <libexplain/buffer/pointer.h> 00035 #include <libexplain/explanation.h> 00036 #include <libexplain/is_efault.h> 00037 #include <libexplain/string_buffer.h> 00038 00039 00040 static void 00041 explain_buffer_errno_lstat_system_call(explain_string_buffer_t *sb, 00042 int errnum, const char *pathname, const struct stat *data) 00043 { 00044 (void)errnum; 00045 explain_string_buffer_printf(sb, "lstat(pathname = "); 00046 explain_buffer_pathname(sb, pathname); 00047 explain_string_buffer_puts(sb, ", data = "); 00048 explain_buffer_pointer(sb, data); 00049 explain_string_buffer_putc(sb, ')'); 00050 } 00051 00052 00053 void 00054 explain_buffer_errno_lstat_explanation(explain_string_buffer_t *sb, 00055 int errnum, const char *syscall_name, const char *pathname, 00056 const struct stat *data) 00057 { 00058 explain_final_t final_component; 00059 00060 (void)data; 00061 explain_final_init(&final_component); 00062 final_component.follow_symlink = 0; 00063 00064 switch (errnum) 00065 { 00066 case EACCES: 00067 explain_buffer_eacces(sb, pathname, "pathname", &final_component); 00068 break; 00069 00070 case EFAULT: 00071 if (explain_is_efault_path(pathname)) 00072 { 00073 explain_buffer_efault(sb, "pathname"); 00074 break; 00075 } 00076 if (explain_is_efault_pointer(data, sizeof(*data))) 00077 { 00078 explain_buffer_efault(sb, "data"); 00079 break; 00080 } 00081 break; 00082 00083 case ELOOP: 00084 case EMLINK: /* BSD */ 00085 explain_buffer_eloop(sb, pathname, "pathname", &final_component); 00086 break; 00087 00088 case ENAMETOOLONG: 00089 explain_buffer_enametoolong 00090 ( 00091 sb, 00092 pathname, 00093 "pathname", 00094 &final_component 00095 ); 00096 break; 00097 00098 case ENOENT: 00099 explain_buffer_enoent(sb, pathname, "pathname", &final_component); 00100 break; 00101 00102 case ENOMEM: 00103 explain_buffer_enomem_kernel(sb); 00104 break; 00105 00106 case ENOTDIR: 00107 explain_buffer_enotdir(sb, pathname, "pathname", &final_component); 00108 break; 00109 00110 default: 00111 explain_buffer_errno_generic(sb, errnum, syscall_name); 00112 break; 00113 } 00114 } 00115 00116 00117 void 00118 explain_buffer_errno_lstat(explain_string_buffer_t *sb, int errnum, 00119 const char *pathname, const struct stat *data) 00120 { 00121 explain_explanation_t exp; 00122 00123 explain_explanation_init(&exp, errnum); 00124 explain_buffer_errno_lstat_system_call 00125 ( 00126 &exp.system_call_sb, 00127 errnum, 00128 pathname, 00129 data 00130 ); 00131 explain_buffer_errno_lstat_explanation 00132 ( 00133 &exp.explanation_sb, 00134 errnum, 00135 "lstat", 00136 pathname, 00137 data 00138 ); 00139 explain_explanation_assemble(&exp, sb); 00140 } 00141 00142 00143 /* vim: set ts=8 sw=4 et : */