UNPKG

1.36 kBJavaScriptView Raw
1var path = require('path');
2var util = require('util');
3
4var Item = require('./item').Item;
5
6var EMPTY = new Buffer(0);
7var constants = process.binding('constants');
8
9
10
11/**
12 * A directory.
13 * @param {string} name File name.
14 * @constructor
15 */
16var File = exports.File = function File(name) {
17 Item.call(this, name);
18
19 /**
20 * File content.
21 * @type {Buffer}
22 */
23 this._content = EMPTY;
24
25};
26util.inherits(File, Item);
27
28
29/**
30 * Get the file contents.
31 * @return {Buffer} File contents.
32 */
33File.prototype.getContent = function() {
34 this.setATime(new Date());
35 return this._content;
36};
37
38
39/**
40 * Set the file contents.
41 * @param {string|Buffer} content File contents.
42 */
43File.prototype.setContent = function(content) {
44 if (typeof content === 'string') {
45 content = new Buffer(content);
46 } else if (!Buffer.isBuffer(content)) {
47 throw new Error('File content must be a string or buffer');
48 }
49 this._content = content;
50 var now = Date.now();
51 this.setCTime(new Date(now));
52 this.setMTime(new Date(now));
53};
54
55
56/**
57 * Get file stats.
58 * @return {Object} Stats properties.
59 */
60File.prototype.getStats = function() {
61 return {
62 mode: this.getMode() | constants.S_IFREG,
63 atime: this.getATime(),
64 mtime: this.getMTime(),
65 ctime: this.getCTime(),
66 uid: this.getUid(),
67 gid: this.getGid(),
68 size: this.getContent().length
69 };
70};