File: Filesystem.cpp

package info (click to toggle)
libpwiz 3.0.18342-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,888 kB
  • sloc: cpp: 157,552; sh: 4,182; makefile: 317
file content (672 lines) | stat: -rw-r--r-- 22,410 bytes parent folder | download | duplicates (3)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//
// $Id$
//
//
// Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
//
// Copyright 2008 Spielberg Family Center for Applied Proteomics
//   Cedars Sinai Medical Center, Los Angeles, California  90048
// Copyright 2008 Vanderbilt University - Nashville, TN 37232
//
// 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.
//

#define PWIZ_SOURCE

#include "Filesystem.hpp"

#ifdef WIN32
    #define _WIN32_WINNT 0x0600
    #define WIN32_LEAN_AND_MEAN
    #define NOGDI
    #include <windows.h>
    #include <direct.h>
    #include <wincrypt.h>
    #include <winternl.h>
    #include <Psapi.h>
    #include <boost/nowide/convert.hpp>
    #include <boost/noncopyable.hpp>
#else
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/ioctl.h>
    #include <glob.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <errno.h>
    #ifndef MAX_PATH
        #define MAX_PATH 255
    #endif
#endif

#include <boost/utility/singleton.hpp>
#include "pwiz/utility/misc/random_access_compressed_ifstream.hpp"
#include <boost/filesystem/detail/utf8_codecvt_facet.hpp>
#include <boost/locale/conversion.hpp>
//#include <boost/xpressive/xpressive.hpp>
#include <iostream>

using std::string;
using std::vector;
using std::runtime_error;


namespace {

class UTF8_BoostFilesystemPathImbuer : public boost::singleton<UTF8_BoostFilesystemPathImbuer>
{
    public:
    UTF8_BoostFilesystemPathImbuer(boost::restricted)
    {
        std::locale global_loc = std::locale();
        std::locale loc(global_loc, new boost::filesystem::detail::utf8_codecvt_facet);
        bfs::path::imbue(loc);
    }

    void imbue() const {};
};

#ifdef WIN32
#define STATUS_INFO_LENGTH_MISMATCH 0xc0000004

#define SystemHandleInformation 16

extern "C"
{
    typedef NTSTATUS(NTAPI *_NtQuerySystemInformation)(
        ULONG SystemInformationClass,
        PVOID SystemInformation,
        ULONG SystemInformationLength,
        PULONG ReturnLength
        );

    struct SYSTEM_HANDLE {
        ULONG ProcessID;
        BYTE HandleType;
        BYTE Flags;
        USHORT Handle;
        PVOID Object;
        ACCESS_MASK GrantedAccess;
    };

    struct SYSTEM_HANDLE_INFORMATION {
        ULONG HandleCount;
        SYSTEM_HANDLE Handles[1];
    };

    enum SECTION_INHERIT
    {
        ViewShare = 1,
        ViewUnmap = 2
    };

    struct SYSTEM_HANDLE_TABLE_ENTRY_INFO
    {
        USHORT UniqueProcessId;
        USHORT CreatorBackTraceIndex;
        UCHAR ObjectTypeIndex;
        UCHAR HandleAttributes;
        USHORT HandleValue;
        PVOID Object;
        ULONG GrantedAccess;
    };


    typedef NTSTATUS(NTAPI *_NtUnmapViewOfSection)(
        HANDLE ProcessHandle,
        PVOID  BaseAddress
        );
    typedef NTSTATUS(NTAPI *_NtMapViewOfSection)(
        HANDLE          SectionHandle,
        HANDLE          ProcessHandle,
        PVOID           *BaseAddress,
        ULONG_PTR       ZeroBits,
        SIZE_T          CommitSize,
        PLARGE_INTEGER  SectionOffset,
        PSIZE_T         ViewSize,
        SECTION_INHERIT InheritDisposition,
        ULONG           AllocationType,
        ULONG           Win32Protect
        );

    typedef NTSTATUS(NTAPI *_NtQueryObject)(
        HANDLE Handle,
        OBJECT_INFORMATION_CLASS ObjectInformationClass,
        PVOID ObjectInformation,
        ULONG ObjectInformationLength,
        PULONG ReturnLength
        );

    PVOID GetLibraryProcAddress(PSTR LibraryName, PSTR ProcName) {
        return GetProcAddress(GetModuleHandleA(LibraryName), ProcName);
    }
}

