Skip to content

Commit 6777334

Browse files
Lukas HutakLukas955
authored andcommitted
fdsdump: filelist: define component
1 parent 137add2 commit 6777334

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

src/tools/fdsdump/src/filelist.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* \file src/utils/filelist.cpp
3+
* \author Michal Sedlak <[email protected]>
4+
* \brief Thread safe file list
5+
*
6+
* Copyright (C) 2021 CESNET, z.s.p.o.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name of the Company nor the names of its contributors
18+
* may be used to endorse or promote products derived from this
19+
* software without specific prior written permission.
20+
*
21+
* ALTERNATIVELY, provided that this notice is retained in full, this
22+
* product may be distributed under the terms of the GNU General Public
23+
* License (GPL) version 2 or later, in which case the provisions
24+
* of the GPL apply INSTEAD OF those given above.
25+
*
26+
* This software is provided ``as is, and any express or implied
27+
* warranties, including, but not limited to, the implied warranties of
28+
* merchantability and fitness for a particular purpose are disclaimed.
29+
* In no event shall the company or contributors be liable for any
30+
* direct, indirect, incidental, special, exemplary, or consequential
31+
* damages (including, but not limited to, procurement of substitute
32+
* goods or services; loss of use, data, or profits; or business
33+
* interruption) however caused and on any theory of liability, whether
34+
* in contract, strict liability, or tort (including negligence or
35+
* otherwise) arising in any way out of the use of this software, even
36+
* if advised of the possibility of such damage.
37+
*
38+
*/
39+
40+
#include <memory>
41+
#include <string>
42+
43+
#include <glob.h>
44+
45+
#include "filelist.hpp"
46+
47+
void
48+
FileList::add_files(const std::string &pattern)
49+
{
50+
std::lock_guard<std::mutex> guard(m_mutex);
51+
52+
std::unique_ptr<glob_t, decltype(&globfree)> glob_ptr {nullptr, &globfree};
53+
glob_t globbuf = {};
54+
const int flags = GLOB_MARK | GLOB_BRACE | GLOB_TILDE;
55+
int ret;
56+
57+
ret = glob(pattern.c_str(), flags, NULL, &globbuf);
58+
switch (ret) {
59+
case 0:
60+
break;
61+
case GLOB_NOMATCH:
62+
return;
63+
case GLOB_NOSPACE:
64+
throw std::bad_alloc();
65+
case GLOB_ABORTED:
66+
throw std::runtime_error("glob() failed: GLOB_ABORTED");
67+
default:
68+
throw std::runtime_error("glob() failed: " + std::to_string(ret));
69+
break;
70+
}
71+
72+
glob_ptr.reset(&globbuf);
73+
74+
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
75+
std::string filename = globbuf.gl_pathv[i];
76+
77+
if (filename.back() == '/') {
78+
// Skip directories
79+
continue;
80+
}
81+
82+
m_files.push_back(std::move(filename));
83+
}
84+
}
85+
86+
bool
87+
FileList::pop(std::string &filename)
88+
{
89+
std::lock_guard<std::mutex> guard(m_mutex);
90+
91+
if (empty()) {
92+
return false;
93+
}
94+
95+
filename = std::move(*m_files.begin());
96+
m_files.erase(m_files.begin());
97+
return true;
98+
}
99+
100+
void
101+
FileList::clear()
102+
{
103+
std::lock_guard<std::mutex> guard(m_mutex);
104+
105+
m_files.clear();
106+
}

src/tools/fdsdump/src/filelist.hpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* \file
3+
* \author Michal Sedlak <[email protected]>
4+
* \brief Thread safe file list
5+
*
6+
* Copyright (C) 2021 CESNET, z.s.p.o.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name of the Company nor the names of its contributors
18+
* may be used to endorse or promote products derived from this
19+
* software without specific prior written permission.
20+
*
21+
* ALTERNATIVELY, provided that this notice is retained in full, this
22+
* product may be distributed under the terms of the GNU General Public
23+
* License (GPL) version 2 or later, in which case the provisions
24+
* of the GPL apply INSTEAD OF those given above.
25+
*
26+
* This software is provided ``as is, and any express or implied
27+
* warranties, including, but not limited to, the implied warranties of
28+
* merchantability and fitness for a particular purpose are disclaimed.
29+
* In no event shall the company or contributors be liable for any
30+
* direct, indirect, incidental, special, exemplary, or consequential
31+
* damages (including, but not limited to, procurement of substitute
32+
* goods or services; loss of use, data, or profits; or business
33+
* interruption) however caused and on any theory of liability, whether
34+
* in contract, strict liability, or tort (including negligence or
35+
* otherwise) arising in any way out of the use of this software, even
36+
* if advised of the possibility of such damage.
37+
*
38+
*/
39+
#pragma once
40+
41+
#include <string>
42+
#include <mutex>
43+
#include <vector>
44+
45+
/**
46+
* \brief A file list that allows thread safe retrieval of items;
47+
*/
48+
class FileList {
49+
public:
50+
using Iterator = std::vector<std::string>::const_iterator;
51+
52+
/**
53+
* \brief Add files matching the specified pattern onto the list.
54+
* \param[in] pattern The file path glob pattern.
55+
*/
56+
void
57+
add_files(const std::string &pattern);
58+
59+
/**
60+
* \brief A thread safe method to retrieve a filename off the list.
61+
* \param[out] filename The filename
62+
* \return true if the filename was retrieved, false if the list was empty
63+
*/
64+
bool
65+
pop(std::string &filename);
66+
67+
/**
68+
* \brief Clear all files in the list.
69+
*/
70+
void clear();
71+
72+
/**
73+
* \brief Get the length of the list
74+
* \return The length of the list
75+
*/
76+
size_t length() const { return m_files.size(); }
77+
78+
/**
79+
* \brief Check whether the list is empty
80+
* \return true or false
81+
*/
82+
bool empty() const { return m_files.empty(); }
83+
84+
/**
85+
* \brief Get an iterator to the beginning of the list
86+
* \return The constant iterator
87+
*/
88+
Iterator begin() const { return m_files.cbegin(); }
89+
90+
/**
91+
* \brief Get an iterator representing the end of the list
92+
* \return The constant iterator
93+
*/
94+
Iterator end() const { return m_files.cend(); }
95+
96+
private:
97+
std::mutex m_mutex;
98+
std::vector<std::string> m_files;
99+
};

0 commit comments

Comments
 (0)