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