michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "win_dirent.h" michael@0: #include michael@0: #include michael@0: michael@0: // This file implements the minimum set of dirent APIs used by updater.cpp on michael@0: // Windows. If updater.cpp is modified to use more of this API, we need to michael@0: // implement those parts here too. michael@0: michael@0: static dirent gDirEnt; michael@0: michael@0: DIR::DIR(const WCHAR* path) michael@0: : findHandle(INVALID_HANDLE_VALUE) michael@0: { michael@0: memset(name, 0, sizeof(name)); michael@0: wcsncpy(name, path, sizeof(name)/sizeof(name[0])); michael@0: wcsncat(name, L"\\*", sizeof(name)/sizeof(name[0]) - wcslen(name) - 1); michael@0: } michael@0: michael@0: DIR::~DIR() michael@0: { michael@0: if (findHandle != INVALID_HANDLE_VALUE) { michael@0: FindClose(findHandle); michael@0: } michael@0: } michael@0: michael@0: dirent::dirent() michael@0: { michael@0: d_name[0] = L'\0'; michael@0: } michael@0: michael@0: DIR* michael@0: opendir(const WCHAR* path) michael@0: { michael@0: return new DIR(path); michael@0: } michael@0: michael@0: int michael@0: closedir(DIR* dir) michael@0: { michael@0: delete dir; michael@0: return 0; michael@0: } michael@0: michael@0: dirent* readdir(DIR* dir) michael@0: { michael@0: WIN32_FIND_DATAW data; michael@0: if (dir->findHandle != INVALID_HANDLE_VALUE) { michael@0: BOOL result = FindNextFileW(dir->findHandle, &data); michael@0: if (!result) { michael@0: if (GetLastError() != ERROR_FILE_NOT_FOUND) { michael@0: errno = ENOENT; michael@0: } michael@0: return 0; michael@0: } michael@0: } else { michael@0: // Reading the first directory entry michael@0: dir->findHandle = FindFirstFileW(dir->name, &data); michael@0: if (dir->findHandle == INVALID_HANDLE_VALUE) { michael@0: if (GetLastError() == ERROR_FILE_NOT_FOUND) { michael@0: errno = ENOENT; michael@0: } else { michael@0: errno = EBADF; michael@0: } michael@0: return 0; michael@0: } michael@0: } michael@0: memset(gDirEnt.d_name, 0, sizeof(gDirEnt.d_name)); michael@0: wcsncpy(gDirEnt.d_name, data.cFileName, michael@0: sizeof(gDirEnt.d_name)/sizeof(gDirEnt.d_name[0])); michael@0: return &gDirEnt; michael@0: } michael@0: