mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
LibC: Use shared functon to generate unique filenames
Move some dupliated code into __generate_unique_filename()
This commit is contained in:
parent
17aef7dc99
commit
f6bd4f8691
Notes:
sideshowbarker
2024-07-19 10:39:54 +09:00
Author: https://github.com/shannonbooth Commit: https://github.com/SerenityOS/serenity/commit/f6bd4f86919 Pull-request: https://github.com/SerenityOS/serenity/pull/917 Reviewed-by: https://github.com/awesomekling ✅
@ -104,9 +104,34 @@ static inline T strtol_impl(const char* nptr, char** endptr, int base)
|
||||
return num;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool __generate_unique_filename(char* pattern)
|
||||
{
|
||||
int length = strlen(pattern);
|
||||
|
||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
||||
errno = EINVAL;
|
||||
return false;
|
||||
}
|
||||
|
||||
int start = length - 6;
|
||||
|
||||
static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for (int attempt = 0; attempt < 100; ++attempt) {
|
||||
for (int i = 0; i < 6; ++i)
|
||||
pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
|
||||
struct stat st;
|
||||
int rc = lstat(pattern, &st);
|
||||
if (rc < 0 && errno == ENOENT)
|
||||
return true;
|
||||
}
|
||||
errno = EEXIST;
|
||||
return false;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
// Itanium C++ ABI methods defined in crt0.cpp
|
||||
// Itanium C++ ABI methods defined in crt0.cpp
|
||||
extern int __cxa_atexit(void (*function)(void*), void* paramter, void* dso_handle);
|
||||
extern void __cxa_finalize(void* dso_handle);
|
||||
|
||||
@ -508,28 +533,9 @@ int system(const char* command)
|
||||
|
||||
char* mktemp(char* pattern)
|
||||
{
|
||||
int length = strlen(pattern);
|
||||
|
||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
||||
if (!__generate_unique_filename(pattern))
|
||||
pattern[0] = '\0';
|
||||
errno = EINVAL;
|
||||
return pattern;
|
||||
}
|
||||
|
||||
int start = length - 6;
|
||||
|
||||
static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for (int attempt = 0; attempt < 100; ++attempt) {
|
||||
for (int i = 0; i < 6; ++i)
|
||||
pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
|
||||
struct stat st;
|
||||
int rc = lstat(pattern, &st);
|
||||
if (rc < 0 && errno == ENOENT)
|
||||
return pattern;
|
||||
}
|
||||
pattern[0] = '\0';
|
||||
errno = EEXIST;
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@ -546,31 +552,13 @@ int mkstemp(char* pattern)
|
||||
|
||||
char* mkdtemp(char* pattern)
|
||||
{
|
||||
int length = strlen(pattern);
|
||||
|
||||
if (length < 6 || !String(pattern).ends_with("XXXXXX")) {
|
||||
errno = EINVAL;
|
||||
if (!__generate_unique_filename(pattern))
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int start = length - 6;
|
||||
if (mkdir(pattern, 0700) < 0)
|
||||
return nullptr;
|
||||
|
||||
static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for (int attempt = 0; attempt < 100; ++attempt) {
|
||||
for (int i = 0; i < 6; ++i)
|
||||
pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
|
||||
struct stat st;
|
||||
int rc = lstat(pattern, &st);
|
||||
if (rc < 0 && errno == ENOENT) {
|
||||
if (mkdir(pattern, 0700) < 0)
|
||||
return nullptr;
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
|
||||
errno = EEXIST;
|
||||
return nullptr;
|
||||
return pattern;
|
||||
}
|
||||
|
||||
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
[[nodiscard]] bool __generate_unique_filename(char* pattern);
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define EXIT_SUCCESS 0
|
||||
|
Loading…
Reference in New Issue
Block a user