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