Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f5ad9aa
add file permission utility functions
mikaelarguedas Mar 14, 2017
bd4fb02
add get_env function
Mar 20, 2017
bf7132f
style fixup
mikaelarguedas Mar 28, 2017
10e22c5
add docblocks for the newly added functions
mikaelarguedas Mar 28, 2017
f8422f7
WIP start adding tests
mikaelarguedas Mar 29, 2017
2fe9441
convert to gtest
mikaelarguedas Mar 30, 2017
550e91e
return NULL on success and error string on failure
mikaelarguedas Mar 30, 2017
f53c4db
clarify note on windows read write test
mikaelarguedas Mar 30, 2017
5365144
rename file_permissions to filesystem
mikaelarguedas Mar 31, 2017
f9a3123
added test for filesystem functions
mikaelarguedas Mar 31, 2017
69ebc60
update functions docblocks
mikaelarguedas Mar 31, 2017
e220bab
fix file flag on windows
mikaelarguedas Mar 31, 2017
f4b7fae
include standard on windows
mikaelarguedas Mar 31, 2017
7fb303c
fix performance warning windows
mikaelarguedas Mar 31, 2017
8ded3f5
commit folder for tests
mikaelarguedas Mar 31, 2017
3f05a88
linter and disable test for file/folders not pulled during git checko…
mikaelarguedas Mar 31, 2017
1464f70
Merge branch 'file_permissions_utilities' of https://github.com/ros2/…
mikaelarguedas Mar 31, 2017
e7d8848
reenable folder test, cpplint happiness
mikaelarguedas Mar 31, 2017
87873cc
warning unused
mikaelarguedas Mar 31, 2017
8255f35
warning on unused return type only where it makes sense
mikaelarguedas Mar 31, 2017
446015d
C linkage?
mikaelarguedas Apr 1, 2017
f7a3b18
visibility macro before check return
mikaelarguedas Apr 1, 2017
642ebdd
fix memory allocation description
mikaelarguedas Apr 3, 2017
3afd1ba
add join_path function
mikaelarguedas Apr 3, 2017
144b729
cleanup includes
mikaelarguedas Apr 3, 2017
7c9f1df
copy-paste issues
mikaelarguedas Apr 3, 2017
4a7e51a
docs
wjwwood Apr 4, 2017
ec27710
more docs since utilities_get_env is sort of weird
wjwwood Apr 4, 2017
f7ea413
adress review comments
mikaelarguedas Apr 4, 2017
bcc2559
add test for null variable passed to get_cwd
mikaelarguedas Apr 4, 2017
9172840
more doc on return value
mikaelarguedas Apr 4, 2017
eaa5e37
remove now unused vars
mikaelarguedas Apr 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ include_directories(include)
set(utilities_sources
src/cmdline_parser.c
src/concat.c
src/filesystem.c
src/find.c
src/get_env.c
src/split.c
src/string_array.c)
set_source_files_properties(
Expand Down Expand Up @@ -71,6 +73,33 @@ if(BUILD_TESTING)
)
target_link_libraries(test_concat c_utilities)
endif()

ament_add_gtest(test_get_env test/test_get_env.cpp
ENV
EMPTY_TEST=
NORMAL_TEST=foo
APPEND_LIBRARY_DIRS "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
)
if(TARGET test_get_env)
target_link_libraries(test_get_env ${PROJECT_NAME})
if(UNIX AND NOT APPLE)
target_link_libraries(test_get_env pthread)
endif()
if(NOT WIN32)
set_target_properties(test_get_env PROPERTIES COMPILE_FLAGS "-std=c++11")
endif()
endif()

ament_add_gtest(test_filesystem
test/test_filesystem.cpp
)
if(TARGET test_filesystem)
target_include_directories(test_filesystem PUBLIC
${c_utilities_INCLUDE_DIRS}
)
target_link_libraries(test_filesystem c_utilities)
endif()

endif()

ament_export_dependencies(ament_cmake)
Expand Down
123 changes: 123 additions & 0 deletions include/c_utilities/filesystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef C_UTILITIES__FILESYSTEM_H_
#define C_UTILITIES__FILESYSTEM_H_

