refactor: replace tr_ptrArray with std::unordered_set in tr_watchdir_scan() (#1854)

* replace tr_ptrArray with std:: in tr_watchdir_scan
This commit is contained in:
Charles Kerr 2021-09-29 12:25:52 -05:00 committed by GitHub
parent 055a8ecb4d
commit e03bc8e5bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 26 deletions

View File

@ -12,14 +12,15 @@
#error only the libtransmission watchdir module should #include this header.
#endif
struct tr_ptrArray;
#include <string>
#include <unordered_set>
typedef struct tr_watchdir_backend
{
void (*free_func)(struct tr_watchdir_backend*);
} tr_watchdir_backend;
#define BACKEND_DOWNCAST(b) ((tr_watchdir_backend*)(b))
#define BACKEND_DOWNCAST(b) (reinterpret_cast<tr_watchdir_backend*>(b))
/* ... */
@ -31,7 +32,7 @@ struct event_base* tr_watchdir_get_event_base(tr_watchdir_t handle);
void tr_watchdir_process(tr_watchdir_t handle, char const* name);
void tr_watchdir_scan(tr_watchdir_t handle, struct tr_ptrArray* dir_entries);
void tr_watchdir_scan(tr_watchdir_t handle, std::unordered_set<std::string>* dir_entries);
/* ... */

View File

@ -7,6 +7,8 @@
*/
#include <errno.h>
#include <string>
#include <unordered_set>
#include <event2/event.h>
@ -14,7 +16,6 @@
#include "transmission.h"
#include "log.h"
#include "ptrarray.h"
#include "tr-assert.h"
#include "utils.h"
#include "watchdir.h"
@ -37,10 +38,10 @@ typedef struct tr_watchdir_generic
tr_watchdir_backend base;
struct event* event;
tr_ptrArray dir_entries;
std::unordered_set<std::string> dir_entries;
} tr_watchdir_generic;
#define BACKEND_UPCAST(b) ((tr_watchdir_generic*)(b))
#define BACKEND_UPCAST(b) (reinterpret_cast<tr_watchdir_generic*>(b))
/* Non-static and mutable for unit tests. default to 10 sec. */
auto tr_watchdir_generic_interval = timeval{ 10, 0 };
@ -77,16 +78,12 @@ static void tr_watchdir_generic_free(tr_watchdir_backend* backend_base)
event_free(backend->event);
}
tr_ptrArrayDestruct(&backend->dir_entries, &tr_free);
tr_free(backend);
delete backend;
}
tr_watchdir_backend* tr_watchdir_generic_new(tr_watchdir_t handle)
{
tr_watchdir_generic* backend;
backend = tr_new0(tr_watchdir_generic, 1);
auto* backend = new tr_watchdir_generic{};
backend->base.free_func = &tr_watchdir_generic_free;
if ((backend

View File

@ -8,6 +8,8 @@
#include <errno.h>
#include <string.h> /* strcmp() */
#include <string>
#include <unordered_set>
#include <fcntl.h> /* open() */
#include <unistd.h> /* close() */
@ -25,7 +27,6 @@
#include "transmission.h"
#include "log.h"
#include "ptrarray.h"
#include "tr-assert.h"
#include "utils.h"
#include "watchdir.h"
@ -50,10 +51,10 @@ typedef struct tr_watchdir_kqueue
int kq;
int dirfd;
struct event* event;
tr_ptrArray dir_entries;
std::unordered_set<std::string> dir_entries;
} tr_watchdir_kqueue;
#define BACKEND_UPCAST(b) ((tr_watchdir_kqueue*)(b))
#define BACKEND_UPCAST(b) (reinterpret_cast<tr_watchdir_kqueue*>(b))
#define KQUEUE_WATCH_MASK (NOTE_WRITE | NOTE_EXTEND)
@ -108,18 +109,15 @@ static void tr_watchdir_kqueue_free(tr_watchdir_backend* backend_base)
close(backend->dirfd);
}
tr_ptrArrayDestruct(&backend->dir_entries, &tr_free);
tr_free(backend);
delete backend;
}
tr_watchdir_backend* tr_watchdir_kqueue_new(tr_watchdir_t handle)
{
char const* const path = tr_watchdir_get_path(handle);
struct kevent ke;
tr_watchdir_kqueue* backend;
backend = tr_new0(tr_watchdir_kqueue, 1);
auto* backend = new tr_watchdir_kqueue{};
backend->base.free_func = &tr_watchdir_kqueue_free;
backend->kq = -1;
backend->dirfd = -1;

View File

@ -328,12 +328,11 @@ void tr_watchdir_process(tr_watchdir_t handle, char const* name)
}
}
void tr_watchdir_scan(tr_watchdir_t handle, tr_ptrArray* dir_entries)
void tr_watchdir_scan(tr_watchdir_t handle, std::unordered_set<std::string>* dir_entries)
{
tr_sys_dir_t dir;
char const* name;
auto new_dir_entries = tr_ptrArray{};
auto const name_compare_func = (PtrArrayCompareFunc)&strcmp;
auto new_dir_entries = std::unordered_set<std::string>{};
tr_error* error = nullptr;
if ((dir = tr_sys_dir_open(handle->path, &error)) == TR_BAD_SYS_DIR)
@ -352,9 +351,10 @@ void tr_watchdir_scan(tr_watchdir_t handle, tr_ptrArray* dir_entries)
if (dir_entries != nullptr)
{
tr_ptrArrayInsertSorted(&new_dir_entries, tr_strdup(name), name_compare_func);
auto const namestr = std::string(name);
new_dir_entries.insert(namestr);
if (tr_ptrArrayFindSorted(dir_entries, name, name_compare_func) != nullptr)
if (dir_entries->count(namestr) != 0)
{
continue;
}
@ -373,7 +373,6 @@ void tr_watchdir_scan(tr_watchdir_t handle, tr_ptrArray* dir_entries)
if (dir_entries != nullptr)
{
tr_ptrArrayDestruct(dir_entries, &tr_free);
*dir_entries = new_dir_entries;
}
}

View File

@ -360,6 +360,8 @@ TEST_P(WatchDirTest, retry)
processEvents();
EXPECT_EQ(wd, wd_data.wd);
EXPECT_EQ(test_file, wd_data.name);
tr_watchdir_free(wd);
}
INSTANTIATE_TEST_SUITE_P( //