LCOV - code coverage report
Current view: top level - eventdispatcher - file_changed.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 2 0.0 %
Date: 2019-08-10 01:48:51 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2012-2019  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/eventdispatcher
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License
      17             : // along with this program; if not, write to the Free Software
      18             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      19             : #pragma once
      20             : 
      21             : /** \file
      22             :  * \brief Event dispatch class.
      23             :  *
      24             :  * Class used to handle events.
      25             :  */
      26             : 
      27             : // self
      28             : //
      29             : #include    "eventdispatcher/connection.h"
      30             : 
      31             : 
      32             : // C++ lib
      33             : //
      34             : #include    <map>
      35             : 
      36             : 
      37             : 
      38             : namespace ed
      39             : {
      40             : 
      41             : 
      42             : 
      43             : class file_changed
      44             :     : public connection
      45             : {
      46             : public:
      47             :     typedef std::shared_ptr<file_changed>           pointer_t;
      48             :     typedef std::uint32_t                           event_mask_t;
      49             : 
      50             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_NO_EVENTS  = 0x0000;
      51             : 
      52             :     // bits added to watch_...() functions
      53             :     //
      54             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_ATTRIBUTES = 0x0001;   // chmod, chown (timestamp, link count, user/group, etc.)
      55             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_READ       = 0x0002;   // read, execve
      56             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_WRITE      = 0x0004;   // write, truncate
      57             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_CREATED    = 0x0008;   // open & O_CREAT, rename, mkdir, link, symlink, bind
      58             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_DELETED    = 0x0010;   // unlink, rename
      59             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_ACCESS     = 0x0020;   // open, close
      60             : 
      61             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_IO         = SNAP_FILE_CHANGED_EVENT_READ
      62             :                                                                    | SNAP_FILE_CHANGED_EVENT_WRITE;
      63             : 
      64             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_ALL        = SNAP_FILE_CHANGED_EVENT_ATTRIBUTES
      65             :                                                                    | SNAP_FILE_CHANGED_EVENT_IO
      66             :                                                                    | SNAP_FILE_CHANGED_EVENT_CREATED
      67             :                                                                    | SNAP_FILE_CHANGED_EVENT_DELETED
      68             :                                                                    | SNAP_FILE_CHANGED_EVENT_ACCESS;
      69             : 
      70             :     // flags added in event_t objects
      71             :     //
      72             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_DIRECTORY  = 0x1000;   // object is a directory
      73             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_GONE       = 0x2000;   // removed
      74             :     static event_mask_t const   SNAP_FILE_CHANGED_EVENT_UNMOUNTED  = 0x4000;   // unmounted
      75             : 
      76           0 :     class event_t
      77             :     {
      78             :     public:
      79             :                                 event_t(std::string const & watched_path
      80             :                                       , event_mask_t events
      81             :                                       , std::string const & filename);
      82             : 
      83             :         std::string const &     get_watched_path() const;
      84             :         event_mask_t            get_events() const;
      85             :         std::string const &     get_filename() const;
      86             : 
      87             :         bool                    operator < (event_t const & rhs) const;
      88             : 
      89             :     private:
      90             :         std::string             f_watched_path = std::string();
      91             :         event_mask_t            f_events       = 0;
      92             :         std::string             f_filename     = std::string();
      93             :     };
      94             : 
      95             :                                 file_changed();
      96             :     virtual                     ~file_changed() override;
      97             : 
      98             :     void                        watch_file(std::string const & watched_path, event_mask_t const events);
      99             :     void                        watch_symlink(std::string const & watched_path, event_mask_t const events);
     100             :     void                        watch_directory(std::string const & watched_path, event_mask_t const events);
     101             : 
     102             :     void                        stop_watch(std::string const & watched_path);
     103             : 
     104             :     // snap_connection implementation
     105             :     virtual bool                is_reader() const override;
     106             :     virtual int                 get_socket() const override;
     107             :     virtual void                set_enable(bool enabled);
     108             :     virtual void                process_read() override;
     109             : 
     110             :     // new callback
     111             :     virtual void                process_event(event_t const & watch_event) = 0;
     112             : 
     113             : private:
     114             :     // TODO: RAII would be great with an impl and a counter...
     115             :     //       (i.e. we make copies at the moment.)
     116           0 :     struct watch_t
     117             :     {
     118             :         typedef std::map<int, watch_t>          map_t;
     119             : 
     120             :                                 watch_t();
     121             :                                 watch_t(std::string const & watched_path, event_mask_t events, uint32_t add_flags);
     122             : 
     123             :         void                    add_watch(int inotify);
     124             :         void                    merge_watch(int inotify, event_mask_t const events);
     125             :         void                    remove_watch(int inotify);
     126             : 
     127             :         std::string             f_watched_path = std::string();
     128             :         event_mask_t            f_events       = SNAP_FILE_CHANGED_EVENT_NO_EVENTS;
     129             :         uint32_t                f_mask         = 0;
     130             :         int                     f_watch        = -1;
     131             :     };
     132             : 
     133             :     bool                        merge_watch(std::string const & watched_path, event_mask_t const events);
     134             :     static uint32_t             events_to_mask(event_mask_t const events);
     135             :     static event_mask_t         mask_to_events(uint32_t const mask);
     136             : 
     137             :     int                         f_inotify = -1;
     138             :     watch_t::map_t              f_watches = watch_t::map_t();
     139             : };
     140             : 
     141             : 
     142             : 
     143             : } // namespace ed
     144             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12