@septag
DMon est une petite bibliothèque C qui surveille les changements dans un répertoire. Il fournit une solution unifiée à plusieurs API système qui existent pour chaque système d'exploitation. Il peut également surveiller les répertoires récursivement.
ReadDirectoryChangesW Backend. Testé avec Windows10 SDK + Visual Studio 2019inotify . Testé avec GCC-7.4 / Clang-6, Ubuntu 18.04 LTSFSEvents . Testé avec macOS-10.14 Clang 10Il vous suffit d'inclure le fichier et d'utiliser ses fonctions. Il est également compatible avec le code C ++. Les barreaux de barreaux dans les chemins Windows sont également convertis en «/» pour la portabilité.
#define DMON_IMPL
#include "dmon.h"
static void watch_callback ( dmon_watch_id watch_id , dmon_action action , const char * rootdir ,
const char * filepath , const char * oldfilepath , void * user )
{
// receive change events. type of event is stored in 'action' variable
}
int main ()
{
dmon_init ();
dmon_watch ( "/path/to/directory" , watch_callback , DMON_WATCHFLAGS_RECURSIVE , NULL );
// wait ...
dmon_deinit ();
return 0 ;
}Pour plus d'informations et comment personnaliser les fonctionnalités, voir dmon.h
Pour construire sur Linux, lien avec pthread : gcc test.c -lpthread -o test
Pour construire sur macOS, liez avec CoreServices et CoreFoundation : clang test.c -framework CoreFoundation -framework CoreServices -lpthread -o test
Il y a cet autre fichier dmon_extra.h que vous pouvez inclure éventuellement après dmon.h pour le backend Linux. Il y a deux API qui sont introduites dans cet en-tête, dmon_watch_add et dmon_watch_rm dans lesquelles vous pouvez ajouter / supprimer des sous-répertoires dans le répertoire actuellement regardé. Cela peut être utile dans les cas où il y a un grand ensemble de modifications qui se produisent sur le backend Linux et vous n'obtiendrez pas tous les événements, en raison de certains inconvénients du backend inotify . Ainsi, en désactivant DMON_WATCHFLAGS_RECURSIVE et en utilisant ces fonctions pour gérer manuellement la récursivité du répertoire, vous pouvez s’éloigner ces problèmes.
Copyright 2019 Sepehr Taghdisian. All rights reserved.
https://github.com/septag/dmon
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.