"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.atomicWriteFileSync = atomicWriteFileSync;
exports.atomicWriteFile = atomicWriteFile;
/**
 * Atomic file-write helpers.
 *
 * The pattern is "write to a unique tmp file, then rename into place":
 * POSIX rename on the same filesystem is atomic, so the destination is
 * always either the prior content or the new content, never a partial
 * write. The tmp suffix includes pid + timestamp + random bits so
 * concurrent writers don't collide on the same tmp path.
 *
 * Both sync and async variants exist because different call sites have
 * different constraints: CLI command handlers are synchronous tops-to-
 * tails, while service-layer stores prefer `fs.promises`. Unifying on a
 * single style would propagate a signature change through many callers
 * for no real benefit.
 */
const fs = __importStar(require("fs"));
const fsp = __importStar(require("fs/promises"));
function makeTmpPath(target) {
    return `${target}.tmp.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`;
}
/** Atomic write — synchronous variant. */
function atomicWriteFileSync(target, content) {
    const tmp = makeTmpPath(target);
    fs.writeFileSync(tmp, content);
    try {
        fs.renameSync(tmp, target);
    }
    catch (err) {
        // Cross-device rename / ENOSPC / similar — drop the orphaned tmp before
        // surfacing the error so we don't leak file handles.
        try {
            fs.unlinkSync(tmp);
        }
        catch { /* ignore */ }
        throw err;
    }
}
/** Atomic write — asynchronous variant, for fs/promises call sites. */
async function atomicWriteFile(target, content) {
    const tmp = makeTmpPath(target);
    await fsp.writeFile(tmp, content);
    try {
        await fsp.rename(tmp, target);
    }
    catch (err) {
        await fsp.unlink(tmp).catch(() => undefined);
        throw err;
    }
}
//# sourceMappingURL=atomicFileWriter.js.map