UNPKG

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