    int GetFileHandleTypeNumber(SYSTEM_HANDLE_INFORMATION* handleInfos)
    {
        DWORD currentProcessId = GetCurrentProcessId();
        wstring fileType = L"File";
        std::vector<BYTE> typeInfoBytes(sizeof(PUBLIC_OBJECT_TYPE_INFORMATION));

        _NtQueryObject NtQueryObject = (_NtQueryObject)GetLibraryProcAddress("ntdll.dll", "NtQueryObject");
        if (NtQueryObject == nullptr)
        {
            fprintf(stderr, "[force_close_handles_to_filepath()] Error getting NtQueryObject function.\n");
            return 0;
        }

        map<int, int> handlesPerType;
        for (size_t i = 0; i < handleInfos->HandleCount; ++i)
        {
            if (handleInfos->Handles[i].ProcessID != currentProcessId)
                continue;

            if (handleInfos->Handles[i].HandleType < 20) // this is not the File string you're looking for
                continue;

            const auto handle = reinterpret_cast<HANDLE>(handleInfos->Handles[i].Handle);
            ULONG size;
            auto queryResult = NtQueryObject(handle, ObjectTypeInformation, typeInfoBytes.data(), typeInfoBytes.size(), &size);
            if (queryResult == STATUS_INFO_LENGTH_MISMATCH)
            {
                typeInfoBytes.resize(size);
                queryResult = NtQueryObject(handle, ObjectTypeInformation, typeInfoBytes.data(), size, nullptr);
            }

            if (NT_SUCCESS(queryResult))
            {
                const auto typeInfo = reinterpret_cast<PUBLIC_OBJECT_TYPE_INFORMATION*>(typeInfoBytes.data());
                const auto type = std::wstring(typeInfo->TypeName.Buffer, typeInfo->TypeName.Length / sizeof(WCHAR));
                if (type == fileType)
                    //return handleInfos->Handles[i].HandleType;
                    ++handlesPerType[handleInfos->Handles[i].HandleType];
            }
        }

        if (handlesPerType.empty())
            return 0;

        auto typeMode = std::max_element(handlesPerType.begin(), handlesPerType.end(), [](const auto& a, const auto& b) { return a.second < b.second; });
        return typeMode->first;
    }

    bool GetFileNameFromHandle(HANDLE hFile, wchar_t* filepathBuffer, size_t bufferLength)
    {
        // Get the file size.
        DWORD dwFileSizeHi = 0;
        DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);

        // Cannot map 0-byte files
        if (dwFileSizeLo == 0 && dwFileSizeHi == 0)
            return false;

        // Create a file mapping object.
        HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 1, NULL);

        if (!hFileMap)
            return false;

        void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);

        if (!pMem)
            return false;

        if (!GetMappedFileNameW(GetCurrentProcess(), pMem, filepathBuffer, bufferLength))
            return false;

        UnmapViewOfFile(pMem);
        CloseHandle(hFileMap);

        return true;
    }
#endif
} // namespace

namespace pwiz {
namespace util {


PWIZ_API_DECL void force_close_handles_to_filepath(const std::string& filepath, bool closeMemoryMappedSections) noexcept(true)
{
#ifdef WIN32
    bool runningUnderWine = GetLibraryProcAddress("ntdll.dll", "wine_get_version") != NULL;
    if (runningUnderWine)
        return;

    _NtQuerySystemInformation NtQuerySystemInformation = (_NtQuerySystemInformation)GetLibraryProcAddress("ntdll.dll", "NtQuerySystemInformation");
    if (NtQuerySystemInformation == nullptr)
    {
        fprintf(stderr, "[force_close_handles_to_filepath()] Error getting NtQuerySystemInformation function.\n");
        return;
    }

    _NtUnmapViewOfSection NtUnmapViewOfSection = nullptr;
    _NtMapViewOfSection NtMapViewOfSection = nullptr;
    if (closeMemoryMappedSections)
    {
        NtUnmapViewOfSection = (_NtUnmapViewOfSection)GetLibraryProcAddress("ntdll.dll", "NtUnmapViewOfSection");
        if (NtUnmapViewOfSection == nullptr)
        {
            fprintf(stderr, "[force_close_handles_to_filepath()] Error getting NtUnmapViewOfSection function.\n");
            return;
        }

        NtMapViewOfSection = (_NtMapViewOfSection)GetLibraryProcAddress("ntdll.dll", "NtMapViewOfSection");
        if (NtMapViewOfSection == nullptr)
        {
            fprintf(stderr, "[force_close_handles_to_filepath()] Error getting NtMapViewOfSection function.\n");
            return;
        }
    }

    NTSTATUS status = 0;
    DWORD dwSize = sizeof(SYSTEM_HANDLE_INFORMATION);
    vector<BYTE> pInfoBytes(dwSize);

    do
    {
        // keep reallocing until buffer is big enough
        DWORD newSize = 0;
        status = NtQuerySystemInformation(SystemHandleInformation, pInfoBytes.data(), dwSize, &newSize);
        if (status == STATUS_INFO_LENGTH_MISMATCH)
        {
            if (newSize > 0)
                dwSize = newSize;
            else
                dwSize *= 2;
            pInfoBytes.resize(dwSize);
        }
    } while (status == STATUS_INFO_LENGTH_MISMATCH);

    if (status != 0)
    {
        char messageBuffer[256];
        memset(messageBuffer, 0, 256);
        FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 255, NULL);

        fprintf(stderr, "[force_close_handles_to_filepath()] Error calling NtQuerySystemInformation function: %s\n", messageBuffer);
        return;
    }

