11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This test makes sure that `writeFile()` always writes from the current
41cb0ef41Sopenharmony_ci// position of the file, instead of truncating the file, when used with file
51cb0ef41Sopenharmony_ci// descriptors.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst fs = require('fs');
101cb0ef41Sopenharmony_ciconst join = require('path').join;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  /* writeFileSync() test. */
171cb0ef41Sopenharmony_ci  const filename = join(tmpdir.path, 'test.txt');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  /* Open the file descriptor. */
201cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'w');
211cb0ef41Sopenharmony_ci  try {
221cb0ef41Sopenharmony_ci    /* Write only five characters, so that the position moves to five. */
231cb0ef41Sopenharmony_ci    assert.strictEqual(fs.writeSync(fd, 'Hello'), 5);
241cb0ef41Sopenharmony_ci    assert.strictEqual(fs.readFileSync(filename).toString(), 'Hello');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    /* Write some more with writeFileSync(). */
271cb0ef41Sopenharmony_ci    fs.writeFileSync(fd, 'World');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    /* New content should be written at position five, instead of zero. */
301cb0ef41Sopenharmony_ci    assert.strictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
311cb0ef41Sopenharmony_ci  } finally {
321cb0ef41Sopenharmony_ci    fs.closeSync(fd);
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst fdsToCloseOnExit = [];
371cb0ef41Sopenharmony_ciprocess.on('beforeExit', common.mustCall(() => {
381cb0ef41Sopenharmony_ci  for (const fd of fdsToCloseOnExit) {
391cb0ef41Sopenharmony_ci    try {
401cb0ef41Sopenharmony_ci      fs.closeSync(fd);
411cb0ef41Sopenharmony_ci    } catch {
421cb0ef41Sopenharmony_ci      // Failed to close, ignore
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci}));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci{
481cb0ef41Sopenharmony_ci  /* writeFile() test. */
491cb0ef41Sopenharmony_ci  const file = join(tmpdir.path, 'test1.txt');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  /* Open the file descriptor. */
521cb0ef41Sopenharmony_ci  fs.open(file, 'w', common.mustSucceed((fd) => {
531cb0ef41Sopenharmony_ci    fdsToCloseOnExit.push(fd);
541cb0ef41Sopenharmony_ci    /* Write only five characters, so that the position moves to five. */
551cb0ef41Sopenharmony_ci    fs.write(fd, 'Hello', common.mustSucceed((bytes) => {
561cb0ef41Sopenharmony_ci      assert.strictEqual(bytes, 5);
571cb0ef41Sopenharmony_ci      assert.strictEqual(fs.readFileSync(file).toString(), 'Hello');
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci      /* Write some more with writeFile(). */
601cb0ef41Sopenharmony_ci      fs.writeFile(fd, 'World', common.mustSucceed(() => {
611cb0ef41Sopenharmony_ci        /* New content should be written at position five, instead of zero. */
621cb0ef41Sopenharmony_ci        assert.strictEqual(fs.readFileSync(file).toString(), 'HelloWorld');
631cb0ef41Sopenharmony_ci      }));
641cb0ef41Sopenharmony_ci    }));
651cb0ef41Sopenharmony_ci  }));
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci// Test read-only file descriptor
701cb0ef41Sopenharmony_ci{
711cb0ef41Sopenharmony_ci  const file = join(tmpdir.path, 'test.txt');
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  fs.open(file, 'r', common.mustSucceed((fd) => {
741cb0ef41Sopenharmony_ci    fdsToCloseOnExit.push(fd);
751cb0ef41Sopenharmony_ci    fs.writeFile(fd, 'World', common.expectsError(/EBADF/));
761cb0ef41Sopenharmony_ci  }));
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci// Test with an AbortSignal
801cb0ef41Sopenharmony_ci{
811cb0ef41Sopenharmony_ci  const controller = new AbortController();
821cb0ef41Sopenharmony_ci  const signal = controller.signal;
831cb0ef41Sopenharmony_ci  const file = join(tmpdir.path, 'test.txt');
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  fs.open(file, 'w', common.mustSucceed((fd) => {
861cb0ef41Sopenharmony_ci    fdsToCloseOnExit.push(fd);
871cb0ef41Sopenharmony_ci    fs.writeFile(fd, 'World', { signal }, common.expectsError({
881cb0ef41Sopenharmony_ci      name: 'AbortError'
891cb0ef41Sopenharmony_ci    }));
901cb0ef41Sopenharmony_ci  }));
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  controller.abort();
931cb0ef41Sopenharmony_ci}
94