libexplain
1.4.D001
|
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/fcntl.h> 00021 #include <libexplain/ac/sys/stat.h> 00022 00023 #include <libexplain/buffer/fildes.h> 00024 #include <libexplain/buffer/pathname.h> 00025 #include <libexplain/buffer/uid.h> 00026 #include <libexplain/buffer/gid.h> 00027 #include <libexplain/buffer/errno/chown.h> 00028 #include <libexplain/buffer/errno/fchown.h> 00029 #include <libexplain/buffer/errno/lchown.h> 00030 #include <libexplain/buffer/errno/lchownat.h> 00031 #include <libexplain/explanation.h> 00032 #include <libexplain/fileinfo.h> 00033 00034 00035 static void 00036 explain_buffer_errno_lchownat_system_call(explain_string_buffer_t *sb, int 00037 errnum, int fildes, const char *pathname, int uid, int gid) 00038 { 00039 (void)errnum; 00040 explain_string_buffer_puts(sb, "lchownat(fildes = "); 00041 explain_buffer_fildes(sb, fildes); 00042 explain_string_buffer_puts(sb, ", pathname = "); 00043 explain_buffer_pathname(sb, pathname); 00044 explain_string_buffer_puts(sb, ", uid = "); 00045 explain_buffer_uid(sb, uid); 00046 explain_string_buffer_puts(sb, ", gid = "); 00047 explain_buffer_gid(sb, gid); 00048 explain_string_buffer_putc(sb, ')'); 00049 } 00050 00051 00052 static int 00053 is_a_directory(int fildes) 00054 { 00055 struct stat st; 00056 if (fildes == AT_FDCWD) 00057 return 1; 00058 return S_ISDIR(st.st_mode); 00059 } 00060 00061 00062 void 00063 explain_buffer_errno_lchownat_explanation(explain_string_buffer_t *sb, int 00064 errnum, const char *syscall_name, int fildes, const char *pathname, int uid, 00065 int gid) 00066 { 00067 if (is_a_directory(fildes)) 00068 { 00069 if (!pathname || !*pathname) 00070 pathname = "."; 00071 if (pathname[0] == '/') 00072 { 00073 explain_buffer_errno_lchown_explanation(sb, errnum, syscall_name, 00074 pathname, uid, gid); 00075 return; 00076 } 00077 else if (fildes == AT_FDCWD && pathname[0] != '/') 00078 { 00079 explain_buffer_errno_lchown_explanation(sb, errnum, syscall_name, 00080 pathname, uid, gid); 00081 } 00082 else 00083 { 00084 char path2[PATH_MAX]; 00085 explain_string_buffer_t path2_sb; 00086 char dir_of[PATH_MAX]; 00087 explain_fileinfo_self_fd_n(fildes, dir_of, sizeof(dir_of)); 00088 explain_string_buffer_init(&path2_sb, path2, sizeof(path2)); 00089 explain_string_buffer_puts(&path2_sb, dir_of); 00090 explain_string_buffer_path_join(&path2_sb, pathname); 00091 explain_buffer_errno_lchown_explanation(sb, errnum, syscall_name, 00092 path2, uid, gid); 00093 return; 00094 } 00095 } 00096 else 00097 { 00098 /* not a dir => ENOTDIR */ 00099 errnum = ENOTDIR; 00100 00101 if (!pathname) 00102 { 00103 explain_buffer_errno_fchown_explanation(sb, errnum, syscall_name, 00104 fildes, uid, gid, "fildes"); 00105 return; 00106 } 00107 else 00108 { 00109 if (pathname[0] == '/') 00110 { 00111 explain_buffer_errno_lchown_explanation(sb, errnum, 00112 syscall_name, pathname, uid, gid); 00113 return; 00114 } 00115 else 00116 { 00117 char path2[PATH_MAX]; 00118 explain_string_buffer_t path2_sb; 00119 char dir_of[PATH_MAX]; 00120 explain_fileinfo_self_fd_n(fildes, dir_of, sizeof(dir_of)); 00121 explain_string_buffer_init(&path2_sb, path2, sizeof(path2)); 00122 explain_string_buffer_puts(&path2_sb, dir_of); 00123 explain_string_buffer_path_join(&path2_sb, pathname); 00124 explain_buffer_errno_lchown_explanation(sb, errnum, 00125 syscall_name, path2, uid, gid); 00126 return; 00127 } 00128 } 00129 } 00130 } 00131 00132 00133 void 00134 explain_buffer_errno_lchownat(explain_string_buffer_t *sb, int errnum, int 00135 fildes, const char *pathname, int uid, int gid) 00136 { 00137 explain_explanation_t exp; 00138 00139 explain_explanation_init(&exp, errnum); 00140 explain_buffer_errno_lchownat_system_call(&exp.system_call_sb, errnum, 00141 fildes, pathname, uid, gid); 00142 explain_buffer_errno_lchownat_explanation(&exp.explanation_sb, errnum, 00143 "lchownat", fildes, pathname, uid, gid); 00144 explain_explanation_assemble(&exp, sb); 00145 } 00146 00147 00148 /* vim: set ts=8 sw=4 et : */