Beispiel #1
0
function copyFile(sourceFile, destPath): void {
    checkFileCopy(sourceFile, destPath);

    var filename = path.basename(sourceFile);

    if (fs.existsSync(destPath)) {
        if (fs.lstatSync(destPath).isDirectory()) {
            destPath += "/" + filename;
        }
    } else {
        if (destPath[destPath.length - 1] === "/" || destPath[destPath.length - 1] === "\\") {
            mkdirParentsSync(destPath);
            destPath += filename;
        } else {
            mkdirParentsSync(path.dirname(destPath));
        }
    }

    var bufLength = 64 * 1024;
    var buff = new Buffer(bufLength);

    var fdr = fs.openSync(sourceFile, "r");
    var fdw = fs.openSync(destPath, "w");
    var bytesRead = 1;
    var pos = 0;
    while (bytesRead > 0) {
        bytesRead = fs.readSync(fdr, buff, 0, bufLength, pos);
        fs.writeSync(fdw, buff, 0, bytesRead);
        pos += bytesRead;
    }
    fs.closeSync(fdr);
    fs.closeSync(fdw);
}
 it('should not remove files if nothing is matched', () => {
   fs.closeSync(fs.openSync(path.resolve(tmpDir, 'bar-123'), 'w'));
   fs.closeSync(fs.openSync(path.resolve(tmpDir, 'bar-456'), 'w'));
   fs.closeSync(fs.openSync(path.resolve(tmpDir, 'bar-789'), 'w'));
   expect(removeFiles(tmpDir, [/zebra-.*/g])).toBe('');
   expect(fs.readdirSync(tmpDir).length).toBe(3);
 });
function copyFile(fromPath: string, toPath: string) {

    console.log("Copying from '" + fromPath + "' to '" + toPath);
    makePathTo(toPath);

    const fileSize = (fs.statSync(fromPath)).size,
        bufferSize = Math.min(0x10000, fileSize),
        buffer = new Buffer(bufferSize),
        progress = makeProgress(fileSize),
        handleFrom = fs.openSync(fromPath, "r"),
        handleTo = fs.openSync(toPath, "w");

    try {
        for (;;) {
            const got = fs.readSync(handleFrom, buffer, 0, bufferSize, null);
            if (got <= 0) break;
            fs.writeSync(handleTo, buffer, 0, got);
            progress(got);
        }
        progress(null);
    } finally {
        fs.closeSync(handleFrom);
        fs.closeSync(handleTo);
    }
}
Beispiel #4
0
 var onDone = (err: Error) => {
   remaining--;
   if (remaining === 0) {
     fs.closeSync(fd);
     for (var type in outFds) {
       fs.writeSync(outFds[type], '\n]\n');
       fs.closeSync(outFds[type]);
     }
     console.log('finished!');
   }
 }
 beforeAll(() => {
   // create the directory
   try {
     fs.mkdirSync(tmpDir);
   } catch (err) {
   }
   // create files that should be deleted.
   fs.closeSync(fs.openSync(path.resolve(tmpDir, 'chromedriver_2.41'), 'w'));
   fs.closeSync(
       fs.openSync(path.resolve(tmpDir, 'chromedriver_foo.zip'), 'w'));
   fs.closeSync(
       fs.openSync(path.resolve(tmpDir, 'chromedriver.config.json'), 'w'));
   fs.closeSync(fs.openSync(path.resolve(tmpDir, 'chromedriver.xml'), 'w'));
 });
Beispiel #6
0
function writeToBuffer(filename, buffer) {
  const fd = fs.openSync(filename, 'r');
  const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);
  fs.closeSync(fd);

  return bytesRead;
}
		fs.open (filepath, 'r', function (err, fd) {
			if (!err) {
				// File already exits
				fs.closeSync (fd);
				resolve (null);
			}
			else {
				// File does not exist...create it
				var fs_write_stream = fs.createWriteStream (filepath);

				var gfs = Grid(conn.db);

				//read from mongodb
				var readstream = gfs.createReadStream ({_id: file._id});
				readstream.pipe (fs_write_stream);
				fs_write_stream.on ('close', function () {
					console.log ('file created: ' + filepath);
					resolve (null);
				});
				fs_write_stream.on ('error', function () {
					err = 'file creation error: ' + filepath;
					reject (err);
				});
			}
		});
Beispiel #8
0
function unlockChannelLock(): void {
	'use strict';
	if (channelLockFileDescriptor !== undefined) {
		fs.closeSync(channelLockFileDescriptor);
		channelLockFileDescriptor = undefined;
	}
}
Beispiel #9
0
export function writeFileSync(path: string, data: string | Buffer, options?: IWriteFileOptions): void {
	const ensuredOptions = ensureWriteOptions(options);

	if (ensuredOptions.encoding) {
		data = encode(data, ensuredOptions.encoding.charset, { addBOM: ensuredOptions.encoding.addBOM });
	}

	if (!canFlush) {
		return fs.writeFileSync(path, data, { mode: ensuredOptions.mode, flag: ensuredOptions.flag });
	}

	// Open the file with same flags and mode as fs.writeFile()
	const fd = fs.openSync(path, ensuredOptions.flag, ensuredOptions.mode);

	try {

		// It is valid to pass a fd handle to fs.writeFile() and this will keep the handle open!
		fs.writeFileSync(fd, data);

		// Flush contents (not metadata) of the file to disk
		try {
			fs.fdatasyncSync(fd);
		} catch (syncError) {
			console.warn('[node.js fs] fdatasyncSync is now disabled for this session because it failed: ', syncError);
			canFlush = false;
		}
	} finally {
		fs.closeSync(fd);
	}
}
Beispiel #10
0
 function done_cb(failed: boolean): void {
   print((failed ? '✗' : '✓'));
   if (failed) {
     fs.writeSync(outfile, '\n');
   }
   fs.closeSync(outfile);
 };