libexplain  1.4.D001
libexplain/buffer/enotdir.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 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
00008  * published by the Free Software Foundation; either version 3 of the
00009  * License, or (at 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/stdio.h>
00022 #include <libexplain/ac/sys/stat.h>
00023 
00024 #include <libexplain/buffer/enotdir.h>
00025 #include <libexplain/buffer/errno/path_resolution.h>
00026 #include <libexplain/buffer/file_type.h>
00027 #include <libexplain/buffer/wrong_file_type.h>
00028 
00029 
00030 void
00031 explain_buffer_enotdir(explain_string_buffer_t *sb, const char *pathname,
00032     const char *caption, const explain_final_t *final_component)
00033 {
00034     if
00035     (
00036         explain_buffer_errno_path_resolution
00037         (
00038             sb,
00039             ENOTDIR,
00040             pathname,
00041             caption,
00042             final_component
00043         )
00044     )
00045     {
00046         if (must_be_a_directory(final_component))
00047         {
00048             explain_string_buffer_printf
00049             (
00050                 sb,
00051                 "%s, or a directory component of %s, is not, in fact, a "
00052                     "directory",
00053                 caption,
00054                 caption
00055             );
00056         }
00057         else
00058         {
00059             explain_string_buffer_printf
00060             (
00061                 sb,
00062                 "a directory component of %s is not, in fact, a directory",
00063                 caption
00064             );
00065         }
00066     }
00067 }
00068 
00069 
00070 void
00071 explain_buffer_enotdir2(explain_string_buffer_t *sb,
00072     const char *oldpath, const char *oldpath_caption,
00073     const explain_final_t *oldpath_final_component,
00074     const char *newpath, const char *newpath_caption,
00075     const explain_final_t *newpath_final_component)
00076 {
00077     if
00078     (
00079         explain_buffer_errno_path_resolution
00080         (
00081             sb,
00082             ENOTDIR,
00083             oldpath,
00084             oldpath_caption,
00085             oldpath_final_component
00086         )
00087     &&
00088         explain_buffer_errno_path_resolution
00089         (
00090             sb,
00091             ENOTDIR,
00092             newpath,
00093             newpath_caption,
00094             newpath_final_component
00095         )
00096     )
00097     {
00098         char            both[20];
00099 
00100         snprintf
00101         (
00102             both,
00103             sizeof(both),
00104             "%s or %s",
00105             oldpath_caption,
00106             newpath_caption
00107         );
00108 
00109         if
00110         (
00111             must_be_a_directory(oldpath_final_component)
00112         ||
00113             must_be_a_directory(newpath_final_component)
00114         )
00115         {
00116             explain_string_buffer_printf
00117             (
00118                 sb,
00119                 "%s, or a directory component of %s, is not, in fact, a "
00120                     "directory",
00121                 both,
00122                 both
00123             );
00124         }
00125         else
00126         {
00127             explain_string_buffer_printf
00128             (
00129                 sb,
00130                 "a directory component of %s is not, in fact, a directory",
00131                 both
00132             );
00133         }
00134     }
00135 }
00136 
00137 
00138 void
00139 explain_buffer_enotdir_fd(explain_string_buffer_t *sb, int fildes,
00140     const char *caption)
00141 {
00142     explain_buffer_wrong_file_type(sb, fildes, caption, S_IFDIR);
00143 }
00144 
00145 
00146 int
00147 explain_fildes_is_a_directory(int fildes)
00148 {
00149     struct stat st;
00150     if (fstat(fildes, &st) < 0)
00151         return 0;
00152     return !!S_ISDIR(st.st_mode);
00153 }
00154 
00155 
00156 /* vim: set ts=8 sw=4 et : */