MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_mkstemp

Function cbm_mkstemp

src/foundation/compat.c:252–323  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

250
251#ifdef _WIN32
252int cbm_mkstemp(char *tmpl) {
253 /* Rewrite /tmp/ to %TEMP%\ like cbm_mkdtemp */
254 /* Per-call storage: daemon project workers can create staging files
255 * concurrently, so a process-global scratch buffer is a data race. */
256 char buf[CBM_SZ_4K];
257 int written;
258 if (strncmp(tmpl, "/tmp/", 5) == 0) {
259 const char *tmp = getenv("TEMP");
260 if (!tmp)
261 tmp = getenv("TMP");
262 if (!tmp)
263 tmp = ".";
264 written = snprintf(buf, sizeof(buf), "%s\\%s", tmp, tmpl + 5);
265 } else {
266 written = snprintf(buf, sizeof(buf), "%s", tmpl);
267 }
268 if (written < 0 || (size_t)written >= sizeof(buf)) {
269 errno = ENAMETOOLONG;
270 return CBM_NOT_FOUND;
271 }
272 /* Keep the six-character mkstemp contract, but do not use _wmktemp:
273 * that CRT helper has a tiny name space on Windows and retained worker
274 * logs can exhaust it during recovery. The exclusive open closes races
275 * with other processes; collisions simply draw another random name. */
276 wchar_t *wide_template = cbm_utf8_to_wide(buf);
277 if (!wide_template) {
278 errno = EINVAL;
279 return CBM_NOT_FOUND;
280 }
281 size_t wide_len = wcslen(wide_template);
282 if (wide_len < 6 || wcscmp(wide_template + wide_len - 6, L"XXXXXX") != 0) {
283 free(wide_template);
284 errno = EINVAL;
285 return CBM_NOT_FOUND;
286 }
287 static const wchar_t hex[] = L"0123456789abcdef";
288 int saved_errno = EEXIST;
289 for (int attempt = 0; attempt < 128; attempt++) {
290 unsigned int random_bits = 0;
291 if (!cbm_secure_random(&random_bits, sizeof(random_bits))) {
292 saved_errno = EIO;
293 break;
294 }
295 for (int digit = 0; digit < 6; digit++) {
296 wide_template[wide_len - 6 + digit] = hex[(random_bits >> (digit * 4)) & 0xf];
297 }
298 char *expanded = cbm_wide_to_utf8(wide_template);
299 wchar_t *wide_open = expanded ? cbm_path_to_wide(expanded) : NULL;
300 if (!expanded || !wide_open || strlen(expanded) >= sizeof(buf)) {
301 free(expanded);
302 free(wide_open);
303 saved_errno = ENAMETOOLONG;
304 break;
305 }
306 int fd = _wopen(wide_open, _O_CREAT | _O_EXCL | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE);
307 saved_errno = errno;
308 free(wide_open);
309 if (fd >= 0) {

Callers 15

tracked_readFunction · 0.85
verify_download_checksumFunction · 0.85
download_verify_installFunction · 0.85
toml_write_atomic_atFunction · 0.85
search_scratch_fileFunction · 0.85
mcp_command_output_pathFunction · 0.85
worker_unique_fileFunction · 0.85
config_write_atomicFunction · 0.85
sha256_vector_okFunction · 0.85

Calls 4

cbm_utf8_to_wideFunction · 0.85
cbm_secure_randomFunction · 0.85
cbm_wide_to_utf8Function · 0.85
cbm_path_to_wideFunction · 0.85

Tested by 5

sha256_vector_okFunction · 0.68
make_temp_dbFunction · 0.68
make_tree_pid_pathFunction · 0.68
subprocess_park_high_fdsFunction · 0.68