Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Corryvreckan
Corryvreckan
Commits
645335d4
Commit
645335d4
authored
Dec 13, 2018
by
Simon Spannagel
Browse files
Utils: update file utilities
parent
3ff1cfb7
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/core/config/ConfigManager.cpp
View file @
645335d4
...
...
@@ -34,7 +34,7 @@ ConfigManager::ConfigManager(std::string file_name) : file_name_(std::move(file_
}
// Convert main file to absolute path
file_name_
=
corryvreckan
::
get_
absolute
_path
(
file_name_
);
file_name_
=
corryvreckan
::
get_
canonical
_path
(
file_name_
);
// Initialize global base configuration with absolute file name
global_base_config_
=
Configuration
(
""
,
file_name_
);
...
...
src/core/config/ConfigReader.cpp
View file @
645335d4
...
...
@@ -101,7 +101,7 @@ void ConfigReader::add(std::istream& stream, std::string file_name) {
// Convert file name to absolute path (if given)
if
(
!
file_name
.
empty
())
{
file_name
=
corryvreckan
::
get_
absolute
_path
(
file_name
);
file_name
=
corryvreckan
::
get_
canonical
_path
(
file_name
);
}
// Build first empty configuration
...
...
src/core/config/Configuration.cpp
View file @
645335d4
...
...
@@ -97,7 +97,7 @@ std::string Configuration::path_to_absolute(std::string path, bool canonicalize_
// Normalize path only if we have to check if it exists
// NOTE: This throws an error if the path does not exist
if
(
canonicalize_path
)
{
path
=
corryvreckan
::
get_
absolute
_path
(
path
);
path
=
corryvreckan
::
get_
canonical
_path
(
path
);
}
}
return
path
;
...
...
src/core/utils/file.h
View file @
645335d4
/**
* @file
* @brief Collection of simple file system utilities
* @copyright Copyright (c) 2017 CERN and the Allpix Squared authors.
* This software is distributed under the terms of the MIT License, copied
* verbatim in the file "LICENSE.md".
* In applying this license, CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an
* @copyright Copyright (c) 2017 CERN and the Corryvreckan authors.
* This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
* In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
...
...
@@ -37,8 +35,7 @@ namespace corryvreckan {
* @return The canonical path (without dots)
* @throws std::invalid_argument If absolute path does not exist on the system
*/
// TODO [DOC] should be renamed get_canonical_path
inline
std
::
string
get_absolute_path
(
const
std
::
string
&
path
)
{
inline
std
::
string
get_canonical_path
(
const
std
::
string
&
path
)
{
char
*
path_ptr
=
realpath
(
path
.
c_str
(),
nullptr
);
if
(
path_ptr
==
nullptr
)
{
// Throw an error if the path does not exist
...
...
@@ -80,8 +77,7 @@ namespace corryvreckan {
* @param path The base directory
* @return A list with the full names of all files in the directory
*
* Does not recurse on subdirectories, but only return the files that are
* directly in the directory.
* Does not recurse on subdirectories, but only return the files that are directly in the directory.
*/
// TODO [doc] check if path exists and ensure canonical paths
inline
std
::
vector
<
std
::
string
>
get_files_in_directory
(
const
std
::
string
&
path
)
{
...
...
@@ -122,11 +118,9 @@ namespace corryvreckan {
* @brief Create a directory
* @param path The path to create
* @param mode The flags permissions of the file to create
* @throws std::invalid_argument If the directory or one of its subpaths cannot
* be created
* @throws std::invalid_argument If the directory or one of its subpaths cannot be created
*
* All the required directories are created from the top-directory until the
* last folder. Therefore it can be used to
* All the required directories are created from the top-directory until the last folder. Therefore it can be used to
* create a structure of directories.
*/
inline
void
create_directories
(
std
::
string
path
,
mode_t
mode
=
0777
)
{
...
...
@@ -160,8 +154,7 @@ namespace corryvreckan {
* @throws std::invalid_argument If the path cannot be removed
* @warning This method is not thread-safe
*
* All the required directories are deleted recursively from the top-directory
* (use this with caution).
* All the required directories are deleted recursively from the top-directory (use this with caution).
*/
inline
void
remove_path
(
const
std
::
string
&
path
)
{
int
status
=
nftw
(
path
.
c_str
(),
...
...
@@ -174,6 +167,45 @@ namespace corryvreckan {
}
}
/*
* @brief Removes a single file from the file system
* @param path Path to the file
* @throws std::invalid_argument If the file cannot be removed
*
* Remove a single file at the given path. If the function returns the deletion was successfull.
*/
inline
void
remove_file
(
const
std
::
string
&
path
)
{
int
status
=
unlink
(
path
.
c_str
());
if
(
status
!=
0
)
{
throw
std
::
invalid_argument
(
"file cannot be deleted"
);
}
}
/**
* @brief Check for the existence of the file extension and add it if not present
* @param path File name or path to file
* @param extension File extension (without separating dot) to be checked for or added
* @return File name or path to file including the appropriate file extension
*/
inline
std
::
string
add_file_extension
(
const
std
::
string
&
path
,
std
::
string
extension
)
{
if
(
extension
.
empty
())
{
return
path
;
}
// Add separating dot if not present:
if
(
extension
.
at
(
0
)
!=
'.'
)
{
extension
.
insert
(
0
,
1
,
'.'
);
}
if
(
path
.
size
()
>
extension
.
size
())
{
if
(
std
::
equal
(
extension
.
rbegin
(),
extension
.
rend
(),
path
.
rbegin
()))
{
return
path
;
}
}
return
path
+
extension
;
}
/**
* @brief Get the name of the file together with the extension
* @param path Absolute path to the file
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment