-
Notifications
You must be signed in to change notification settings - Fork 112
Add get_env and file permission utilities #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 bd4fb02
add get_env function
bf7132f
style fixup
mikaelarguedas 10e22c5
add docblocks for the newly added functions
mikaelarguedas f8422f7
WIP start adding tests
mikaelarguedas 2fe9441
convert to gtest
mikaelarguedas 550e91e
return NULL on success and error string on failure
mikaelarguedas f53c4db
clarify note on windows read write test
mikaelarguedas 5365144
rename file_permissions to filesystem
mikaelarguedas f9a3123
added test for filesystem functions
mikaelarguedas 69ebc60
update functions docblocks
mikaelarguedas e220bab
fix file flag on windows
mikaelarguedas f4b7fae
include standard on windows
mikaelarguedas 7fb303c
fix performance warning windows
mikaelarguedas 8ded3f5
commit folder for tests
mikaelarguedas 3f05a88
linter and disable test for file/folders not pulled during git checko…
mikaelarguedas 1464f70
Merge branch 'file_permissions_utilities' of https://github.com/ros2/…
mikaelarguedas e7d8848
reenable folder test, cpplint happiness
mikaelarguedas 87873cc
warning unused
mikaelarguedas 8255f35
warning on unused return type only where it makes sense
mikaelarguedas 446015d
C linkage?
mikaelarguedas f7a3b18
visibility macro before check return
mikaelarguedas 642ebdd
fix memory allocation description
mikaelarguedas 3afd1ba
add join_path function
mikaelarguedas 144b729
cleanup includes
mikaelarguedas 7c9f1df
copy-paste issues
mikaelarguedas 4a7e51a
docs
wjwwood ec27710
more docs since utilities_get_env is sort of weird
wjwwood f7ea413
adress review comments
mikaelarguedas bcc2559
add test for null variable passed to get_cwd
mikaelarguedas 9172840
more doc on return value
mikaelarguedas eaa5e37
remove now unused vars
mikaelarguedas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| /// 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_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| * ``` | ||
| * | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks @wjwwood for improving the doc!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 passNULL? I think it's worth just putting a in each docblock that saysFalseis 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.
There was a problem hiding this comment.
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
statwill cause areturn falseso 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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good 9172840