[2830] in BarnOwl Developers
Re: [PATCH] Fixup owl_util_recursive_resolve_link to handle relative symlinks
daemon@ATHENA.MIT.EDU (Nelson Elhage)
Sun Dec 19 17:53:37 2010
In-Reply-To: <1292446673-1471-1-git-send-email-davidben@mit.edu>
Date: Sun, 19 Dec 2010 17:53:34 -0500
From: Nelson Elhage <nelhage@nelhage.com>
To: David Benjamin <davidben@mit.edu>
Cc: barnowl-dev@mit.edu
lgtm.
On Wed, Dec 15, 2010 at 3:57 PM, David Benjamin <davidben@mit.edu> wrote:
> Relative paths in symlinks are resolve relative to the parent directory.
> We need to correctly handle this case.
> ---
> =C2=A0util.c | =C2=A0 39 ++++++++++++++++++++++++++++-----------
> =C2=A01 files changed, 28 insertions(+), 11 deletions(-)
>
> diff --git a/util.c b/util.c
> index d478ed1..eedce02 100644
> --- a/util.c
> +++ b/util.c
> @@ -350,19 +350,36 @@ char *owl_util_stripnewlines(const char *in)
> =C2=A0*/
> =C2=A0gchar *owl_util_recursive_resolve_link(const char *filename)
> =C2=A0{
> - =C2=A0gchar *last_filename =3D g_strdup(filename);
> + =C2=A0gchar *last_path =3D g_strdup(filename);
> =C2=A0 GError *err =3D NULL;
> - =C2=A0while (last_filename !=3D NULL && g_file_test(last_filename, G_FI=
LE_TEST_IS_SYMLINK)) {
> - =C2=A0 =C2=A0gchar *cur_filename =3D g_file_read_link(last_filename, &e=
rr);
> - =C2=A0 =C2=A0g_free(last_filename);
> - =C2=A0 =C2=A0last_filename =3D cur_filename;
> - =C2=A0}
> - =C2=A0if (last_filename =3D=3D NULL) {
> - =C2=A0 =C2=A0owl_function_error("Cannot resolve symlinked file %s: %s",
> - =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0filename, err->message);
> - =C2=A0 =C2=A0g_error_free(err);
> +
> + =C2=A0while (g_file_test(last_path, G_FILE_TEST_IS_SYMLINK)) {
> + =C2=A0 =C2=A0gchar *link_path =3D g_file_read_link(last_path, &err);
> + =C2=A0 =C2=A0if (link_path =3D=3D NULL) {
> + =C2=A0 =C2=A0 =C2=A0owl_function_error("Cannot resolve symlink %s: %s",
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0last_path, err->message);
> + =C2=A0 =C2=A0 =C2=A0g_error_free(err);
> + =C2=A0 =C2=A0 =C2=A0g_free(last_path);
> + =C2=A0 =C2=A0 =C2=A0return NULL;
> + =C2=A0 =C2=A0}
> +
> + =C2=A0 =C2=A0/* Deal with obnoxious relative paths. If we really care, =
all this
> + =C2=A0 =C2=A0 * is racy. Whatever. */
> + =C2=A0 =C2=A0if (!g_path_is_absolute(link_path)) {
> + =C2=A0 =C2=A0 =C2=A0char *last_dir =3D g_path_get_dirname(last_path);
> + =C2=A0 =C2=A0 =C2=A0char *tmp =3D g_build_path(G_DIR_SEPARATOR_S,
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0last_dir,
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0link_path,
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0NULL);
> + =C2=A0 =C2=A0 =C2=A0g_free(last_dir);
> + =C2=A0 =C2=A0 =C2=A0g_free(link_path);
> + =C2=A0 =C2=A0 =C2=A0link_path =3D tmp;
> + =C2=A0 =C2=A0}
> +
> + =C2=A0 =C2=A0g_free(last_path);
> + =C2=A0 =C2=A0last_path =3D link_path;
> =C2=A0 }
> - =C2=A0return last_filename;
> + =C2=A0return last_path;
> =C2=A0}
>
> =C2=A0/* Delete all lines matching "line" from the named file. =C2=A0If n=
o such
> --
> 1.7.3.3.399.g06ddf
>
>