1 | var path = require('path');
|
2 | var util = require('util');
|
3 |
|
4 | var Item = require('./item');
|
5 |
|
6 | var EMPTY = new Buffer(0);
|
7 | var constants = process.binding('constants');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function File() {
|
16 | Item.call(this);
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 | this._content = EMPTY;
|
23 |
|
24 | }
|
25 | util.inherits(File, Item);
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | File.prototype.getContent = function() {
|
33 | this.setATime(new Date());
|
34 | return this._content;
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | File.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 |
|
57 |
|
58 |
|
59 | File.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 |
|
71 |
|
72 |
|
73 | exports = module.exports = File;
|