libexplain  1.4.D001
libexplain/buffer/errno/utime.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008-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/dac.h>
00022 #include <libexplain/buffer/eacces.h>
00023 #include <libexplain/buffer/efault.h>
00024 #include <libexplain/buffer/eloop.h>
00025 #include <libexplain/buffer/enametoolong.h>
00026 #include <libexplain/buffer/enoent.h>
00027 #include <libexplain/buffer/enotdir.h>
00028 #include <libexplain/buffer/erofs.h>
00029 #include <libexplain/buffer/errno/generic.h>
00030 #include <libexplain/buffer/errno/path_resolution.h>
00031 #include <libexplain/buffer/errno/utime.h>
00032 #include <libexplain/buffer/pathname.h>
00033 #include <libexplain/buffer/pointer.h>
00034 #include <libexplain/buffer/utimbuf.h>
00035 #include <libexplain/explanation.h>
00036 #include <libexplain/is_efault.h>
00037 
00038 
00039 static void
00040 explain_buffer_errno_utime_system_call(explain_string_buffer_t *sb,
00041     int errnum, const char *pathname, const struct utimbuf *times)
00042 {
00043     (void)errnum;
00044     explain_string_buffer_puts(sb, "utime(pathname = ");
00045     explain_buffer_pathname(sb, pathname);
00046     explain_string_buffer_puts(sb, ", times = ");
00047     explain_buffer_utimbuf(sb, times);
00048     explain_string_buffer_putc(sb, ')');
00049 }
00050 
00051 
00052 void
00053 explain_buffer_errno_utime_explanation(explain_string_buffer_t *sb,
00054     int errnum, const char *syscall_name, const char *pathname,
00055     const struct utimbuf *times)
00056 {
00057     explain_final_t final_component;
00058 
00059     /*
00060      * http://www.opengroup.org/onlinepubs/009695399/functions/utime.html
00061      */
00062     explain_final_init(&final_component);
00063     if (times)
00064         final_component.want_to_modify_inode = 1;
00065     else
00066         final_component.want_to_write = 1;
00067     switch (errnum)
00068     {
00069     case EACCES:
00070         /*
00071          * if time is NULL, change to "now",
00072          * but need write permission
00073          */
00074         explain_buffer_eacces(sb, pathname, "pathname", &final_component);
00075         break;
00076 
00077     case EFAULT:
00078         if (explain_is_efault_path(pathname))
00079             explain_buffer_efault(sb, "pathname");
00080         else
00081             explain_buffer_efault(sb, "times");
00082         break;
00083 
00084     case ELOOP:
00085     case EMLINK:
00086         explain_buffer_eloop(sb, pathname, "pathname", &final_component);
00087         break;
00088 
00089     case ENAMETOOLONG:
00090         explain_buffer_enametoolong(sb, pathname, "pathname", &final_component);
00091         break;
00092 
00093     case ENOENT:
00094         explain_buffer_enoent(sb, pathname, "pathname", &final_component);
00095         break;
00096 
00097     case ENOTDIR:
00098         explain_buffer_enotdir(sb, pathname, "pathname", &final_component);
00099         break;
00100 
00101     case EPERM:
00102         /*
00103          * If times is not NULL, change as given,
00104          * but need inode modify permission
00105          */
00106         explain_buffer_eacces(sb, pathname, "pathname", &final_component);
00107         break;
00108 
00109     case EROFS:
00110         explain_buffer_erofs(sb, pathname, "pathname");
00111         break;
00112 
00113     default:
00114         explain_buffer_errno_generic(sb, errnum, syscall_name);
00115         break;
00116     }
00117 }
00118 
00119 
00120 void
00121 explain_buffer_errno_utime(explain_string_buffer_t *sb, int errnum,
00122     const char *pathname, const struct utimbuf *times)
00123 {
00124     explain_explanation_t exp;
00125 
00126     explain_explanation_init(&exp, errnum);
00127     explain_buffer_errno_utime_system_call
00128     (
00129         &exp.system_call_sb,
00130         errnum,
00131         pathname,
00132         times
00133     );
00134     explain_buffer_errno_utime_explanation
00135     (
00136         &exp.explanation_sb,
00137         errnum,
00138         "utime",
00139         pathname,
00140         times
00141     );
00142     explain_explanation_assemble(&exp, sb);
00143 }
00144 
00145 
00146 /* vim: set ts=8 sw=4 et : */