summaryrefslogtreecommitdiff
path: root/libopie2/opiecore/ofilenotify.cpp
blob: 5f2a1cc17e219ed75cc37293083d811997cc0ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
                            This file is part of the Opie Project
             =.             Copyright (C) 2004 Michael 'Mickey' Lauer <mickey@Vanille.de>
           .=l.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
          .>+-=
_;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=         redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :          the terms of the GNU Library General Public
.="- .-=="i,     .._        License as published by the Free Software
- .   .-<_>     .<>         Foundation; either version 2 of the License,
    ._= =}       :          or (at your option) any later version.
   .%`+i>       _;_.
   .i_,=:_.      -<s.       This program is distributed in the hope that
    +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
   : ..    .:,     . . .    without even the implied warranty of
   =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
 _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;     Library General Public License for more
++=   -.     .`     .:      details.
:     =  ...= . :.=-
-.   .:....=;==+<;          You should have received a copy of the GNU
 -_. . .   )=.  =           Library General Public License along with
   --        :-=`           this library; see the file COPYING.LIB.
                            If not, write to the Free Software Foundation,
                            Inc., 59 Temple Place - Suite 330,
                            Boston, MA 02111-1307, USA.
*/

#include "ofilenotify.h"
using namespace Opie::Core;

/* OPIE */

/* QT */
#include <qobject.h>
#include <qsignal.h>
#include <qintdict.h>
#include <qdir.h>

/* STD */
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

static QIntDict<OFileNotification> notification_list;

namespace Opie {
namespace Core {

OFileNotification::OFileNotification( QObject* parent, const char* name )
                  :QObject( parent, name ), _active( false )
{
    qDebug( "OFileNotification::OFileNotification()" );
}


OFileNotification::~OFileNotification()
{
    qDebug( "OFileNotification::~OFileNotification()" );
}


bool OFileNotification::isActive() const
{
    return _active;
}


int OFileNotification::start( const QString& path, bool sshot, OFileNotificationType type )
{
    _path = QString::null;
    _fd = 0;
    if ( _active ) stop();
    QString dirpath;

    // check if path exists and whether it is a file or a directory, if it exists at all
    int result = ::stat( (const char*) path, &_stat );
    if ( !(type & Create) && result == -1 )
    {
        qWarning( "OFileNotification::start(): Can't stat '%s': %s.", (const char*) path, strerror( errno ) );
        return -1;
    }

    // if it is not a directory, we need to find out in which directory the file is
    bool isDirectory = S_ISDIR( _stat.st_mode );
    if ( !isDirectory )
    {
        int slashpos;
        slashpos = path.findRev( '/' );
        if ( slashpos > 0 )
        {
            _path = path;
            dirpath = path.left( slashpos );
        }
    }
    else /* isDirectory */
    {
        qWarning( "FIXME FIXME FIXME = Directory Notification Not Yet Implemented!" );
        _path = path;
        dirpath = path;
        assert( 0 );
    }

    int fd = ::open( (const char*) dirpath, O_RDONLY );
    if ( fd != -1 )
    {
        if ( notification_list.isEmpty() )
        {
            OFileNotification::registerSignalHandler();
        }

        result = ::fcntl( fd, F_SETSIG, SIGRTMIN );
        if ( result == -1 )
        {
            qWarning( "OFileNotification::start(): Can't subscribe to '%s': %s.", (const char*) dirpath, strerror( errno ) );
            return -1;
        }
        if ( !sshot ) (int) type |= (int) Multi;
        result = ::fcntl( fd, F_NOTIFY, type );
        if ( result == -1 )
        {
            qWarning( "OFileNotification::start(): Can't subscribe to '%s': %s.", (const char*) dirpath, strerror( errno ) );
            return -1;
        }
        qDebug( "OFileNotification::start(): Subscribed for changes to %s (fd = %d, mask = 0x%0x)", (const char*) dirpath, fd, type );
        notification_list.insert( fd, this );
        _type = type;
        _fd = fd;
        _active = true;
        ::memset( &_stat, 0, sizeof _stat );
        ::stat( _path, &_stat );
        return fd;
    }
    else
    {
        qWarning( "OFileNotification::start(): Error with path '%s': %s.", (const char*) dirpath, strerror( errno ) );
        return -1;
    }
}


void OFileNotification::stop()
{
    if ( !_active ) return;

    int result = ::fcntl( _fd, F_NOTIFY, 0 );
    if ( result == -1 )
    {
        qWarning( "OFileNotification::stop(): Can't remove subscription to '%s': %s.", (const char*) _path, strerror( errno ) );
    }
    else
    {
        ::close( _fd );
        _type = Single;
        _path = QString::null;
        _fd = 0;
        _active = false;
    }
}


OFileNotificationType OFileNotification::type() const
{
    return _type;
}


QString OFileNotification::path() const
{
    return _path;
}


int OFileNotification::fileno() const
{
    return _fd;
}


bool OFileNotification::activate()
{
    if ( hasChanged() )
    {
        emit triggered();
        _signal.activate();
        return true;
    }
    else
        return false;
}


bool OFileNotification::hasChanged()
{
    bool c = false;

    struct stat newstat;
    ::memset( &newstat, 0, sizeof newstat );
    int result = ::stat( _path, &newstat ); // may fail if file has been renamed or deleted. that doesn't matter :)

    qDebug( "result of newstat call is %d (%s=%d)", result, result == -1 ? strerror( errno ) : "success", errno );
    qDebug( "stat.atime = %0lx, newstat.atime = %0lx", (long)_stat.st_atime, (long)newstat.st_atime );
    qDebug( "stat.mtime = %0lx, newstat.mtime = %0lx", (long)_stat.st_mtime, (long)newstat.st_mtime );
    qDebug( "stat.ctime = %0lx, newstat.ctime = %0lx", (long)_stat.st_ctime, (long)newstat.st_ctime );

    if ( !c && (_type & Create) &&
        (long)_stat.st_atime == 0 && (long)_stat.st_mtime == 0 && (long)_stat.st_ctime == 0 &&
        (long)newstat.st_atime > 0 && (long)newstat.st_mtime > 0 && (long)newstat.st_ctime > 0)
    {
        qDebug( "OFileNotification::hasChanged(): file has been created" );
        c = true;
    }
    if ( !c && (_type & (Delete|Rename)) && (long)newstat.st_atime == 0 && (long)newstat.st_mtime == 0 && (long)newstat.st_ctime == 0)
    {
        qDebug( "OFileNotification::hasChanged(): file has been deleted or renamed" );
        c = true;
    }
    if ( !c && (_type & Access) && (long)_stat.st_atime < (long)newstat.st_atime )
    {
        qDebug( "OFileNotification::hasChanged(): atime changed" );
        c = true;
    }
    if ( !c && (_type & Modify) && (long)_stat.st_mtime < (long)newstat.st_mtime )
    {
        qDebug( "OFileNotification::hasChanged(): mtime changed" );
        c = true;
    }
    if ( !c && (_type & Attrib) && (long)_stat.st_ctime < (long)newstat.st_ctime )
    {
        qDebug( "OFileNotification::hasChanged(): ctime changed" );
        c = true;
    }

    return c;
}


void OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type )
{
    OFileNotification* ofn = new OFileNotification();
    ofn->_signal.connect( receiver, member );
    ofn->start( path, true, type );
}


void OFileNotification::__signalHandler( int sig, siginfo_t *si, void *data )
{
    qWarning( "OFileNotification::__signalHandler(): reached." );
    int fd = si->si_fd;
    OFileNotification* fn = notification_list[fd];
    if ( fn )
    {
        // check if it really was the file (dnotify triggers on directory granularity, not file granularity)
        if ( !fn->activate() )
        {
            qDebug( "OFileNotification::__signalHandler(): false alarm ;) Restarting the trigger (if it was single)..." );
            if ( !(fn->type() & Multi ) )
            {
                int result = ::fcntl( fn->fileno(), F_NOTIFY, fn->type() );
                if ( result == -1 )
                {
                    qWarning( "OFileNotification::__signalHandler(): Can't restart the trigger: %s.", strerror( errno ) );
                }
            }
            return;
        }
        #if 1
        if ( !(fn->type() & Multi) )
        {
            qDebug( "OFileNotification::__signalHandler(): '%d' was singleShot. Removing from list.", fd );
            notification_list.remove( fd );
            if ( notification_list.isEmpty() )
            {
                OFileNotification::unregisterSignalHandler();
            }
        }
        #endif
    }
    else
    {
        qWarning( "OFileNotification::__signalHandler(): D'oh! Called without fd in notification_list. Race condition?" );
    }
}


bool OFileNotification::registerSignalHandler()
{
    struct sigaction act;
    act.sa_sigaction = OFileNotification::__signalHandler;
    ::sigemptyset( &act.sa_mask );
    act.sa_flags = SA_SIGINFO;
    if ( ::sigaction( SIGRTMIN, &act, NULL ) == -1 )
    {
        qWarning( "OFileNotification::registerSignalHandler(): couldn't register signal handler: %s", strerror( errno ) );
        return false;
    }
    qDebug( "OFileNotification::registerSignalHandler(): done" );
}


void OFileNotification::unregisterSignalHandler()
{
    struct sigaction act;
    act.sa_sigaction = ( void (*)(int, siginfo_t*, void*) ) SIG_DFL;
    ::sigemptyset( &act.sa_mask );
    if ( ::sigaction( SIGRTMIN, &act, NULL ) == -1 )
    {
        qWarning( "OFileNotification::unregisterSignalHandler(): couldn't deregister signal handler: %s", strerror( errno ) );
    }
    qDebug( "OFileNotification::unregisterSignalHandler(): done" );
}


}
}