2020-11-07 09:40:53 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2021-02-08 20:40:58 +01:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2020-11-07 09:40:53 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-07 09:40:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <AK/Noncopyable.h>
|
2021-02-08 20:40:58 +01:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
|
#include <AK/Result.h>
|
2020-11-07 09:40:53 +02:00
|
|
|
#include <AK/String.h>
|
2021-02-08 20:40:58 +01:00
|
|
|
#include <LibCore/Notifier.h>
|
2020-11-07 09:40:53 +02:00
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
2021-02-08 20:40:58 +01:00
|
|
|
struct FileWatcherEvent {
|
|
|
|
|
enum class Type {
|
|
|
|
|
Modified,
|
|
|
|
|
ChildAdded,
|
|
|
|
|
ChildRemoved,
|
|
|
|
|
};
|
|
|
|
|
Type type;
|
|
|
|
|
String child_path;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BlockingFileWatcher {
|
|
|
|
|
AK_MAKE_NONCOPYABLE(BlockingFileWatcher);
|
2020-11-07 09:40:53 +02:00
|
|
|
|
|
|
|
|
public:
|
2021-02-08 20:40:58 +01:00
|
|
|
explicit BlockingFileWatcher(const String& path);
|
|
|
|
|
~BlockingFileWatcher();
|
2020-11-07 09:40:53 +02:00
|
|
|
|
2021-02-08 20:40:58 +01:00
|
|
|
Optional<FileWatcherEvent> wait_for_event();
|
2020-11-07 09:40:53 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
String m_path;
|
|
|
|
|
int m_watcher_fd { -1 };
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-08 20:40:58 +01:00
|
|
|
class FileWatcher : public RefCounted<FileWatcher> {
|
|
|
|
|
AK_MAKE_NONCOPYABLE(FileWatcher);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static Result<NonnullRefPtr<FileWatcher>, String> watch(const String& path);
|
|
|
|
|
~FileWatcher();
|
|
|
|
|
|
|
|
|
|
Function<void(FileWatcherEvent)> on_change;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
FileWatcher(NonnullRefPtr<Notifier>, const String& path);
|
|
|
|
|
|
|
|
|
|
NonnullRefPtr<Notifier> m_notifier;
|
|
|
|
|
String m_path;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-07 09:40:53 +02:00
|
|
|
}
|