toolkit/components/osfile/modules/osfile_win_back.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * This file can be used in the following contexts:
michael@0 7 *
michael@0 8 * 1. included from a non-osfile worker thread using importScript
michael@0 9 * (it serves to define a synchronous API for that worker thread)
michael@0 10 * (bug 707681)
michael@0 11 *
michael@0 12 * 2. included from the main thread using Components.utils.import
michael@0 13 * (it serves to define the asynchronous API, whose implementation
michael@0 14 * resides in the worker thread)
michael@0 15 * (bug 729057)
michael@0 16 *
michael@0 17 * 3. included from the osfile worker thread using importScript
michael@0 18 * (it serves to define the implementation of the asynchronous API)
michael@0 19 * (bug 729057)
michael@0 20 */
michael@0 21
michael@0 22 {
michael@0 23 if (typeof Components != "undefined") {
michael@0 24 // We do not wish osfile_win.jsm to be used directly as a main thread
michael@0 25 // module yet. When time comes, it will be loaded by a combination of
michael@0 26 // a main thread front-end/worker thread implementation that makes sure
michael@0 27 // that we are not executing synchronous IO code in the main thread.
michael@0 28
michael@0 29 throw new Error("osfile_win.jsm cannot be used from the main thread yet");
michael@0 30 }
michael@0 31
michael@0 32 (function(exports) {
michael@0 33 "use strict";
michael@0 34 if (exports.OS && exports.OS.Win && exports.OS.Win.File) {
michael@0 35 return; // Avoid double initialization
michael@0 36 }
michael@0 37
michael@0 38 let SharedAll = require("resource://gre/modules/osfile/osfile_shared_allthreads.jsm");
michael@0 39 let SysAll = require("resource://gre/modules/osfile/osfile_win_allthreads.jsm");
michael@0 40 let LOG = SharedAll.LOG.bind(SharedAll, "Unix", "back");
michael@0 41 let libc = SysAll.libc;
michael@0 42 let advapi32 = new SharedAll.Library("advapi32", "advapi32.dll");
michael@0 43 let Const = SharedAll.Constants.Win;
michael@0 44
michael@0 45 /**
michael@0 46 * Initialize the Windows module.
michael@0 47 *
michael@0 48 * @param {function=} declareFFI
michael@0 49 */
michael@0 50 // FIXME: Both |init| and |aDeclareFFI| are deprecated, we should remove them
michael@0 51 let init = function init(aDeclareFFI) {
michael@0 52 let declareFFI;
michael@0 53 if (aDeclareFFI) {
michael@0 54 declareFFI = aDeclareFFI.bind(null, libc);
michael@0 55 } else {
michael@0 56 declareFFI = SysAll.declareFFI;
michael@0 57 }
michael@0 58 let declareLazyFFI = SharedAll.declareLazyFFI;
michael@0 59
michael@0 60 // Initialize types that require additional OS-specific
michael@0 61 // support - either finalization or matching against
michael@0 62 // OS-specific constants.
michael@0 63 let Type = Object.create(SysAll.Type);
michael@0 64 let SysFile = exports.OS.Win.File = { Type: Type };
michael@0 65
michael@0 66 // Initialize types
michael@0 67
michael@0 68 /**
michael@0 69 * A C integer holding INVALID_HANDLE_VALUE in case of error or
michael@0 70 * a file descriptor in case of success.
michael@0 71 */
michael@0 72 Type.HANDLE =
michael@0 73 Type.voidptr_t.withName("HANDLE");
michael@0 74 Type.HANDLE.importFromC = function importFromC(maybe) {
michael@0 75 if (Type.int.cast(maybe).value == INVALID_HANDLE) {
michael@0 76 // Ensure that API clients can effectively compare against
michael@0 77 // Const.INVALID_HANDLE_VALUE. Without this cast,
michael@0 78 // == would always return |false|.
michael@0 79 return INVALID_HANDLE;
michael@0 80 }
michael@0 81 return ctypes.CDataFinalizer(maybe, this.finalizeHANDLE);
michael@0 82 };
michael@0 83 Type.HANDLE.finalizeHANDLE = function placeholder() {
michael@0 84 throw new Error("finalizeHANDLE should be implemented");
michael@0 85 };
michael@0 86 let INVALID_HANDLE = Const.INVALID_HANDLE_VALUE;
michael@0 87
michael@0 88 Type.file_HANDLE = Type.HANDLE.withName("file HANDLE");
michael@0 89 SharedAll.defineLazyGetter(Type.file_HANDLE,
michael@0 90 "finalizeHANDLE",
michael@0 91 function() {
michael@0 92 return SysFile._CloseHandle;
michael@0 93 });
michael@0 94
michael@0 95 Type.find_HANDLE = Type.HANDLE.withName("find HANDLE");
michael@0 96 SharedAll.defineLazyGetter(Type.find_HANDLE,
michael@0 97 "finalizeHANDLE",
michael@0 98 function() {
michael@0 99 return SysFile._FindClose;
michael@0 100 });
michael@0 101
michael@0 102 Type.DWORD = Type.uint32_t.withName("DWORD");
michael@0 103
michael@0 104 /* A special type used to represent flags passed as DWORDs to a function.
michael@0 105 * In JavaScript, bitwise manipulation of numbers, such as or-ing flags,
michael@0 106 * can produce negative numbers. Since DWORD is unsigned, these negative
michael@0 107 * numbers simply cannot be converted to DWORD. For this reason, whenever
michael@0 108 * bit manipulation is called for, you should rather use DWORD_FLAGS,
michael@0 109 * which is represented as a signed integer, hence has the correct
michael@0 110 * semantics.
michael@0 111 */
michael@0 112 Type.DWORD_FLAGS = Type.int32_t.withName("DWORD_FLAGS");
michael@0 113
michael@0 114 /**
michael@0 115 * A C integer holding 0 in case of error or a positive integer
michael@0 116 * in case of success.
michael@0 117 */
michael@0 118 Type.zero_or_DWORD =
michael@0 119 Type.DWORD.withName("zero_or_DWORD");
michael@0 120
michael@0 121 /**
michael@0 122 * A C integer holding 0 in case of error, any other value in
michael@0 123 * case of success.
michael@0 124 */
michael@0 125 Type.zero_or_nothing =
michael@0 126 Type.int.withName("zero_or_nothing");
michael@0 127
michael@0 128 /**
michael@0 129 * A C integer holding flags related to NTFS security.
michael@0 130 */
michael@0 131 Type.SECURITY_ATTRIBUTES =
michael@0 132 Type.void_t.withName("SECURITY_ATTRIBUTES");
michael@0 133
michael@0 134 /**
michael@0 135 * A C integer holding pointers related to NTFS security.
michael@0 136 */
michael@0 137 Type.PSID =
michael@0 138 Type.voidptr_t.withName("PSID");
michael@0 139
michael@0 140 Type.PACL =
michael@0 141 Type.voidptr_t.withName("PACL");
michael@0 142
michael@0 143 Type.PSECURITY_DESCRIPTOR =
michael@0 144 Type.voidptr_t.withName("PSECURITY_DESCRIPTOR");
michael@0 145
michael@0 146 /**
michael@0 147 * A C integer holding Win32 local memory handle.
michael@0 148 */
michael@0 149 Type.HLOCAL =
michael@0 150 Type.voidptr_t.withName("HLOCAL");
michael@0 151
michael@0 152 Type.FILETIME =
michael@0 153 new SharedAll.Type("FILETIME",
michael@0 154 ctypes.StructType("FILETIME", [
michael@0 155 { lo: Type.DWORD.implementation },
michael@0 156 { hi: Type.DWORD.implementation }]));
michael@0 157
michael@0 158 Type.FindData =
michael@0 159 new SharedAll.Type("FIND_DATA",
michael@0 160 ctypes.StructType("FIND_DATA", [
michael@0 161 { dwFileAttributes: ctypes.uint32_t },
michael@0 162 { ftCreationTime: Type.FILETIME.implementation },
michael@0 163 { ftLastAccessTime: Type.FILETIME.implementation },
michael@0 164 { ftLastWriteTime: Type.FILETIME.implementation },
michael@0 165 { nFileSizeHigh: Type.DWORD.implementation },
michael@0 166 { nFileSizeLow: Type.DWORD.implementation },
michael@0 167 { dwReserved0: Type.DWORD.implementation },
michael@0 168 { dwReserved1: Type.DWORD.implementation },
michael@0 169 { cFileName: ctypes.ArrayType(ctypes.jschar, Const.MAX_PATH) },
michael@0 170 { cAlternateFileName: ctypes.ArrayType(ctypes.jschar, 14) }
michael@0 171 ]));
michael@0 172
michael@0 173 Type.FILE_INFORMATION =
michael@0 174 new SharedAll.Type("FILE_INFORMATION",
michael@0 175 ctypes.StructType("FILE_INFORMATION", [
michael@0 176 { dwFileAttributes: ctypes.uint32_t },
michael@0 177 { ftCreationTime: Type.FILETIME.implementation },
michael@0 178 { ftLastAccessTime: Type.FILETIME.implementation },
michael@0 179 { ftLastWriteTime: Type.FILETIME.implementation },
michael@0 180 { dwVolumeSerialNumber: ctypes.uint32_t },
michael@0 181 { nFileSizeHigh: Type.DWORD.implementation },
michael@0 182 { nFileSizeLow: Type.DWORD.implementation },
michael@0 183 { nNumberOfLinks: ctypes.uint32_t },
michael@0 184 { nFileIndex: ctypes.uint64_t }
michael@0 185 ]));
michael@0 186
michael@0 187 Type.SystemTime =
michael@0 188 new SharedAll.Type("SystemTime",
michael@0 189 ctypes.StructType("SystemTime", [
michael@0 190 { wYear: ctypes.int16_t },
michael@0 191 { wMonth: ctypes.int16_t },
michael@0 192 { wDayOfWeek: ctypes.int16_t },
michael@0 193 { wDay: ctypes.int16_t },
michael@0 194 { wHour: ctypes.int16_t },
michael@0 195 { wMinute: ctypes.int16_t },
michael@0 196 { wSecond: ctypes.int16_t },
michael@0 197 { wMilliSeconds: ctypes.int16_t }
michael@0 198 ]));
michael@0 199
michael@0 200 // Special case: these functions are used by the
michael@0 201 // finalizer
michael@0 202 libc.declareLazy(SysFile, "_CloseHandle",
michael@0 203 "CloseHandle", ctypes.winapi_abi,
michael@0 204 /*return */ctypes.bool,
michael@0 205 /*handle*/ ctypes.voidptr_t);
michael@0 206
michael@0 207 SysFile.CloseHandle = function(fd) {
michael@0 208 if (fd == INVALID_HANDLE) {
michael@0 209 return true;
michael@0 210 } else {
michael@0 211 return fd.dispose(); // Returns the value of |CloseHandle|.
michael@0 212 }
michael@0 213 };
michael@0 214
michael@0 215 libc.declareLazy(SysFile, "_FindClose",
michael@0 216 "FindClose", ctypes.winapi_abi,
michael@0 217 /*return */ctypes.bool,
michael@0 218 /*handle*/ ctypes.voidptr_t);
michael@0 219
michael@0 220 SysFile.FindClose = function(handle) {
michael@0 221 if (handle == INVALID_HANDLE) {
michael@0 222 return true;
michael@0 223 } else {
michael@0 224 return handle.dispose(); // Returns the value of |FindClose|.
michael@0 225 }
michael@0 226 };
michael@0 227
michael@0 228 // Declare libc functions as functions of |OS.Win.File|
michael@0 229
michael@0 230 libc.declareLazyFFI(SysFile, "CopyFile",
michael@0 231 "CopyFileW", ctypes.winapi_abi,
michael@0 232 /*return*/ Type.zero_or_nothing,
michael@0 233 /*sourcePath*/ Type.path,
michael@0 234 /*destPath*/ Type.path,
michael@0 235 /*bailIfExist*/Type.bool);
michael@0 236
michael@0 237 libc.declareLazyFFI(SysFile, "CreateDirectory",
michael@0 238 "CreateDirectoryW", ctypes.winapi_abi,
michael@0 239 /*return*/ Type.zero_or_nothing,
michael@0 240 /*name*/ Type.jschar.in_ptr,
michael@0 241 /*security*/Type.SECURITY_ATTRIBUTES.in_ptr);
michael@0 242
michael@0 243 libc.declareLazyFFI(SysFile, "CreateFile",
michael@0 244 "CreateFileW", ctypes.winapi_abi,
michael@0 245 /*return*/ Type.file_HANDLE,
michael@0 246 /*name*/ Type.path,
michael@0 247 /*access*/ Type.DWORD_FLAGS,
michael@0 248 /*share*/ Type.DWORD_FLAGS,
michael@0 249 /*security*/Type.SECURITY_ATTRIBUTES.in_ptr,
michael@0 250 /*creation*/Type.DWORD_FLAGS,
michael@0 251 /*flags*/ Type.DWORD_FLAGS,
michael@0 252 /*template*/Type.HANDLE);
michael@0 253
michael@0 254 libc.declareLazyFFI(SysFile, "DeleteFile",
michael@0 255 "DeleteFileW", ctypes.winapi_abi,
michael@0 256 /*return*/ Type.zero_or_nothing,
michael@0 257 /*path*/ Type.path);
michael@0 258
michael@0 259 libc.declareLazyFFI(SysFile, "FileTimeToSystemTime",
michael@0 260 "FileTimeToSystemTime", ctypes.winapi_abi,
michael@0 261 /*return*/ Type.zero_or_nothing,
michael@0 262 /*filetime*/Type.FILETIME.in_ptr,
michael@0 263 /*systime*/ Type.SystemTime.out_ptr);
michael@0 264
michael@0 265 libc.declareLazyFFI(SysFile, "SystemTimeToFileTime",
michael@0 266 "SystemTimeToFileTime", ctypes.winapi_abi,
michael@0 267 /*return*/ Type.zero_or_nothing,
michael@0 268 /*systime*/ Type.SystemTime.in_ptr,
michael@0 269 /*filetime*/ Type.FILETIME.out_ptr);
michael@0 270
michael@0 271 libc.declareLazyFFI(SysFile, "FindFirstFile",
michael@0 272 "FindFirstFileW", ctypes.winapi_abi,
michael@0 273 /*return*/ Type.find_HANDLE,
michael@0 274 /*pattern*/Type.path,
michael@0 275 /*data*/ Type.FindData.out_ptr);
michael@0 276
michael@0 277 libc.declareLazyFFI(SysFile, "FindNextFile",
michael@0 278 "FindNextFileW", ctypes.winapi_abi,
michael@0 279 /*return*/ Type.zero_or_nothing,
michael@0 280 /*prev*/ Type.find_HANDLE,
michael@0 281 /*data*/ Type.FindData.out_ptr);
michael@0 282
michael@0 283 libc.declareLazyFFI(SysFile, "FormatMessage",
michael@0 284 "FormatMessageW", ctypes.winapi_abi,
michael@0 285 /*return*/ Type.DWORD,
michael@0 286 /*flags*/ Type.DWORD_FLAGS,
michael@0 287 /*source*/ Type.void_t.in_ptr,
michael@0 288 /*msgid*/ Type.DWORD_FLAGS,
michael@0 289 /*langid*/ Type.DWORD_FLAGS,
michael@0 290 /*buf*/ Type.out_wstring,
michael@0 291 /*size*/ Type.DWORD,
michael@0 292 /*Arguments*/Type.void_t.in_ptr
michael@0 293 );
michael@0 294
michael@0 295 libc.declareLazyFFI(SysFile, "GetCurrentDirectory",
michael@0 296 "GetCurrentDirectoryW", ctypes.winapi_abi,
michael@0 297 /*return*/ Type.zero_or_DWORD,
michael@0 298 /*length*/ Type.DWORD,
michael@0 299 /*buf*/ Type.out_path
michael@0 300 );
michael@0 301
michael@0 302 libc.declareLazyFFI(SysFile, "GetDiskFreeSpaceEx",
michael@0 303 "GetDiskFreeSpaceExW", ctypes.winapi_abi,
michael@0 304 /*return*/ Type.zero_or_nothing,
michael@0 305 /*directoryName*/ Type.path,
michael@0 306 /*freeBytesForUser*/ Type.uint64_t.out_ptr,
michael@0 307 /*totalBytesForUser*/ Type.uint64_t.out_ptr,
michael@0 308 /*freeTotalBytesOnDrive*/ Type.uint64_t.out_ptr);
michael@0 309
michael@0 310 libc.declareLazyFFI(SysFile, "GetFileInformationByHandle",
michael@0 311 "GetFileInformationByHandle", ctypes.winapi_abi,
michael@0 312 /*return*/ Type.zero_or_nothing,
michael@0 313 /*handle*/ Type.HANDLE,
michael@0 314 /*info*/ Type.FILE_INFORMATION.out_ptr);
michael@0 315
michael@0 316 libc.declareLazyFFI(SysFile, "MoveFileEx",
michael@0 317 "MoveFileExW", ctypes.winapi_abi,
michael@0 318 /*return*/ Type.zero_or_nothing,
michael@0 319 /*sourcePath*/ Type.path,
michael@0 320 /*destPath*/ Type.path,
michael@0 321 /*flags*/ Type.DWORD
michael@0 322 );
michael@0 323
michael@0 324 libc.declareLazyFFI(SysFile, "ReadFile",
michael@0 325 "ReadFile", ctypes.winapi_abi,
michael@0 326 /*return*/ Type.zero_or_nothing,
michael@0 327 /*file*/ Type.HANDLE,
michael@0 328 /*buffer*/ Type.voidptr_t,
michael@0 329 /*nbytes*/ Type.DWORD,
michael@0 330 /*nbytes_read*/Type.DWORD.out_ptr,
michael@0 331 /*overlapped*/Type.void_t.inout_ptr // FIXME: Implement?
michael@0 332 );
michael@0 333
michael@0 334 libc.declareLazyFFI(SysFile, "RemoveDirectory",
michael@0 335 "RemoveDirectoryW", ctypes.winapi_abi,
michael@0 336 /*return*/ Type.zero_or_nothing,
michael@0 337 /*path*/ Type.path);
michael@0 338
michael@0 339 libc.declareLazyFFI(SysFile, "SetCurrentDirectory",
michael@0 340 "SetCurrentDirectoryW", ctypes.winapi_abi,
michael@0 341 /*return*/ Type.zero_or_nothing,
michael@0 342 /*path*/ Type.path
michael@0 343 );
michael@0 344
michael@0 345 libc.declareLazyFFI(SysFile, "SetEndOfFile",
michael@0 346 "SetEndOfFile", ctypes.winapi_abi,
michael@0 347 /*return*/ Type.zero_or_nothing,
michael@0 348 /*file*/ Type.HANDLE);
michael@0 349
michael@0 350 libc.declareLazyFFI(SysFile, "SetFilePointer",
michael@0 351 "SetFilePointer", ctypes.winapi_abi,
michael@0 352 /*return*/ Type.DWORD,
michael@0 353 /*file*/ Type.HANDLE,
michael@0 354 /*distlow*/Type.long,
michael@0 355 /*disthi*/ Type.long.in_ptr,
michael@0 356 /*method*/ Type.DWORD);
michael@0 357
michael@0 358 libc.declareLazyFFI(SysFile, "SetFileTime",
michael@0 359 "SetFileTime", ctypes.winapi_abi,
michael@0 360 /*return*/ Type.zero_or_nothing,
michael@0 361 /*file*/ Type.HANDLE,
michael@0 362 /*creation*/ Type.FILETIME.in_ptr,
michael@0 363 /*access*/ Type.FILETIME.in_ptr,
michael@0 364 /*write*/ Type.FILETIME.in_ptr);
michael@0 365
michael@0 366
michael@0 367 libc.declareLazyFFI(SysFile, "WriteFile",
michael@0 368 "WriteFile", ctypes.winapi_abi,
michael@0 369 /*return*/ Type.zero_or_nothing,
michael@0 370 /*file*/ Type.HANDLE,
michael@0 371 /*buffer*/ Type.voidptr_t,
michael@0 372 /*nbytes*/ Type.DWORD,
michael@0 373 /*nbytes_wr*/Type.DWORD.out_ptr,
michael@0 374 /*overlapped*/Type.void_t.inout_ptr // FIXME: Implement?
michael@0 375 );
michael@0 376
michael@0 377 libc.declareLazyFFI(SysFile, "FlushFileBuffers",
michael@0 378 "FlushFileBuffers", ctypes.winapi_abi,
michael@0 379 /*return*/ Type.zero_or_nothing,
michael@0 380 /*file*/ Type.HANDLE);
michael@0 381
michael@0 382 libc.declareLazyFFI(SysFile, "GetFileAttributes",
michael@0 383 "GetFileAttributesW", ctypes.winapi_abi,
michael@0 384 /*return*/ Type.DWORD_FLAGS,
michael@0 385 /*fileName*/ Type.path);
michael@0 386
michael@0 387 libc.declareLazyFFI(SysFile, "SetFileAttributes",
michael@0 388 "SetFileAttributesW", ctypes.winapi_abi,
michael@0 389 /*return*/ Type.zero_or_nothing,
michael@0 390 /*fileName*/ Type.path,
michael@0 391 /*fileAttributes*/ Type.DWORD_FLAGS);
michael@0 392
michael@0 393 advapi32.declareLazyFFI(SysFile, "GetNamedSecurityInfo",
michael@0 394 "GetNamedSecurityInfoW", ctypes.winapi_abi,
michael@0 395 /*return*/ Type.DWORD,
michael@0 396 /*objectName*/ Type.path,
michael@0 397 /*objectType*/ Type.DWORD,
michael@0 398 /*securityInfo*/ Type.DWORD,
michael@0 399 /*sidOwner*/ Type.PSID.out_ptr,
michael@0 400 /*sidGroup*/ Type.PSID.out_ptr,
michael@0 401 /*dacl*/ Type.PACL.out_ptr,
michael@0 402 /*sacl*/ Type.PACL.out_ptr,
michael@0 403 /*securityDesc*/ Type.PSECURITY_DESCRIPTOR.out_ptr);
michael@0 404
michael@0 405 advapi32.declareLazyFFI(SysFile, "SetNamedSecurityInfo",
michael@0 406 "SetNamedSecurityInfoW", ctypes.winapi_abi,
michael@0 407 /*return*/ Type.DWORD,
michael@0 408 /*objectName*/ Type.path,
michael@0 409 /*objectType*/ Type.DWORD,
michael@0 410 /*securityInfo*/ Type.DWORD,
michael@0 411 /*sidOwner*/ Type.PSID,
michael@0 412 /*sidGroup*/ Type.PSID,
michael@0 413 /*dacl*/ Type.PACL,
michael@0 414 /*sacl*/ Type.PACL);
michael@0 415
michael@0 416 libc.declareLazyFFI(SysFile, "LocalFree",
michael@0 417 "LocalFree", ctypes.winapi_abi,
michael@0 418 /*return*/ Type.HLOCAL,
michael@0 419 /*mem*/ Type.HLOCAL);
michael@0 420 };
michael@0 421
michael@0 422 exports.OS.Win = {
michael@0 423 File: {
michael@0 424 _init: init
michael@0 425 }
michael@0 426 };
michael@0 427 })(this);
michael@0 428 }

mercurial