| 1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
| 3 | #ident "$Id$" |
| 4 | /*====== |
| 5 | This file is part of PerconaFT. |
| 6 | |
| 7 | |
| 8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
| 9 | |
| 10 | PerconaFT is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License, version 2, |
| 12 | as published by the Free Software Foundation. |
| 13 | |
| 14 | PerconaFT is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | ---------------------------------------- |
| 23 | |
| 24 | PerconaFT is free software: you can redistribute it and/or modify |
| 25 | it under the terms of the GNU Affero General Public License, version 3, |
| 26 | as published by the Free Software Foundation. |
| 27 | |
| 28 | PerconaFT is distributed in the hope that it will be useful, |
| 29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 | GNU Affero General Public License for more details. |
| 32 | |
| 33 | You should have received a copy of the GNU Affero General Public License |
| 34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 35 | ======= */ |
| 36 | |
| 37 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
| 38 | |
| 39 | #include "toku_path.h" |
| 40 | #include <toku_assert.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <libgen.h> |
| 44 | |
| 45 | const char *toku_test_filename(const char *default_filename) { |
| 46 | const char *filename = getenv("TOKU_TEST_FILENAME" ); |
| 47 | if (filename == nullptr) { |
| 48 | filename = basename((char *) default_filename); |
| 49 | assert(filename != nullptr); |
| 50 | } |
| 51 | return filename; |
| 52 | } |
| 53 | |
| 54 | // Guarantees NUL termination (unless siz == 0) |
| 55 | // siz is full size of dst (including NUL terminator) |
| 56 | // Appends src to end of dst, (truncating if necessary) to use no more than siz bytes (including NUL terminator) |
| 57 | // Returns strnlen(dst, siz) (size (excluding NUL) of string we tried to create) |
| 58 | size_t toku_strlcat(char *dst, const char *src, size_t siz) |
| 59 | { |
| 60 | if (siz == 0) { |
| 61 | return 0; |
| 62 | } |
| 63 | dst[siz-1] = '\0'; //Guarantee NUL termination. |
| 64 | |
| 65 | const size_t old_dst_len = strnlen(dst, siz - 1); |
| 66 | paranoid_invariant(old_dst_len <= siz - 1); |
| 67 | if (old_dst_len == siz - 1) { |
| 68 | // No room for anything more. |
| 69 | return old_dst_len; |
| 70 | } |
| 71 | char *d = &dst[old_dst_len]; //Points to null ptr at end of old string |
| 72 | const size_t remaining_space = siz-old_dst_len-1; |
| 73 | const size_t allowed_src_len = strnlen(src, remaining_space); // Limit to remaining space (leave space for NUL) |
| 74 | paranoid_invariant(allowed_src_len <= remaining_space); |
| 75 | paranoid_invariant(old_dst_len + allowed_src_len < siz); |
| 76 | memcpy(d, src, allowed_src_len); |
| 77 | d[allowed_src_len] = '\0'; // NUL terminate (may be redundant with previous NUL termination) |
| 78 | |
| 79 | return old_dst_len + allowed_src_len; |
| 80 | } |
| 81 | |
| 82 | // Guarantees NUL termination (unless siz == 0) |
| 83 | // siz is full size of dst (including NUL terminator) |
| 84 | // Appends src to end of dst, (truncating if necessary) to use no more than siz bytes (including NUL terminator) |
| 85 | // Returns strnlen(dst, siz) (size (excluding NUL) of string we tried to create) |
| 86 | // |
| 87 | // Implementation note: implemented for simplicity as oppsed to performance |
| 88 | size_t toku_strlcpy(char *dst, const char *src, size_t siz) |
| 89 | { |
| 90 | if (siz == 0) { |
| 91 | return 0; |
| 92 | } |
| 93 | *dst = '\0'; |
| 94 | return toku_strlcat(dst, src, siz); |
| 95 | } |
| 96 | |
| 97 | char *toku_path_join(char *dest, int n, const char *base, ...) { |
| 98 | static const char PATHSEP = '/'; |
| 99 | size_t written; |
| 100 | written = toku_strlcpy(dest, base, TOKU_PATH_MAX); |
| 101 | paranoid_invariant(written < TOKU_PATH_MAX); |
| 102 | paranoid_invariant(dest[written] == '\0'); |
| 103 | |
| 104 | va_list ap; |
| 105 | va_start(ap, base); |
| 106 | for (int i = 1; written < TOKU_PATH_MAX && i < n; ++i) { |
| 107 | if (dest[written - 1] != PATHSEP) { |
| 108 | if (written+2 >= TOKU_PATH_MAX) { |
| 109 | // No room. |
| 110 | break; |
| 111 | } |
| 112 | dest[written++] = PATHSEP; |
| 113 | dest[written] = '\0'; |
| 114 | } |
| 115 | const char *next = va_arg(ap, const char *); |
| 116 | written = toku_strlcat(dest, next, TOKU_PATH_MAX); |
| 117 | paranoid_invariant(written < TOKU_PATH_MAX); |
| 118 | paranoid_invariant(dest[written] == '\0'); |
| 119 | } |
| 120 | va_end(ap); |
| 121 | |
| 122 | // Zero out rest of buffer for security |
| 123 | memset(&dest[written], 0, TOKU_PATH_MAX - written); |
| 124 | return dest; |
| 125 | } |
| 126 | |