libexplain  1.4.D001
libexplain/buffer/errno/fstatat.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 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, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
00013  * 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 #include <libexplain/ac/string.h>
00021 
00022 #include <libexplain/buffer/ebadf.h>
00023 #include <libexplain/buffer/einval.h>
00024 #include <libexplain/buffer/enomem.h>
00025 #include <libexplain/buffer/enotdir.h>
00026 #include <libexplain/buffer/errno/fstatat.h>
00027 #include <libexplain/buffer/errno/generic.h>
00028 #include <libexplain/buffer/errno/lstat.h>
00029 #include <libexplain/buffer/fildes.h>
00030 #include <libexplain/buffer/fstatat_flags.h>
00031 #include <libexplain/buffer/pathname.h>
00032 #include <libexplain/buffer/pointer.h>
00033 #include <libexplain/explanation.h>
00034 #include <libexplain/fildes_is_dot.h>
00035 #include <libexplain/fileinfo.h>
00036 
00037 
00038 static void
00039 explain_buffer_errno_fstatat_system_call(explain_string_buffer_t *sb, int
00040     errnum, int fildes, const char *pathname, struct stat *data, int flags)
00041 {
00042     (void)errnum;
00043     explain_string_buffer_puts(sb, "fstatat(fildes = ");
00044     explain_buffer_fildes(sb, fildes);
00045     explain_string_buffer_puts(sb, ", pathname = ");
00046     explain_buffer_pathname(sb, pathname);
00047     explain_string_buffer_puts(sb, ", data = ");
00048     explain_buffer_pointer(sb, data);
00049     explain_string_buffer_puts(sb, ", flags = ");
00050     explain_buffer_fstatat_flags(sb, flags);
00051     explain_string_buffer_putc(sb, ')');
00052 }
00053 
00054 
00055 void
00056 explain_buffer_errno_fstatat_explanation(explain_string_buffer_t *sb,
00057     int errnum, const char *syscall_name, int fildes, const char *pathname,
00058         struct stat *data, int flags)
00059 {
00060     (void)flags;
00061     switch (errnum)
00062     {
00063     case EBADF:
00064         explain_buffer_ebadf(sb, fildes, "fildes");
00065         break;
00066 
00067     case EINVAL:
00068         explain_buffer_einval_vague(sb, "flags");
00069         return;
00070 
00071     case ENOTDIR:
00072         /*
00073          * This is ambiguous, it could refer to fildes or it could refer
00074          * to pathname.
00075          */
00076         if (!explain_fildes_is_a_directory(fildes))
00077         {
00078             explain_buffer_enotdir_fd(sb, fildes, "fildes");
00079             return;
00080         }
00081         break;
00082 
00083     default:
00084         break;
00085     }
00086 
00087     if (explain_fildes_is_dot(fildes))
00088     {
00089         while (*pathname == '/')
00090             ++pathname;
00091         if (!*pathname)
00092             pathname = ".";
00093         explain_buffer_errno_lstat_explanation
00094         (
00095             sb,
00096             errnum,
00097             syscall_name,
00098             pathname,
00099             data
00100         );
00101         return;
00102     }
00103 
00104     {
00105         char long_path[PATH_MAX * 2];
00106         if (explain_fileinfo_self_fd_n(fildes, long_path, sizeof(long_path)))
00107         {
00108             char *lp = long_path + strlen(long_path);
00109             char *end = long_path + sizeof(long_path);
00110             lp = explain_strendcpy(lp, "/", end);
00111             lp = explain_strendcpy(lp, pathname, end);
00112             explain_buffer_errno_lstat_explanation
00113             (
00114                 sb,
00115                 errnum,
00116                 syscall_name,
00117                 long_path,
00118                 data
00119             );
00120             return;
00121         }
00122         /* Fall through... */
00123     }
00124 
00125     explain_buffer_errno_generic(sb, errnum, syscall_name);
00126 }
00127 
00128 
00129 void
00130 explain_buffer_errno_fstatat(explain_string_buffer_t *sb, int errnum, int
00131     fildes, const char *pathname, struct stat *data, int flags)
00132 {
00133     explain_explanation_t exp;
00134 
00135     explain_explanation_init(&exp, errnum);
00136     explain_buffer_errno_fstatat_system_call(&exp.system_call_sb, errnum,
00137         fildes, pathname, data, flags);
00138     explain_buffer_errno_fstatat_explanation(&exp.explanation_sb, errnum,
00139         "fstatat", fildes, pathname, data, flags);
00140     explain_explanation_assemble(&exp, sb);
00141 }
00142 
00143 
00144 /* vim: set ts=8 sw=4 et : */