libexplain  1.4.D001
libexplain/buffer/errno/fchownat.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/chown.h>
00027 #include <libexplain/buffer/errno/fchown.h>
00028 #include <libexplain/buffer/errno/fchownat.h>
00029 #include <libexplain/buffer/errno/generic.h>
00030 #include <libexplain/buffer/fildes.h>
00031 #include <libexplain/buffer/fstatat_flags.h>
00032 #include <libexplain/buffer/gid.h>
00033 #include <libexplain/buffer/pathname.h>
00034 #include <libexplain/buffer/uid.h>
00035 #include <libexplain/explanation.h>
00036 #include <libexplain/fildes_is_dot.h>
00037 #include <libexplain/fileinfo.h>
00038 
00039 
00040 static void
00041 explain_buffer_errno_fchownat_system_call(explain_string_buffer_t *sb,
00042     int errnum, int fildes, const char *pathname, int uid, int gid,
00043     int flags)
00044 {
00045     (void)errnum;
00046     explain_string_buffer_puts(sb, "fchownat(fildes = ");
00047     explain_buffer_fildes(sb, fildes);
00048     explain_string_buffer_puts(sb, ", pathname = ");
00049     explain_buffer_pathname(sb, pathname);
00050     explain_string_buffer_puts(sb, ", uid = ");
00051     explain_buffer_uid(sb, uid);
00052     explain_string_buffer_puts(sb, ", gid = ");
00053     explain_buffer_gid(sb, gid);
00054     explain_string_buffer_puts(sb, ", flags = ");
00055     explain_buffer_fstatat_flags(sb, flags);
00056     explain_string_buffer_putc(sb, ')');
00057 }
00058 
00059 
00060 void
00061 explain_buffer_errno_fchownat_explanation(explain_string_buffer_t *sb,
00062     int errnum, const char *syscall_name, int fildes, const char *pathname,
00063     int uid, int gid, int flags)
00064 {
00065     (void)flags;
00066     switch (errnum)
00067     {
00068     case EBADF:
00069         explain_buffer_ebadf(sb, fildes, "fildes");
00070         break;
00071 
00072     case EINVAL:
00073         explain_buffer_einval_vague(sb, "flags");
00074         break;
00075 
00076     case ENOTDIR:
00077         /*
00078          * This is ambiguous, it could refer to fildes or it could refer
00079          * to pathname.
00080          */
00081         if (!explain_fildes_is_a_directory(fildes))
00082         {
00083             explain_buffer_enotdir_fd(sb, fildes, "fildes");
00084             return;
00085         }
00086         break;
00087 
00088     case ENOMEM:
00089         explain_buffer_enomem_kernel(sb);
00090         return;
00091 
00092     default:
00093         break;
00094     }
00095 
00096     if (explain_fildes_is_dot(fildes))
00097     {
00098         while (*pathname == '/')
00099             ++pathname;
00100         if (!*pathname)
00101             pathname = ".";
00102         explain_buffer_errno_chown_explanation
00103         (
00104             sb,
00105             errnum,
00106             syscall_name,
00107             pathname,
00108             uid,
00109             gid,
00110             "pathname"
00111         );
00112         return;
00113     }
00114 
00115     {
00116         char long_path[PATH_MAX * 2];
00117         if (explain_fileinfo_self_fd_n(fildes, long_path, sizeof(long_path)))
00118         {
00119             char *lp = long_path + strlen(long_path);
00120             char *end = long_path + sizeof(long_path);
00121             lp = explain_strendcpy(lp, "/", end);
00122             lp = explain_strendcpy(lp, pathname, end);
00123             explain_buffer_errno_chown_explanation
00124             (
00125                 sb,
00126                 errnum,
00127                 syscall_name,
00128                 long_path,
00129                 uid,
00130                 gid,
00131                 "pathname"
00132             );
00133             return;
00134         }
00135         /* Fall through... */
00136     }
00137 
00138     explain_buffer_errno_generic(sb, errnum, syscall_name);
00139 }
00140 
00141 
00142 void
00143 explain_buffer_errno_fchownat(explain_string_buffer_t *sb, int errnum,
00144     int fildes, const char *pathname, int uid, int gid, int flags)
00145 {
00146     explain_explanation_t exp;
00147 
00148     explain_explanation_init(&exp, errnum);
00149     explain_buffer_errno_fchownat_system_call(&exp.system_call_sb, errnum,
00150         fildes, pathname, uid, gid, flags);
00151     explain_buffer_errno_fchownat_explanation(&exp.explanation_sb, errnum,
00152         "fchownat", fildes, pathname, uid, gid, flags);
00153     explain_explanation_assemble(&exp, sb);
00154 }
00155 
00156 
00157 /* vim: set ts=8 sw=4 et : */