Path::strip_last_element removes path delimiter

Remove the trailing slash of a path where the path is not "/".
New Path::append_element convenience function.

Fixes #1744
This commit is contained in:
Emery Hemingway
2016-05-12 12:50:16 +02:00
committed by Christian Helmuth
parent 8b78001858
commit 0a01edded2
4 changed files with 20 additions and 16 deletions

View File

@ -128,8 +128,7 @@ static void resolve_symlinks(char const *path, Absolute_path &resolved_path)
PDBGV("path_element = %s", path_element); PDBGV("path_element = %s", path_element);
try { try {
next_iteration_working_path.append("/"); next_iteration_working_path.append_element(path_element);
next_iteration_working_path.append(path_element);
} catch (Genode::Path_base::Path_too_long) { } catch (Genode::Path_base::Path_too_long) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
throw Symlink_resolve_error(); throw Symlink_resolve_error();
@ -167,7 +166,7 @@ static void resolve_symlinks(char const *path, Absolute_path &resolved_path)
/* relative target */ /* relative target */
next_iteration_working_path.strip_last_element(); next_iteration_working_path.strip_last_element();
try { try {
next_iteration_working_path.append(symlink_target); next_iteration_working_path.append_element(symlink_target);
} catch (Genode::Path_base::Path_too_long) { } catch (Genode::Path_base::Path_too_long) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
throw Symlink_resolve_error(); throw Symlink_resolve_error();
@ -203,7 +202,7 @@ static void resolve_symlinks_except_last_element(char const *path, Absolute_path
Absolute_path absolute_path_last_element(path, cwd().base()); Absolute_path absolute_path_last_element(path, cwd().base());
absolute_path_last_element.keep_only_last_element(); absolute_path_last_element.keep_only_last_element();
try { try {
resolved_path.append(absolute_path_last_element.base()); resolved_path.append_element(absolute_path_last_element.base());
} catch (Genode::Path_base::Path_too_long) { } catch (Genode::Path_base::Path_too_long) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
throw Symlink_resolve_error(); throw Symlink_resolve_error();
@ -364,8 +363,7 @@ extern "C" int fstatat(int libc_fd, char const *path, struct stat *buf, int flag
if (libc_fd == AT_FDCWD) { if (libc_fd == AT_FDCWD) {
abs_path = cwd(); abs_path = cwd();
abs_path.append("/"); abs_path.append_element(path);
abs_path.append(path);
} else { } else {
Libc::File_descriptor *fd = Libc::File_descriptor *fd =
Libc::file_descriptor_allocator()->find_by_libc_fd(libc_fd); Libc::file_descriptor_allocator()->find_by_libc_fd(libc_fd);

View File

@ -4,6 +4,13 @@
* \date 2012-04-11 * \date 2012-04-11
*/ */
/*
* Copyright (C) 2012-2016 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _FILE_SYSTEM__UTIL_H_ #ifndef _FILE_SYSTEM__UTIL_H_
#define _FILE_SYSTEM__UTIL_H_ #define _FILE_SYSTEM__UTIL_H_
@ -89,7 +96,6 @@ namespace File_system {
} catch (Lookup_failed) { } catch (Lookup_failed) {
Genode::Path<MAX_PATH_LEN> target(path); Genode::Path<MAX_PATH_LEN> target(path);
target.strip_last_element(); target.strip_last_element();
target.remove_trailing('/');
fs.close(ensure_dir(fs, target.base())); fs.close(ensure_dir(fs, target.base()));
} }
} }

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (C) 2011-2013 Genode Labs GmbH * Copyright (C) 2011-2016 Genode Labs GmbH
* *
* This file is part of the Genode OS framework, which is distributed * This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2. * under the terms of the GNU General Public License version 2.
@ -267,7 +267,8 @@ class Genode::Path_base
void strip_last_element() void strip_last_element()
{ {
last_element(_path)[1] = 0; char *p = last_element(_path);
p[p == _path ? 1 : 0] = 0;
} }
bool equals(Path_base const &ref) const { return strcmp(ref._path, _path) == 0; } bool equals(Path_base const &ref) const { return strcmp(ref._path, _path) == 0; }
@ -310,6 +311,12 @@ class Genode::Path_base
void append(char const *str) { _append(str); _canonicalize(); } void append(char const *str) { _append(str); _canonicalize(); }
void append_element(char const *str)
{
_append("/"); _append(str);
_canonicalize();
}
bool operator == (char const *other) const bool operator == (char const *other) const
{ {
return strcmp(_path, other) == 0; return strcmp(_path, other) == 0;

View File

@ -173,7 +173,6 @@ class Vfs::Fs_file_system : public File_system
Absolute_path dir_path(path); Absolute_path dir_path(path);
dir_path.strip_last_element(); dir_path.strip_last_element();
dir_path.remove_trailing('/');
Absolute_path file_name(path); Absolute_path file_name(path);
file_name.keep_only_last_element(); file_name.keep_only_last_element();
@ -340,7 +339,6 @@ class Vfs::Fs_file_system : public File_system
{ {
Absolute_path dir_path(path); Absolute_path dir_path(path);
dir_path.strip_last_element(); dir_path.strip_last_element();
dir_path.remove_trailing('/');
Absolute_path file_name(path); Absolute_path file_name(path);
file_name.keep_only_last_element(); file_name.keep_only_last_element();
@ -368,7 +366,6 @@ class Vfs::Fs_file_system : public File_system
*/ */
Absolute_path abs_path(path); Absolute_path abs_path(path);
abs_path.strip_last_element(); abs_path.strip_last_element();
abs_path.remove_trailing('/');
Absolute_path symlink_name(path); Absolute_path symlink_name(path);
symlink_name.keep_only_last_element(); symlink_name.keep_only_last_element();
@ -397,14 +394,12 @@ class Vfs::Fs_file_system : public File_system
Absolute_path from_dir_path(from_path); Absolute_path from_dir_path(from_path);
from_dir_path.strip_last_element(); from_dir_path.strip_last_element();
from_dir_path.remove_trailing('/');
Absolute_path from_file_name(from_path); Absolute_path from_file_name(from_path);
from_file_name.keep_only_last_element(); from_file_name.keep_only_last_element();
Absolute_path to_dir_path(to_path); Absolute_path to_dir_path(to_path);
to_dir_path.strip_last_element(); to_dir_path.strip_last_element();
to_dir_path.remove_trailing('/');
Absolute_path to_file_name(to_path); Absolute_path to_file_name(to_path);
to_file_name.keep_only_last_element(); to_file_name.keep_only_last_element();
@ -457,7 +452,6 @@ class Vfs::Fs_file_system : public File_system
*/ */
Absolute_path abs_path(to); Absolute_path abs_path(to);
abs_path.strip_last_element(); abs_path.strip_last_element();
abs_path.remove_trailing('/');
Absolute_path symlink_name(to); Absolute_path symlink_name(to);
symlink_name.keep_only_last_element(); symlink_name.keep_only_last_element();
@ -528,7 +522,6 @@ class Vfs::Fs_file_system : public File_system
Absolute_path dir_path(path); Absolute_path dir_path(path);
dir_path.strip_last_element(); dir_path.strip_last_element();
dir_path.remove_trailing('/');
Absolute_path file_name(path); Absolute_path file_name(path);
file_name.keep_only_last_element(); file_name.keep_only_last_element();