    auto pInfo = reinterpret_cast<SYSTEM_HANDLE_INFORMATION*>(pInfoBytes.data());
    int fileHandleType = GetFileHandleTypeNumber(pInfo);
    if (fileHandleType == 0)
    {
        fprintf(stderr, "[force_close_handles_to_filepath()] Unable to determine file handle type number.\n");
        return;
    }

    auto currentProcessId = GetCurrentProcessId();
    HANDLE currentProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, currentProcessId);

    SYSTEM_INFO si;
    GetSystemInfo(&si);
    MEMORY_BASIC_INFORMATION mbi;
    PCHAR lpMem = 0;
    vector<wchar_t> mappedFilename(260);
    string narrowFilename = bfs::path(filepath).filename().string();
    vector<wchar_t> wideFilename(narrowFilename.length());
    std::use_facet<std::ctype<wchar_t>>(std::locale()).widen(narrowFilename.c_str(), narrowFilename.c_str() + narrowFilename.length(), &wideFilename[0]);
    wstring wideFilepathWithoutRoot = bfs::path(filepath).relative_path().wstring();

    if (closeMemoryMappedSections)
    {
        bool closedMappedSection = false;
        while (lpMem < si.lpMaximumApplicationAddress)
        {
            VirtualQueryEx(currentProcessHandle, lpMem, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
            lpMem += mbi.RegionSize;

            if (mbi.Type != MEM_MAPPED || mbi.State != MEM_COMMIT || mbi.Protect == PAGE_NOACCESS || mbi.RegionSize <= 4096)
                continue;

            if (GetMappedFileNameW(currentProcessHandle, mbi.BaseAddress, &mappedFilename[0], 260) == 0)
                continue;

            if (!bal::iends_with(wstring(mappedFilename.data()), wideFilename))
                continue;

            if (!UnmapViewOfFile(mbi.BaseAddress))
            {
                fprintf(stderr, "[force_close_handles_to_filepath()] Error calling UnmapViewOfFile.\n");
                return;
            }
            else
            {
                //fprintf(stderr, "[force_close_handles_to_filepath()] Closed memory mapped section.\n");
                closedMappedSection = true;
            }
        }

        if (!closedMappedSection)
            fprintf(stderr, "[force_close_handles_to_filepath()] Failed to find memory mapped section.\n");
    }

    wchar_t szPath[260];

    // iterate over every handle and close file handles that match the filepath
    for (DWORD i = 0; i < pInfo->HandleCount; i++)
    {
        auto& handleInfo = pInfo->Handles[i];
        if (handleInfo.ProcessID != currentProcessId)
            continue;

        if (handleInfo.HandleType == fileHandleType)
        {
            szPath[0] = '\0';
            if (!GetFileNameFromHandle((HANDLE)handleInfo.Handle, szPath, 260))
            {
                /*char messageBuffer[256];
                memset(messageBuffer, 0, 256);
                FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 255, NULL);

                fprintf(stderr, "[force_close_handles_to_filepath()] Error calling GetFileNameFromHandle: %s\n", messageBuffer);*/
                continue;
            }

            wchar_t* handlePath = szPath;
            if (bal::iends_with(handlePath, wideFilepathWithoutRoot.c_str()))
            {
                if (!CloseHandle((HANDLE)handleInfo.Handle))
                    fprintf(stderr, "[force_close_handles_to_filepath()] Error closing file handle.\n");
                //else
                //    fprintf(stderr, "[force_close_handles_to_filepath()] Closed file handle: " + gcnew String(handlePath));
            }
        }
        else if (closeMemoryMappedSections)
        {
            // close handle to memory mapped section
            SIZE_T viewSize = 1;
            PVOID viewBase = NULL;

            NTSTATUS status = NtMapViewOfSection((HANDLE)handleInfo.Handle, currentProcessHandle, &viewBase, 0, 0, NULL, &viewSize, ViewShare, 0, PAGE_READONLY);

            if (!NT_SUCCESS(status))
                continue;

            vector<wchar_t> mappedFilename(260);
            auto result = GetMappedFileNameW(currentProcessHandle, viewBase, &mappedFilename[0], 260);

            NtUnmapViewOfSection(currentProcessHandle, viewBase);

            if (result == 0)
                continue;

            if (!bal::iends_with(wstring(mappedFilename.data()), wideFilename))
                continue;

            if (!CloseHandle((HANDLE)handleInfo.Handle))
                fprintf(stderr, "[force_close_handles_to_filepath()] Error closing section handle.\n");
            //else
            //    fprintf(stderr, "[force_close_handles_to_filepath()] Closed section handle.\n");
        }
    }
#endif
}


PWIZ_API_DECL int expand_pathmask(const bfs::path& pathmask,
                                  vector<bfs::path>& matchingPaths)
{
    UTF8_BoostFilesystemPathImbuer::instance->imbue();

    using bfs::path;
    int matchingPathCount = 0;

#ifdef WIN32
    path maskParentPath = pathmask.branch_path();
	WIN32_FIND_DATAW fdata;
	HANDLE srcFile = FindFirstFileExW(boost::nowide::widen(pathmask.string()).c_str(), FindExInfoStandard, &fdata, FindExSearchNameMatch, NULL, 0);
	if (srcFile == INVALID_HANDLE_VALUE)
		return 0; // no matches

    do
    {
        if (!bal::equals(fdata.cFileName, L".") &&
            !bal::equals(fdata.cFileName, L"..") != 0)
        {
	        matchingPaths.push_back( maskParentPath / fdata.cFileName );
            ++matchingPathCount;
        }
    }
    while (FindNextFileW(srcFile, &fdata));

	FindClose(srcFile);

#else

	glob_t globbuf;
	int rv = glob(pathmask.string().c_str(), 0, NULL, &globbuf);
	if(rv > 0 && rv != GLOB_NOMATCH)
		throw runtime_error("FindFilesByMask(): glob() error");

	DIR* curDir = opendir(".");
	struct stat curEntryData;

	for (size_t i=0; i < globbuf.gl_pathc; ++i)
	{
		stat(globbuf.gl_pathv[i], &curEntryData);
		if (S_ISDIR(curEntryData.st_mode) ||
            S_ISREG(curEntryData.st_mode) ||
            S_ISLNK(curEntryData.st_mode))
        {
			matchingPaths.push_back(globbuf.gl_pathv[i]);
            ++matchingPathCount;
        }
	}
	closedir(curDir);

	globfree(&globbuf);

#endif

    return matchingPathCount;
}


namespace
{
    void copy_recursive(const bfs::path& from, const bfs::path& to)
    {
        bfs::copy_directory(from, to);

        for(bfs::directory_entry& entry : bfs::directory_iterator(from))
        {
            bfs::file_status status = entry.status();
            if (status.type() == bfs::directory_file)
                copy_recursive(entry.path(), to / entry.path().filename());
            else if (status.type() == bfs::regular_file)
                bfs::copy_file(entry.path(), to / entry.path().filename());
            else
                throw bfs::filesystem_error("[copy_directory] invalid path type", entry.path(), boost::system::error_code(boost::system::errc::no_such_file_or_directory, boost::system::system_category()));
        }
    }

