1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/update/updater/win_dirent.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "win_dirent.h" 1.11 +#include <errno.h> 1.12 +#include <string.h> 1.13 + 1.14 +// This file implements the minimum set of dirent APIs used by updater.cpp on 1.15 +// Windows. If updater.cpp is modified to use more of this API, we need to 1.16 +// implement those parts here too. 1.17 + 1.18 +static dirent gDirEnt; 1.19 + 1.20 +DIR::DIR(const WCHAR* path) 1.21 + : findHandle(INVALID_HANDLE_VALUE) 1.22 +{ 1.23 + memset(name, 0, sizeof(name)); 1.24 + wcsncpy(name, path, sizeof(name)/sizeof(name[0])); 1.25 + wcsncat(name, L"\\*", sizeof(name)/sizeof(name[0]) - wcslen(name) - 1); 1.26 +} 1.27 + 1.28 +DIR::~DIR() 1.29 +{ 1.30 + if (findHandle != INVALID_HANDLE_VALUE) { 1.31 + FindClose(findHandle); 1.32 + } 1.33 +} 1.34 + 1.35 +dirent::dirent() 1.36 +{ 1.37 + d_name[0] = L'\0'; 1.38 +} 1.39 + 1.40 +DIR* 1.41 +opendir(const WCHAR* path) 1.42 +{ 1.43 + return new DIR(path); 1.44 +} 1.45 + 1.46 +int 1.47 +closedir(DIR* dir) 1.48 +{ 1.49 + delete dir; 1.50 + return 0; 1.51 +} 1.52 + 1.53 +dirent* readdir(DIR* dir) 1.54 +{ 1.55 + WIN32_FIND_DATAW data; 1.56 + if (dir->findHandle != INVALID_HANDLE_VALUE) { 1.57 + BOOL result = FindNextFileW(dir->findHandle, &data); 1.58 + if (!result) { 1.59 + if (GetLastError() != ERROR_FILE_NOT_FOUND) { 1.60 + errno = ENOENT; 1.61 + } 1.62 + return 0; 1.63 + } 1.64 + } else { 1.65 + // Reading the first directory entry 1.66 + dir->findHandle = FindFirstFileW(dir->name, &data); 1.67 + if (dir->findHandle == INVALID_HANDLE_VALUE) { 1.68 + if (GetLastError() == ERROR_FILE_NOT_FOUND) { 1.69 + errno = ENOENT; 1.70 + } else { 1.71 + errno = EBADF; 1.72 + } 1.73 + return 0; 1.74 + } 1.75 + } 1.76 + memset(gDirEnt.d_name, 0, sizeof(gDirEnt.d_name)); 1.77 + wcsncpy(gDirEnt.d_name, data.cFileName, 1.78 + sizeof(gDirEnt.d_name)/sizeof(gDirEnt.d_name[0])); 1.79 + return &gDirEnt; 1.80 +} 1.81 +