UNPKG

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