UNPKG

2.61 kBJavaScriptView Raw
1var constants = process.binding('constants');
2
3
4
5/**
6 * Create a new file descriptor.
7 * @param {number} flags Flags.
8 * @constructor
9 */
10function FileDescriptor(flags) {
11
12 /**
13 * Flags.
14 * @type {number}
15 */
16 this._flags = flags;
17
18 /**
19 * File system item.
20 * @type {Item}
21 */
22 this._item = null;
23
24 /**
25 * Current file position.
26 * @type {number}
27 */
28 this._position = 0;
29
30}
31
32
33/**
34 * Set the item.
35 * @param {Item} item File system item.
36 */
37FileDescriptor.prototype.setItem = function(item) {
38 this._item = item;
39};
40
41
42/**
43 * Get the item.
44 * @return {Item} File system item.
45 */
46FileDescriptor.prototype.getItem = function() {
47 return this._item;
48};
49
50
51/**
52 * Get the current file position.
53 * @return {number} File position.
54 */
55FileDescriptor.prototype.getPosition = function() {
56 return this._position;
57};
58
59
60/**
61 * Set the current file position.
62 * @param {number} position File position.
63 */
64FileDescriptor.prototype.setPosition = function(position) {
65 this._position = position;
66};
67
68
69/**
70 * Check if file opened for appending.
71 * @return {boolean} Opened for appending.
72 */
73FileDescriptor.prototype.isAppend = function() {
74 return ((this._flags & constants.O_APPEND) === constants.O_APPEND);
75};
76
77
78/**
79 * Check if file opened for creation.
80 * @return {boolean} Opened for creation.
81 */
82FileDescriptor.prototype.isCreate = function() {
83 return ((this._flags & constants.O_CREAT) === constants.O_CREAT);
84};
85
86
87/**
88 * Check if file opened for reading.
89 * @return {boolean} Opened for reading.
90 */
91FileDescriptor.prototype.isRead = function() {
92 // special treatment because O_RDONLY is 0
93 return (this._flags === constants.O_RDONLY) ||
94 (this._flags === (constants.O_RDONLY | constants.O_SYNC)) ||
95 ((this._flags & constants.O_RDWR) === constants.O_RDWR);
96};
97
98
99/**
100 * Check if file opened for writing.
101 * @return {boolean} Opened for writing.
102 */
103FileDescriptor.prototype.isWrite = function() {
104 return ((this._flags & constants.O_WRONLY) === constants.O_WRONLY) ||
105 ((this._flags & constants.O_RDWR) === constants.O_RDWR);
106};
107
108
109/**
110 * Check if file opened for truncating.
111 * @return {boolean} Opened for truncating.
112 */
113FileDescriptor.prototype.isTruncate = function() {
114 return (this._flags & constants.O_TRUNC) === constants.O_TRUNC;
115};
116
117
118/**
119 * Check if file opened with exclusive flag.
120 * @return {boolean} Opened with exclusive.
121 */
122FileDescriptor.prototype.isExclusive = function() {
123 return ((this._flags & constants.O_EXCL) === constants.O_EXCL);
124};
125
126
127/**
128 * Export the constructor.
129 * @type {function()}
130 */
131exports = module.exports = FileDescriptor;