libexplain  1.4.D001
libexplain/buffer/errno/mkdir.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008-2010, 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/assert.h>
00020 #include <libexplain/ac/errno.h>
00021 #include <libexplain/ac/sys/stat.h>
00022 #include <libexplain/ac/unistd.h>
00023 
00024 #include <libexplain/buffer/eacces.h>
00025 #include <libexplain/buffer/eexist.h>
00026 #include <libexplain/buffer/efault.h>
00027 #include <libexplain/buffer/eloop.h>
00028 #include <libexplain/buffer/emlink.h>
00029 #include <libexplain/buffer/enametoolong.h>
00030 #include <libexplain/buffer/enoent.h>
00031 #include <libexplain/buffer/enomem.h>
00032 #include <libexplain/buffer/enospc.h>
00033 #include <libexplain/buffer/enotdir.h>
00034 #include <libexplain/buffer/eperm.h>
00035 #include <libexplain/buffer/erofs.h>
00036 #include <libexplain/buffer/errno/generic.h>
00037 #include <libexplain/buffer/errno/mkdir.h>
00038 #include <libexplain/buffer/errno/path_resolution.h>
00039 #include <libexplain/buffer/mount_point.h>
00040 #include <libexplain/buffer/permission_mode.h>
00041 #include <libexplain/buffer/pointer.h>
00042 #include <libexplain/dirname.h>
00043 #include <libexplain/get_link_max.h>
00044 #include <libexplain/explanation.h>
00045 
00046 
00047 static void
00048 explain_buffer_errno_mkdir_system_call(explain_string_buffer_t *sb,
00049     int errnum, const char *pathname, int mode)
00050 {
00051     explain_string_buffer_puts(sb, "mkdir(pathname = ");
00052     if (errnum == EFAULT)
00053         explain_buffer_pointer(sb, pathname);
00054     else
00055         explain_string_buffer_puts_quoted(sb, pathname);
00056     explain_string_buffer_puts(sb, ", mode = ");
00057     explain_buffer_permission_mode(sb, mode);
00058     explain_string_buffer_putc(sb, ')');
00059 }
00060 
00061 
00062 void
00063 explain_buffer_errno_mkdir_explanation(explain_string_buffer_t *sb,
00064     int errnum, const char *syscall_name, const char *pathname, int mode)
00065 {
00066     explain_final_t final_component;
00067 
00068     explain_final_init(&final_component);
00069     final_component.must_exist = 0;
00070     final_component.must_not_exist = 1;
00071     final_component.want_to_create = 1;
00072     final_component.follow_symlink = 0;
00073     final_component.st_mode = S_IFDIR | mode;
00074 
00075     switch (errnum)
00076     {
00077     case EACCES:
00078         explain_buffer_eacces(sb, pathname, "pathname", &final_component);
00079         break;
00080 
00081     case EEXIST:
00082         if
00083         (
00084             explain_buffer_errno_path_resolution
00085             (
00086                 sb,
00087                 errnum,
00088                 pathname,
00089                 "pathname",
00090                 &final_component
00091             )
00092         )
00093         {
00094             explain_buffer_eexist(sb, pathname);
00095         }
00096         break;
00097 
00098     case EFAULT:
00099         explain_buffer_efault(sb, "pathname");
00100         break;
00101 
00102     case EMLINK:
00103         {
00104             long            link_max;
00105             struct stat     parent_st;
00106             char            parent[PATH_MAX];
00107 
00108             /*
00109              * The containing directory may already have too many links.
00110              */
00111             explain_dirname(parent, pathname, sizeof(parent));
00112             link_max = explain_get_link_max(parent);
00113             assert(link_max > 0);
00114             if
00115             (
00116                 stat(parent, &parent_st) >= 0
00117             &&
00118                 parent_st.st_nlink >= (unsigned long)link_max
00119             )
00120             {
00121                 explain_buffer_emlink_mkdir(sb, parent, "pathname");
00122                 break;
00123             }
00124         }
00125 
00126         /*
00127          * On BSD an EMLINK error can be returned where Linux would
00128          * return ELOOP.  So fall through...
00129          */
00130 
00131     case ELOOP:
00132         explain_buffer_eloop(sb, pathname, "pathname", &final_component);
00133         break;
00134 
00135     case ENAMETOOLONG:
00136         explain_buffer_enametoolong
00137         (
00138             sb,
00139             pathname,
00140             "pathname",
00141             &final_component
00142         );
00143         break;
00144 
00145     case ENOENT:
00146         explain_buffer_enoent(sb, pathname, "pathname", &final_component);
00147         break;
00148 
00149     case ENOMEM:
00150         explain_buffer_enomem_kernel(sb);
00151         break;
00152 
00153     case ENOSPC:
00154         explain_buffer_enospc(sb, pathname, "pathname");
00155         break;
00156 
00157     case ENOTDIR:
00158         explain_buffer_enotdir(sb, pathname, "pathname", &final_component);
00159         break;
00160 
00161     case EPERM:
00162         explain_buffer_eperm_mknod(sb, pathname, "pathname", S_IFDIR);
00163         break;
00164 
00165     case EROFS:
00166         explain_buffer_erofs(sb, pathname, "pathname");
00167         break;
00168 
00169     default:
00170         explain_buffer_errno_generic(sb, errnum, syscall_name);
00171         break;
00172     }
00173 }
00174 
00175 
00176 void
00177 explain_buffer_errno_mkdir(explain_string_buffer_t *sb, int errnum,
00178     const char *pathname, int mode)
00179 {
00180     explain_explanation_t exp;
00181 
00182     explain_explanation_init(&exp, errnum);
00183     explain_buffer_errno_mkdir_system_call
00184     (
00185         &exp.system_call_sb,
00186         errnum,
00187         pathname,
00188         mode
00189     );
00190     explain_buffer_errno_mkdir_explanation
00191     (
00192         &exp.explanation_sb,
00193         errnum,
00194         "mkdir",
00195         pathname,
00196         mode
00197     );
00198     explain_explanation_assemble(&exp, sb);
00199 }
00200 
00201 
00202 /* vim: set ts=8 sw=4 et : */