forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
182 lines (161 loc) · 5.03 KB
/
files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2012 Software Freedom Conservancy
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @fileoverview Provides a filesystem API. */
goog.provide('fxdriver.files');
goog.provide('fxdriver.files.File');
/**
* Creates a temporary text file starting with opt_prefix and ending with
* opt_suffix.
*
* The file will be stored in the active firefox profile directory.
*
* @param {string=} opt_prefix Prefix of filename.
* @param {string=} opt_suffix Suffix of filename.
* @return {!fxdriver.files.File} The newly created file.
*/
fxdriver.files.createTempFile = function(opt_prefix, opt_suffix) {
var prefix = opt_prefix || '';
var suffix = opt_suffix || '';
var tmpdir = Components.classes['@mozilla.org/file/directory_service;1'].
getService(Components.interfaces.nsIProperties).
get('ProfD', Components.interfaces.nsILocalFile);
var exists = true;
var file;
while (exists) {
var path =
prefix + Math.round(Math.random() * new Date().getTime()) + suffix;
file = tmpdir.clone();
file.append(path);
exists = file.exists();
}
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
return new fxdriver.files.File(file);
};
/**
* Creates a file handler for a file in the active firefox profile directory
* with the given path.
*
* @param {string} path The path to use.
* @return {fxdriver.files.File} The file handler.
*/
fxdriver.files.getFile = function(path) {
if (!path) {
return;
}
return new fxdriver.files.File(fxdriver.files.getLocalFile_(path));
};
/**
* Gets a local file for the given path. If the file does not exist the file
* is created.
*
* The path should be a full path to a file in the profile directory in use.
*
* @private
* @param {string} path The path to use.
* @return {!nsILocalFile} The file.
*/
fxdriver.files.getLocalFile_ = function(path) {
var file = Components.classes['@mozilla.org/file/directory_service;1'].
getService(Components.interfaces.nsIProperties).
get('ProfD', Components.interfaces.nsILocalFile);
file.initWithPath(path);
if (!file.exists()) {
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
}
return file;
};
/**
* An file abstraction.
*
* @param {!nsIFile} nsIFile The nsIFile to wrap.
* @constructor
*/
fxdriver.files.File = function(nsIFile) {
/** @private {!nsIFile} */
this.nsIFile_ = nsIFile;
};
/**
* Mode for opening files for append.
* 0x02 = write
* 0x10 = append
* See https://developer.mozilla.org/en/PR_Open#Parameters
*
* @const
* @private {number}
*/
fxdriver.files.APPEND_MODE_ = 0x02 | 0x10;
/**
* Mode for opening files for read.
* 0x01 = read
* See https://developer.mozilla.org/en/PR_Open#Parameters
*
* @const
* @private {number}
*/
fxdriver.files.READ_MODE_ = 0x01;
/**
* Appends toAppend to the file.
*
* @param {string} toAppend Text to append.
*/
fxdriver.files.File.prototype.append = function(toAppend) {
var ostream = Components.classes['@mozilla.org/network/file-output-stream;1']
.createInstance(Components.interfaces['nsIFileOutputStream']);
ostream.init(this.nsIFile_, fxdriver.files.APPEND_MODE_, 0666, 0);
var converter =
Components.classes['@mozilla.org/intl/converter-output-stream;1']
.createInstance(Components.interfaces['nsIConverterOutputStream']);
converter.init(ostream, 'UTF-8', 0, 0);
converter.writeString(toAppend);
converter.close();
ostream.close();
};
/**
* Reads the whole contents of the file.
*
* @return {string} Entire contents of the file.
*/
fxdriver.files.File.prototype.read = function() {
var istream = Components.classes['@mozilla.org/network/file-input-stream;1']
.createInstance(Components.interfaces['nsIFileInputStream']);
istream.init(this.nsIFile_, fxdriver.files.READ_MODE_, 0666, 0);
var converter =
Components.classes['@mozilla.org/intl/converter-input-stream;1']
.createInstance(Components.interfaces['nsIConverterInputStream']);
converter.init(istream, 'UTF-8', 1024, 0);
var toReturn = '';
var str = {};
while (converter.readString(4096, str) != 0) {
toReturn = toReturn + str.value;
}
converter.close();
istream.close();
return toReturn;
};
/**
* Resets the file buffer.
*/
fxdriver.files.File.prototype.resetBuffer = function() {
var path = this.nsIFile_.path;
this.nsIFile_.remove(true);
this.nsIFile_ = fxdriver.files.getLocalFile_(path);
};
/**
* Gets the file path of this file.
*
* @return {!string} The file path.
*/
fxdriver.files.File.prototype.getFilePath = function() {
return this.nsIFile_.path;
};