Skip to content
Snippets Groups Projects
Commit e3015443 authored by Philip Withnall's avatar Philip Withnall
Browse files

shutil: Fix assertion failure in glnx_shutil_mkdir_p_at()


If the directory for @dfd is deleted after being opened,
glnx_shutil_mkdir_p_at() would fail with an assertion failure. Fix that,
and make it return an ENOENT error instead.

Add a unit test.

Signed-off-by: default avatarPhilip Withnall <withnall@endlessm.com>
Reviewed-by: default avatarColin Walters <walters@verbum.org>
Reviewed-by: default avatarJonathan Lebon <jlebon@redhat.com>

https://github.com/ostreedev/ostree/issues/1215
parent 32a42931
No related branches found
No related tags found
No related merge requests found
...@@ -53,7 +53,7 @@ libglnx_la_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags) ...@@ -53,7 +53,7 @@ libglnx_la_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
libglnx_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^glnx_" -no-undefined -export-dynamic libglnx_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^glnx_" -no-undefined -export-dynamic
libglnx_la_LIBADD = $(libglnx_libs) libglnx_la_LIBADD = $(libglnx_libs)
libglnx_tests = test-libglnx-xattrs test-libglnx-fdio test-libglnx-errors test-libglnx-macros libglnx_tests = test-libglnx-xattrs test-libglnx-fdio test-libglnx-errors test-libglnx-macros test-libglnx-shutil
TESTS += $(libglnx_tests) TESTS += $(libglnx_tests)
check_PROGRAMS += $(libglnx_tests) check_PROGRAMS += $(libglnx_tests)
...@@ -72,3 +72,7 @@ test_libglnx_errors_LDADD = $(libglnx_libs) libglnx.la ...@@ -72,3 +72,7 @@ test_libglnx_errors_LDADD = $(libglnx_libs) libglnx.la
test_libglnx_macros_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-macros.c test_libglnx_macros_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-macros.c
test_libglnx_macros_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags) test_libglnx_macros_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_macros_LDADD = $(libglnx_libs) libglnx.la test_libglnx_macros_LDADD = $(libglnx_libs) libglnx.la
test_libglnx_shutil_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-shutil.c
test_libglnx_shutil_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_shutil_LDADD = $(libglnx_libs) libglnx.la
...@@ -148,7 +148,13 @@ mkdir_p_at_internal (int dfd, ...@@ -148,7 +148,13 @@ mkdir_p_at_internal (int dfd,
g_assert (!did_recurse); g_assert (!did_recurse);
lastslash = strrchr (path, '/'); lastslash = strrchr (path, '/');
g_assert (lastslash != NULL); if (lastslash == NULL)
{
/* This can happen if @dfd was deleted between being opened and
* passed to mkdir_p_at_internal(). */
return glnx_throw_errno_prefix (error, "mkdir(%s)", path);
}
/* Note we can mutate the buffer as we dup'd it */ /* Note we can mutate the buffer as we dup'd it */
*lastslash = '\0'; *lastslash = '\0';
...@@ -187,6 +193,9 @@ mkdir_p_at_internal (int dfd, ...@@ -187,6 +193,9 @@ mkdir_p_at_internal (int dfd,
* directory fd @dfd. * directory fd @dfd.
* *
* See also glnx_ensure_dir() for a non-recursive version. * See also glnx_ensure_dir() for a non-recursive version.
*
* This will return %G_IO_ERROR_NOT_FOUND if @dfd has been deleted since being
* opened. It may return other errors from mkdirat() in other situations.
*/ */
gboolean gboolean
glnx_shutil_mkdir_p_at (int dfd, glnx_shutil_mkdir_p_at (int dfd,
......
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright © 2017 Endless Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "libglnx.h"
#include <glib.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <err.h>
#include <string.h>
#include "libglnx-testlib.h"
static void
test_mkdir_p_enoent (void)
{
_GLNX_TEST_DECLARE_ERROR(local_error, error);
glnx_fd_close int dfd = -1;
if (!glnx_ensure_dir (AT_FDCWD, "test", 0755, error))
return;
if (!glnx_opendirat (AT_FDCWD, "test", FALSE, &dfd, error))
return;
if (rmdir ("test") < 0)
return (void) glnx_throw_errno_prefix (error, "rmdir(%s)", "test");
/* This should fail with ENOENT. */
glnx_shutil_mkdir_p_at (dfd, "blah/baz", 0755, NULL, error);
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
g_clear_error (&local_error);
}
int
main (int argc,
char **argv)
{
int ret;
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/mkdir-p/enoent", test_mkdir_p_enoent);
ret = g_test_run();
return ret;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment