get_tmpname() - create a tmp filename for a given filename * * If a tmpdir is defined, use that as the directory to put it in. Otherwise, * the tmp filename is in the same directory as the given name. Note that * there may be no directory at all in the given name! * * The tmp filename is basically the given filename with a dot prepended, and * .XXXXXX appended (for mkstemp() to put its un
| 163 | * make it easier to figure out what purpose a temp file is serving when a |
| 164 | * transfer is in progress. */ |
| 165 | int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique) |
| 166 | { |
| 167 | int maxname, length = 0; |
| 168 | const char *f; |
| 169 | char *suf; |
| 170 | |
| 171 | if (tmpdir) { |
| 172 | /* Note: this can't overflow, so the return value is safe */ |
| 173 | length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2); |
| 174 | fnametmp[length++] = '/'; |
| 175 | } |
| 176 | |
| 177 | if ((f = strrchr(fname, '/')) != NULL) { |
| 178 | ++f; |
| 179 | if (!tmpdir) { |
| 180 | length = f - fname; |
| 181 | /* copy up to and including the slash */ |
| 182 | strlcpy(fnametmp, fname, length + 1); |
| 183 | } |
| 184 | } else |
| 185 | f = fname; |
| 186 | |
| 187 | if (!tmpdir) { /* using a tmpdir avoids the leading dot on our temp names */ |
| 188 | if (*f == '.') /* avoid an extra leading dot for OS X's sake */ |
| 189 | f++; |
| 190 | fnametmp[length++] = '.'; |
| 191 | } |
| 192 | |
| 193 | /* The maxname value is bufsize, and includes space for the '\0'. |
| 194 | * NAME_MAX needs an extra -1 for the name's leading dot. */ |
| 195 | maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN, |
| 196 | NAME_MAX - 1 - TMPNAME_SUFFIX_LEN); |
| 197 | |
| 198 | if (maxname < 0) { |
| 199 | rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname); |
| 200 | fnametmp[0] = '\0'; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | if (maxname) { |
| 205 | int added = strlcpy(fnametmp + length, f, maxname); |
| 206 | if (added >= maxname) |
| 207 | added = maxname - 1; |
| 208 | suf = fnametmp + length + added; |
| 209 | |
| 210 | /* Trim any dangling high-bit chars if the first-trimmed char (if any) is |
| 211 | * also a high-bit char, just in case we cut into a multi-byte sequence. |
| 212 | * We are guaranteed to stop because of the leading '.' we added. */ |
| 213 | if ((int)f[added] & 0x80) { |
| 214 | while ((int)suf[-1] & 0x80) |
| 215 | suf--; |
| 216 | } |
| 217 | /* trim one trailing dot before our suffix's dot */ |
| 218 | if (suf[-1] == '.') |
| 219 | suf--; |
| 220 | } else |
| 221 | suf = fnametmp + length - 1; /* overwrite the leading dot with suffix's dot */ |
| 222 |
no test coverage detected