UNPKG

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