#if __cplusplus
extern "C"
{
#endif

#include <stdbool.h>

#include "c_utilities/macros.h"
#include "c_utilities/visibility_control.h"

/// Return current working directory.
/**
* \param[in] buffer Allocated string to store current directory path to
* \param[in] max_length maximum length to be stored in buffer
* \return bool True if success
* False if buffer is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
C_UTILITIES_WARN_UNUSED
bool
utilities_get_cwd(char * buffer, size_t max_length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all these functions which take in a char * of some type or another. What happens if I pass NULL? I think it's worth just putting a in each docblock that says False is returned in that case, since that's the easiest solution.

A more extreme, but precise, solution would be to return a return code rather than a bool, so that the user could know if it failed because of the check or because of some other logistical issue, like a null pointer being passed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. For every other function, the check on stat will cause a return false so I think only this function needs to be modified.

I didn't see the need to go all the way to create error codes for these because the scope of these function is pretty limited. But if you think otherwise I can add some

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think documentation should be sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good 9172840


/// Check if the provided path points to a directory.
/**
* \param[in] abs_path Absolute path to check.
* \return bool True if directory
* False if abs_path is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
bool
utilities_is_directory(const char * abs_path);

/// Check if the provided path points to a file.
/**
* \param[in] abs_path Absolute path to check.
* \return bool True if file
* False if abs_path is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
bool
utilities_is_file(const char * abs_path);

/// Check if the provided path points to an existing file/folder.
/**
* \param[in] abs_path Absolute path to check.
* \return bool True if the path exists
* False if abs_path is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
bool
utilities_exists(const char * abs_path);

/// Check if the provided path points to a file/folder readable by current user.
/**
* \param[in] abs_path Absolute path to check.
* \return bool True if the file is readable
* False if abs_path is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
bool
utilities_is_readable(const char * abs_path);

/// Check if the provided path points to a file/folder writable by current user.
/**
* \param[in] abs_path Absolute path to check.
* \return bool True if the file is writable
* False if abs_path is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
bool
utilities_is_writable(const char * abs_path);

/// Check if the provided path points to a file/folder both readable and writable by current user.
/**
* \param[in] abs_path Absolute path to check.
* \return bool True if the file is redable and writable False otherwise
* False if abs_path is NULL
* False on failure
*/
C_UTILITIES_PUBLIC
bool
utilities_is_readable_and_writable(const char * abs_path);

/// Concatenate path adding the right delimiter according to the platform.
/**
* \param[in] left_hand_path
* \param[in] right_hand_path
* \return const char * concatenated path on success
* NULL on invalid arguments
* NULL on failure
*/
C_UTILITIES_PUBLIC
const char *
utilities_join_path(const char * left_hand_path, const char * right_hand_path);

#if __cplusplus
}
#endif

#endif // C_UTILITIES__FILESYSTEM_H_
68 changes: 68 additions & 0 deletions include/c_utilities/get_env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef C_UTILITIES__GET_ENV_H_
#define C_UTILITIES__GET_ENV_H_

#if __cplusplus
extern "C"
{
#endif

#include "c_utilities/macros.h"
#include "c_utilities/visibility_control.h"

/// Retrieve the value of the given environment variable if it exists, or "".
/* The c-string which is returned in the env_value output parameter is only
* valid until the next time this function is called, because it is a direct
* pointer to the static storage.
* The variable env_value populated by this function should never have free()
* called on it.
* If the environment variable is not set, an empty string will be returned.
*
* In both cases, environment variable set or unset, NULL is returned unless
* an exception has occurred, in which case the error string is returned.
* For example:
*
* ```c
* #include <stdio.h>
* #include <c_utilities/get_env.h>
* const char * env_value;
* const char * error_str;
* error_str = utilities_get_env("SOME_ENV_VAR", &env_value);
* if (error_str != NULL) {
* fprintf(stderr, "Error getting env var: %s\n", error_str);
* }
* printf("Valued of 'SOME_ENV_VAR': %s\n", env_value);
* ```
*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @wjwwood for improving the doc!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, np. Please read over it, I'm prone to typos.

* Environment variables will be truncated at 2048 characters on Windows.
*
* This function is not thread-safe.
*
* \param[in] env_name the name of the environment variable
* \param[out] env_value pointer to the value cstring, or "" if unset
* \return NULL on success (success can be returning an empty string)
* error string on failure
*/
C_UTILITIES_PUBLIC
C_UTILITIES_WARN_UNUSED
const char *
utilities_get_env(const char * env_name, const char ** env_value);

#if __cplusplus
}
#endif

#endif // C_UTILITIES__GET_ENV_H_
33 changes: 33 additions & 0 deletions include/c_utilities/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef C_UTILITIES__MACROS_H_
#define C_UTILITIES__MACROS_H_

#if __cplusplus
extern "C"
{
#endif

#ifndef _WIN32
#define C_UTILITIES_WARN_UNUSED __attribute__((warn_unused_result))
#else
#define C_UTILITIES_WARN_UNUSED _Check_return_
#endif

#if __cplusplus
}
#endif

#endif // C_UTILITIES__MACROS_H_
Loading