libexplain  1.4.D001
libexplain/buffer/emlink.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  * 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/limits.h> /* for PATH_MAX on Solaris */
00021 #include <libexplain/ac/sys/param.h> /* for PATH_MAX except Solaris */
00022 #include <libexplain/ac/sys/stat.h>
00023 #include <libexplain/ac/unistd.h>
00024 
00025 #include <libexplain/buffer/caption_name_type.h>
00026 #include <libexplain/buffer/emlink.h>
00027 #include <libexplain/buffer/file_type.h>
00028 #include <libexplain/buffer/gettext.h>
00029 #include <libexplain/dirname.h>
00030 #include <libexplain/option.h>
00031 
00032 
00033 void
00034 explain_buffer_emlink(explain_string_buffer_t *sb, const char *oldpath,
00035     const char *newpath)
00036 {
00037     struct stat     oldpath_st;
00038 
00039     if (stat(oldpath, &oldpath_st) >= 0)
00040     {
00041         if (S_ISDIR(oldpath_st.st_mode))
00042         {
00043             explain_string_buffer_t qnpdir_sb;
00044             char            npdir[PATH_MAX + 1];
00045             char            qnpdir[PATH_MAX + 1];
00046 
00047             explain_dirname(npdir, newpath, sizeof(npdir));
00048             explain_string_buffer_init(&qnpdir_sb, qnpdir, sizeof(qnpdir));
00049             explain_buffer_caption_name_type
00050             (
00051                 &qnpdir_sb,
00052                 "newpath",
00053                 npdir,
00054                 S_IFDIR
00055             );
00056             explain_string_buffer_printf_gettext
00057             (
00058                 sb,
00059                 /*
00060                  * xgettext: This message is used when explaining an
00061                  * EMLINK error, in the case where a directory needs to
00062                  * re-write its ".." directory entry, and the new ".."
00063                  * would thereby exceed the link limit.
00064                  *
00065                  * Note that this message may be followed by the actual
00066                  * limit in parentheses, so it helps of the last phrase
00067                  * can be sensably followed by it.
00068                  *
00069                  * %1$s => The name (already quoted) and file type
00070                  *         (already translated) of the directory of
00071                  *         newpath that has the problem.
00072                  */
00073                 i18n("oldpath is a directory and the %s already has the "
00074                     "maximum number of links"),
00075                 qnpdir
00076             );
00077         }
00078         else
00079         {
00080             explain_string_buffer_t ftype_sb;
00081             char            ftype[FILE_TYPE_BUFFER_SIZE_MIN];
00082 
00083             explain_string_buffer_init(&ftype_sb, ftype, sizeof(ftype));
00084             explain_buffer_file_type_st(&ftype_sb, &oldpath_st);
00085             explain_string_buffer_printf_gettext
00086             (
00087                 sb,
00088                 /*
00089                  * xgettext: This message is used when explaining an
00090                  * EMLINK error, in the non-directory case where a file
00091                  * already has the maximum number of links.
00092                  *
00093                  * Note that this message may be followed by the actual
00094                  * limit in parentheses, so it helps of the last phrase
00095                  * can be sensably followed by it.
00096                  *
00097                  * %1$s => the file type of the problem file
00098                  *         (already translated)
00099                  */
00100                 i18n("oldpath is a %s and already has the maximum number "
00101                     "of links"),
00102                 ftype
00103             );
00104         }
00105     }
00106     else
00107     {
00108         explain_buffer_gettext
00109         (
00110             sb,
00111             /*
00112              * xgettext: This message is used when explaining an EMLINK
00113              * error, in the case where a specific cause could not be
00114              * determined.
00115              */
00116             i18n("oldpath already has the maximum number of links to "
00117             "it, or oldpath is a directory and the directory "
00118             "containing newpath has the maximum number of links")
00119         );
00120     }
00121     if (explain_option_dialect_specific())
00122     {
00123         long            link_max;
00124 
00125         /*
00126          * By definition, oldpath and newpath are on the same file
00127          * system to get this error, so we don't need to call
00128          * pathconf twice.
00129          */
00130         link_max = pathconf(oldpath, _PC_LINK_MAX);
00131 
00132         if (link_max > 0)
00133             explain_string_buffer_printf(sb, " (%ld)", link_max);
00134     }
00135 }
00136 
00137 
00138 /* vim: set ts=8 sw=4 et : */