DeviceLister.cpp 3.6 KB
Newer Older
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
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "DeviceLister.h"
28
29
30

#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)

31
32
#include "logging/Logger.h"
#include "utils/Charsets.h"
33
#include "utils/Filename.h"
34

35
#include <memory>
36
#include <sstream>
37

38
39
40
41
42
43
44
45
46
#include <windows.h>

namespace medialibrary
{
namespace fs
{

std::vector<std::tuple<std::string, std::string, bool>> DeviceLister::devices() const
{
47
    wchar_t volumeName[MAX_PATH];
Steve Lhomme's avatar
Steve Lhomme committed
48
    auto handle = FindFirstVolume( volumeName, sizeof(volumeName)/sizeof(volumeName[0]) );
49
50
51
52
53
54
    if ( handle == INVALID_HANDLE_VALUE )
    {
        std::stringstream ss;
        ss << "Failed to list devices (error code" << GetLastError() << ')';
        throw std::runtime_error( ss.str() );
    }
55
56
    std::unique_ptr<typename std::remove_pointer<HANDLE>::type, decltype(&FindVolumeClose)>
            uh( handle, &FindVolumeClose );
57
    std::vector<std::tuple<std::string, std::string, bool>> res;
Steve Lhomme's avatar
Steve Lhomme committed
58
    for ( BOOL success =  TRUE; ; success = FindNextVolume( handle, volumeName, sizeof( volumeName )/sizeof(volumeName[0]) ) )
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    {
        if ( success == FALSE )
        {
            auto err = GetLastError();
            if ( err == ERROR_NO_MORE_FILES )
                break;
            std::stringstream ss;
            ss << "Failed to list devices (error code" << err << ')';
            throw std::runtime_error( ss.str() );
        }

        auto lastChar = wcslen( volumeName ) - 1;
        if ( volumeName[0] != L'\\' || volumeName[1] != L'\\' || volumeName[2] != L'?' ||
             volumeName[3] != L'\\' || volumeName[lastChar] != L'\\' )
            continue;

75
        wchar_t buffer[MAX_PATH + 1];
76
        DWORD buffLength = sizeof( buffer ) / sizeof( wchar_t );
77
78
79

        if ( GetVolumePathNamesForVolumeName( volumeName, buffer, buffLength, &buffLength ) == 0 )
            continue;
80
        std::string mountpoint = charset::FromWide( buffer ).get();
81
82
83
84
85
86
87
88
89
90
91

        // Filter out anything which isn't a removable or fixed drive. We don't care about network
        // drive here.
        auto type = GetDriveType( buffer );
        if ( type != DRIVE_REMOVABLE && type != DRIVE_FIXED )
            continue;

        std::string uuid =  charset::FromWide( volumeName ).get();

        LOG_INFO( "Discovered device ", uuid, "; mounted on ", mountpoint, "; removable: ",
                  type == DRIVE_REMOVABLE ? "yes" : "no" );
92
93
        res.emplace_back( std::make_tuple( uuid, utils::file::toMrl( mountpoint ),
                                           type == DRIVE_REMOVABLE ) );
94
95
96
97
98
99
100
    }
    return res;
}

}
}

101
#endif