    void copy_recursive(const bfs::path& from, const bfs::path& to, boost::system::error_code& ec)
    {
        bfs::copy_directory(from, to, ec);
        if (ec.value() != 0)
            return;

        for(bfs::directory_entry& entry : bfs::directory_iterator(from))
        {
            bfs::file_status status = entry.status(ec);
            if (status.type() == bfs::directory_file)
                copy_recursive(entry.path(), to / entry.path().filename(), ec);
            else if (status.type() == bfs::regular_file)
                bfs::copy_file(entry.path(), to / entry.path().filename(), ec);
            else if (ec.value() != 0)
                ec.assign(boost::system::errc::no_such_file_or_directory, boost::system::system_category());
        }
    }
}

PWIZ_API_DECL void copy_directory(const bfs::path& from, const bfs::path& to, bool recursive, boost::system::error_code* ec)
{
    if (!bfs::is_directory(from))
        throw bfs::filesystem_error("[copy_directory] source path is not a directory", from, boost::system::error_code(boost::system::errc::not_a_directory, boost::system::system_category()));

    if (bfs::exists(to))
    {
        if (ec != NULL)
            ec->assign(boost::system::errc::file_exists, boost::system::system_category());
        else
            throw bfs::filesystem_error("[copy_directory] target path exists", to, boost::system::error_code(boost::system::errc::file_exists, boost::system::system_category()));
    }

    if (recursive)
    {
        if (ec != NULL)
            copy_recursive(from, to, *ec);
        else
            copy_recursive(from, to);
    }
    else
    {
        if (ec != NULL)
            bfs::copy_directory(from, to, *ec);
        else
            bfs::copy_directory(from, to);
    }
}


using boost::uintmax_t;

PWIZ_API_DECL
string abbreviate_byte_size(uintmax_t byteSize, ByteSizeAbbreviation abbreviationType)
{
    uintmax_t G, M, K;
    string GS, MS, KS;

    switch (abbreviationType)
    {
        default:
        case ByteSizeAbbreviation_IEC:
            G = (M = (K = 1024) << 10) << 10;
            GS = " GiB"; MS = " MiB"; KS = " KiB";
            break;

        case ByteSizeAbbreviation_JEDEC:
            G = (M = (K = 1024) << 10) << 10;
            GS = " GB"; MS = " MB"; KS = " KB";
            break;

        case ByteSizeAbbreviation_SI:
            G = (M = (K = 1000) * 1000) * 1000;
            GS = " GB"; MS = " MB"; KS = " KB";
            break;
    }

    string suffix;

    if( byteSize >= G )
    {
        byteSize /= G;
        suffix = GS;
    } else if( byteSize >= M )
    {
        byteSize /= M;
        suffix = MS;
    } else if( byteSize >= K )
    {
        byteSize /= K;
        suffix = KS;
    } else
    {
        suffix = " B";
    }

    return lexical_cast<string>(byteSize) + suffix;
}


PWIZ_API_DECL bool isHTTP(const string& s)
{
    //using namespace boost::xpressive;

    // from URI RFC via http://stackoverflow.com/a/26766402/638445
    //sregex uriRegex = sregex::compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
    //return regex_match(s, uriRegex);

    return bal::istarts_with(s, "http://") || bal::istarts_with(s, "https://");
}


#ifdef WIN32
namespace {
    struct FileWrapper : boost::noncopyable
    {
        FileWrapper(HANDLE h) : h(h) {}
        ~FileWrapper() { CloseHandle(h); }
        bool operator== (const HANDLE& rhs) { return h==rhs; }
        private:
        HANDLE h;
    };
}
#endif


PWIZ_API_DECL string read_file_header(const string& filepath, size_t length)
{
    UTF8_BoostFilesystemPathImbuer::instance->imbue();

    string head;
    if (!bfs::is_directory(filepath) && !isHTTP(filepath))
    {
        if (!bfs::exists(filepath))
            throw runtime_error("[read_file_header()] Unable to open file " + filepath + " (file does not exist)");

#ifdef WIN32 // check for locked files which can be opened by ifstream but only produce garbage when read (at least in VC12)
        {
            std::wstring wide_filepath = boost::locale::conv::utf_to_utf<wchar_t>(filepath);
            FileWrapper handle(::CreateFileW(wide_filepath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL));
            if (handle == INVALID_HANDLE_VALUE)
                throw runtime_error("[read_file_header()] Unable to open file " + filepath + " (invalid permission or file locked)");
        }
#endif

        random_access_compressed_ifstream is(filepath.c_str());
        if (!is)
            throw runtime_error("[read_file_header()] Unable to open file " + filepath + " (" + strerror(errno) + ")");

        head.resize(length, '\0');
        if (!is.read(&head[0], (std::streamsize)head.size()) && !is.eof())
            throw runtime_error("[read_file_header()] Unable to read file " + filepath + " (" + strerror(errno) + ")");
    }
    return head;
}


PWIZ_API_DECL std::pair<int, int> get_console_bounds(const std::pair<int, int>& defaultBounds)
{
#ifdef WIN32
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    BOOL ret;
    ret = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    if (ret)
        return make_pair(csbi.dwSize.X, csbi.dwSize.Y);
    else
        return defaultBounds;
#else
    winsize max;
    if (ioctl(0, TIOCGWINSZ, &max) == 0)
        return make_pair(max.ws_col, max.ws_row);
    else
        return defaultBounds;
#endif
}


} // util
} // pwiz