File: wildcard-file-reader.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (174 lines) | stat: -rw-r--r-- 4,856 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2018 Balabit
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "wildcard-file-reader.h"
#include "mainloop.h"

static gboolean
_init(LogPipe *s)
{
  WildcardFileReader *self = (WildcardFileReader *)s;
  self->file_state.deleted = FALSE;
  self->file_state.deleted_eof = FALSE;
  return file_reader_init_method(s);
}

static gboolean
_deinit(LogPipe *s)
{
  WildcardFileReader *self = (WildcardFileReader *)s;
  if (iv_task_registered(&self->file_state_event_handler))
    {
      iv_task_unregister(&self->file_state_event_handler);
    }
  return file_reader_deinit_method(s);
}

static void
_deleted_file_eof(FileStateEvent *self, FileReader *reader)
{
  if (self && self->deleted_file_eof)
    {
      self->deleted_file_eof(reader, self->deleted_file_eof_user_data);
    }
}

static void
_schedule_state_change_handling(WildcardFileReader *self)
{
  main_loop_assert_main_thread();
  if (!iv_task_registered(&self->file_state_event_handler))
    {
      iv_task_register(&self->file_state_event_handler);
    }
}

static void
_set_eof(WildcardFileReader *self)
{
  if (self->file_state.deleted)
    {
      self->file_state.deleted_eof = TRUE;
      _schedule_state_change_handling(self);
    }
}

static gboolean
_is_reader_poll_stopped(LogReader *reader)
{
  return (reader == NULL || FALSE == log_reader_is_opened(reader));
}

static void
_set_deleted(WildcardFileReader *self)
{
  /* File can be deleted only once,
   * so there is no need for checking the state
   * before we set it
   */
  self->file_state.deleted = TRUE;

  if (_is_reader_poll_stopped(self->super.reader))
    {
      self->file_state.deleted_eof = TRUE;
      _schedule_state_change_handling(self);
    }
}

static gboolean
_is_deleted_file_eof(WildcardFileReader *self)
{
  return (self->file_state.deleted && self->file_state.deleted_eof);
}

static gint
_notify(LogPipe *s, gint notify_code, gpointer user_data)
{
  WildcardFileReader *self = (WildcardFileReader *)s;
  gint result = NR_OK;
  switch(notify_code)
    {
    case NC_FILE_DELETED:
      _set_deleted(self);
      break;
    case NC_FILE_EOF:
      _set_eof(self);
      break;
    default:
      break;
    }
  result = file_reader_notify_method(s, notify_code, user_data);

  if (_is_deleted_file_eof(self))
    result |= NR_STOP_ON_EOF;
  return result;
}

static void
_handle_file_state_event(gpointer s)
{
  WildcardFileReader *self = (WildcardFileReader *)s;
  msg_debug("File status changed",
            evt_tag_int("EOF", self->file_state.deleted_eof),
            evt_tag_int("DELETED", self->file_state.deleted),
            evt_tag_str("Filename", self->super.filename->str));
  if (_is_deleted_file_eof(self))
    _deleted_file_eof(&self->file_state_event, &self->super);
}

static void
_on_file_moved(FileReader *self)
{
  log_pipe_notify(&self->super, NC_FILE_DELETED, self);
  log_pipe_notify(&self->super, NC_FILE_EOF, self);
}

void
wildcard_file_reader_on_deleted_file_eof(WildcardFileReader *self,
                                         FileStateEventCallback cb,
                                         gpointer user_data)
{
  self->file_state_event.deleted_file_eof = cb;
  self->file_state_event.deleted_file_eof_user_data = user_data;
}

gboolean
wildcard_file_reader_is_deleted(WildcardFileReader *self)
{
  return self->file_state.deleted;
}

WildcardFileReader *
wildcard_file_reader_new(const gchar *filename, FileReaderOptions *options, FileOpener *opener, LogSrcDriver *owner,
                         GlobalConfig *cfg)
{
  WildcardFileReader *self = g_new0(WildcardFileReader, 1);
  file_reader_init_instance(&self->super, filename, options, opener, owner, cfg, "wildcard_file_sd");
  self->super.super.init = _init;
  self->super.super.notify = _notify;
  self->super.super.deinit = _deinit;
  self->super.on_file_moved = _on_file_moved;
  IV_TASK_INIT(&self->file_state_event_handler);
  self->file_state_event_handler.cookie = self;
  self->file_state_event_handler.handler = _handle_file_state_event;
  return self;
}