libexplain  1.4.D001
libexplain/buffer/errno/readdir.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License, or (at
00008  * your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/errno.h>
00020 
00021 #include <libexplain/buffer/ebadf.h>
00022 #include <libexplain/buffer/efault.h>
00023 #include <libexplain/buffer/eio.h>
00024 #include <libexplain/buffer/enotdir.h>
00025 #include <libexplain/buffer/errno/generic.h>
00026 #include <libexplain/buffer/errno/readdir.h>
00027 #include <libexplain/buffer/dir_to_pathname.h>
00028 #include <libexplain/buffer/is_the_null_pointer.h>
00029 #include <libexplain/buffer/pointer.h>
00030 #include <libexplain/dir_to_fildes.h>
00031 #include <libexplain/explanation.h>
00032 #include <libexplain/is_efault.h>
00033 
00034 
00035 static void
00036 explain_buffer_errno_readdir_system_call(explain_string_buffer_t *sb,
00037     int errnum, DIR *dir)
00038 {
00039     (void)errnum;
00040     explain_string_buffer_puts(sb, "readdir(dir = ");
00041     explain_buffer_pointer(sb, dir);
00042     explain_buffer_dir_to_pathname(sb, dir);
00043     explain_string_buffer_putc(sb, ')');
00044 }
00045 
00046 
00047 static void
00048 explain_buffer_errno_readdir_explanation(explain_string_buffer_t *sb,
00049     int errnum, DIR *dir)
00050 {
00051     int             fildes;
00052 
00053     /*
00054      * Most of these errors are from getdents(2).
00055      */
00056     if (dir == NULL)
00057     {
00058         explain_buffer_is_the_null_pointer(sb, "dir");
00059         return;
00060     }
00061     switch (errnum)
00062     {
00063     case EBADF:
00064         explain_buffer_ebadf_dir(sb, "dir");
00065         break;
00066 
00067     case EFAULT:
00068         /*
00069          * DIR is an opaque type, so we don't really know how big
00070          * is actually is.  So guess.
00071          */
00072         if (explain_is_efault_pointer(dir, sizeof(int)))
00073             explain_buffer_efault(sb, "dir");
00074         else
00075             explain_buffer_efault(sb, "internal buffer");
00076         break;
00077 
00078     case EINVAL:
00079         explain_string_buffer_puts
00080         (
00081             sb,
00082             "internal buffer is too small"
00083         );
00084         break;
00085 
00086     case EIO:
00087         fildes = explain_dir_to_fildes(dir);
00088         explain_buffer_eio_fildes(sb, fildes);
00089         break;
00090 
00091     case ENOTDIR:
00092         fildes = explain_dir_to_fildes(dir);
00093         explain_buffer_enotdir_fd(sb, fildes, "dir");
00094         break;
00095 
00096     default:
00097         explain_buffer_errno_generic(sb, errnum, "readdir");
00098         break;
00099     }
00100 }
00101 
00102 
00103 void
00104 explain_buffer_errno_readdir(explain_string_buffer_t *sb, int errnum,
00105     DIR *dir)
00106 {
00107     explain_explanation_t exp;
00108 
00109     explain_explanation_init(&exp, errnum);
00110     explain_buffer_errno_readdir_system_call
00111     (
00112         &exp.system_call_sb,
00113         errnum,
00114         dir
00115     );
00116     explain_buffer_errno_readdir_explanation
00117     (
00118         &exp.explanation_sb,
00119         errnum,
00120         dir
00121     );
00122     explain_explanation_assemble(&exp, sb);
00123 }
00124 
00125 
00126 /* vim: set ts=8 sw=4 et : */