UNPKG

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