-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[common] Split
common_dep
into several dependencies
Currently, all `common/` source files are supplied via a single `common_dep`. However in some cases, we may only want to reuse the part of the utility functions in `common/` without importing the other part of Gramine-supplied mini-libc (which might lead to colliding issues). This commit separates `common/` into several dependencies for more flexible usage. Signed-off-by: Kailun Qin <[email protected]>
- Loading branch information
1 parent
bd97741
commit cda5d4d
Showing
14 changed files
with
240 additions
and
179 deletions.
There are no files selected for viewing
This file contains 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 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,13 @@ | ||
/* SPDX-License-Identifier: LGPL-3.0-or-later */ | ||
/* Copyright (C) 2014 Stony Brook University */ | ||
|
||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <stddef.h> | ||
|
||
bool get_norm_path(const char* path, char* buf, size_t* inout_size); | ||
|
||
bool get_base_name(const char* path, char* buf, size_t* inout_size); | ||
|
||
bool is_dot_or_dotdot(const char* name); |
This file contains 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,41 @@ | ||
/* SPDX-License-Identifier: LGPL-3.0-or-later */ | ||
/* Copyright (C) 2014 Stony Brook University */ | ||
|
||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
/*! | ||
* \brief Parse a size (number with optional "G"/"M"/"K" suffix) into an uint64_t. | ||
* | ||
* \param str A string containing a non-negative, decimal number. The string may end with | ||
* "G"/"g" suffix denoting value in GBs, "M"/"m" for MBs, or "K"/"k" for KBs. | ||
* \param[out] out_val Parsed size (in bytes). | ||
* | ||
* \returns 0 on success, negative if string cannot be parsed into a size (e.g., suffix is wrong). | ||
*/ | ||
int parse_size_str(const char* str, uint64_t* out_val); | ||
|
||
/*! | ||
* \brief Convert a string to number. | ||
* | ||
* \param str Input string. | ||
* \param base Digit base, between 2 and 36. | ||
* \param[out] out_value On success, set to the parsed number. | ||
* \param[out] out_end On success, set to the rest of string. | ||
* | ||
* \returns 0 on success, negative on failure. | ||
* | ||
* Parses a number from the beginning of a string. The number should be non-empty, consist of digits | ||
* only (no `+`/`-` signs), and not overflow the `unsigned long` type. For base 16, the "0x" prefix | ||
* is allowed but not required. | ||
*/ | ||
int str_to_ulong(const char* str, unsigned int base, unsigned long* out_value, | ||
const char** out_end); | ||
|
||
bool strstartswith(const char* str, const char* prefix); | ||
|
||
bool strendswith(const char* str, const char* suffix); | ||
|
||
int parse_digit(char c, int base); |
This file contains 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 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 |
---|---|---|
@@ -1,13 +1,23 @@ | ||
/* SPDX-License-Identifier: LGPL-3.0-or-later */ | ||
/* Copyright (C) 2014 Stony Brook University */ | ||
/* Copyright (C) 2019 Invisible Things Lab | ||
* Borys Popławski <[email protected]> | ||
*/ | ||
|
||
/* | ||
* This file contains functions to read app config (manifest) file and create a tree to | ||
* lookup / access config values. | ||
*/ | ||
|
||
#include <stddef.h> | ||
|
||
#ifdef USE_STDLIB | ||
#include <assert.h> | ||
#include <string.h> | ||
#else | ||
#include "api.h" | ||
#include "pal_error.h" | ||
#endif | ||
|
||
#include "path_utils.h" | ||
|
||
/* | ||
* Finds next '/' in `path`. | ||
|
@@ -53,14 +63,14 @@ static inline bool find_prev_slash_offset(const char* path, size_t* offset) { | |
* After returning it holds number of bytes actually written to it (including the ending '\0'). This | ||
* number is never greater than the size of the input path. | ||
*/ | ||
int get_norm_path(const char* path, char* buf, size_t* inout_size) { | ||
bool get_norm_path(const char* path, char* buf, size_t* inout_size) { | ||
assert(path && buf && inout_size); | ||
size_t path_size = strlen(path) + 1; | ||
__UNUSED(path_size); // used only for an assert at the end | ||
(void)(path_size); // used only for an assert at the end | ||
|
||
size_t size = *inout_size; | ||
if (!size) { | ||
return -PAL_ERROR_INVAL; | ||
return false; | ||
} | ||
/* reserve 1 byte for ending '\0' */ | ||
size--; | ||
|
@@ -73,7 +83,7 @@ int get_norm_path(const char* path, char* buf, size_t* inout_size) { | |
/* handle an absolute path */ | ||
if (is_absolute_path) { | ||
if (size < 1) { | ||
return -PAL_ERROR_TOOLONG; | ||
return false; | ||
} | ||
*buf++ = '/'; | ||
size--; | ||
|
@@ -93,7 +103,7 @@ int get_norm_path(const char* path, char* buf, size_t* inout_size) { | |
/* append undiscardable ".." since there is no previous token | ||
* but only if the path is not absolute */ | ||
if (need_slash + 2u > size) { | ||
return -PAL_ERROR_TOOLONG; | ||
return false; | ||
} | ||
if (need_slash) { | ||
*buf++ = '/'; | ||
|
@@ -113,7 +123,7 @@ int get_norm_path(const char* path, char* buf, size_t* inout_size) { | |
} else { | ||
size_t len = (size_t)(end - path); | ||
if (need_slash + len > size - offset) { | ||
return -PAL_ERROR_TOOLONG; | ||
return false; | ||
} | ||
if (need_slash) { | ||
buf[offset++] = '/'; | ||
|
@@ -133,17 +143,17 @@ int get_norm_path(const char* path, char* buf, size_t* inout_size) { | |
*inout_size = ret_size + offset + 1; | ||
assert(*inout_size <= path_size); | ||
|
||
return 0; | ||
return true; | ||
} | ||
|
||
/* | ||
* Returns the part after the last '/' (so `path` should probably be normalized). | ||
* Before calling this function *size should hold the size of buf. | ||
* After returning it holds number of bytes actually written to it (including the trailing '\0'). | ||
*/ | ||
int get_base_name(const char* path, char* buf, size_t* inout_size) { | ||
bool get_base_name(const char* path, char* buf, size_t* inout_size) { | ||
if (!path || !buf || !inout_size) { | ||
return -PAL_ERROR_INVAL; | ||
return false; | ||
} | ||
|
||
const char* end; | ||
|
@@ -153,15 +163,15 @@ int get_base_name(const char* path, char* buf, size_t* inout_size) { | |
|
||
size_t result = (size_t)(end - path); | ||
if (result + 1 > *inout_size) { | ||
return -PAL_ERROR_TOOLONG; | ||
return false; | ||
} | ||
|
||
memcpy(buf, path, result); | ||
buf[result] = '\0'; | ||
|
||
*inout_size = result + 1; | ||
|
||
return 0; | ||
return true; | ||
} | ||
|
||
bool is_dot_or_dotdot(const char* name) { | ||
|
This file contains 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
Oops, something went wrong.