clsync
indexes.h
Go to the documentation of this file.
1 /*
2  clsync - file tree sync utility based on inotify
3 
4  Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __CLSYNC_INDEXES_H
21 #define __CLSYNC_INDEXES_H
22 
23 #include <glib.h>
24 
25 #include "common.h"
26 #include "error.h"
27 #include "malloc.h"
28 
29 struct fileinfo {
31 };
32 typedef struct fileinfo fileinfo_t;
33 
34 struct indexes {
35  GHashTable *wd2fpath_ht; // watching descriptor -> file path
36  GHashTable *fpath2wd_ht; // file path -> watching descriptor
37  GHashTable *fpath2ei_ht; // file path -> event information
38  GHashTable *exc_fpath_ht; // excluded file path
39  GHashTable *exc_fpath_coll_ht[QUEUE_MAX]; // excluded file path aggregation hashtable for every queue
40  GHashTable *fpath2ei_coll_ht[QUEUE_MAX]; // "file path -> event information" aggregation hashtable for every queue
41  GHashTable *out_lines_aggr_ht; // output lines aggregation hashtable
42  GHashTable *nonthreaded_syncing_fpath2ei_ht; // events that are synchronized in signle-mode (non threaded)
43  GHashTable *fileinfo_ht; // to search "fileinfo" structures (that contains secondary sorts of things about any files/dirs)
44 #ifdef CLUSTER_SUPPORT
45  GHashTable *nodenames_ht; // node_name -> node_id
46 #endif
47 };
48 typedef struct indexes indexes_t;
49 
50 // Removes necessary rows from hash_tables if some watching descriptor closed
51 // Return: 0 on success, non-zero on fail
52 
53 static inline int indexes_remove_bywd ( indexes_t *indexes_p, int wd )
54 {
55  int ret = 0;
56  char *fpath = g_hash_table_lookup ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ) );
57  ret |= g_hash_table_remove ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ) );
58 
59  if ( fpath == NULL ) {
60  error ( "Cannot remove from index \"fpath2wd\" by wd %i.", wd );
61  return -1;
62  }
63 
64  ret |= g_hash_table_remove ( indexes_p->fpath2wd_ht, fpath );
65  return ret;
66 }
67 
68 // Lookups file path by watching descriptor from hash_tables
69 // Return: file path on success, NULL on fail
70 
71 static inline char *indexes_wd2fpath ( indexes_t *indexes_p, int wd )
72 {
73  return g_hash_table_lookup ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ) );
74 }
75 
76 // Lookups watching descriptor by file path from hash_tables
77 // Return: watching descriptor on success, -1 on fail
78 
79 static inline int indexes_fpath2wd ( indexes_t *indexes_p, const char *fpath )
80 {
81  gpointer gint_p = g_hash_table_lookup ( indexes_p->fpath2wd_ht, fpath );
82 
83  if ( gint_p == NULL )
84  return -1;
85 
86  return GPOINTER_TO_INT ( gint_p );
87 }
88 
89 // Adds necessary rows to hash_tables if some watching descriptor opened
90 // Return: 0 on success, non-zero on fail
91 
92 static inline int indexes_add_wd ( indexes_t *indexes_p, int wd, const char *fpath_const, size_t fpathlen )
93 {
94  debug ( 4, "indexes_add_wd(indexes_p, %i, \"%s\", %i)", wd, fpath_const, fpathlen );
95  char *fpath = xmalloc ( fpathlen + 1 );
96  memcpy ( fpath, fpath_const, fpathlen + 1 );
97  g_hash_table_insert ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ), fpath );
98  g_hash_table_insert ( indexes_p->fpath2wd_ht, fpath, GINT_TO_POINTER ( wd ) );
99  return 0;
100 }
101 
102 static inline eventinfo_t *indexes_fpath2ei ( indexes_t *indexes_p, const char *fpath )
103 {
104  return ( eventinfo_t * ) g_hash_table_lookup ( indexes_p->fpath2ei_ht, fpath );
105 }
106 
107 static inline int indexes_fpath2ei_add ( indexes_t *indexes_p, char *fpath, eventinfo_t *evinfo )
108 {
109  debug ( 5, "\"%s\"", fpath );
110  g_hash_table_replace ( indexes_p->fpath2ei_ht, fpath, evinfo );
111  return 0;
112 }
113 
114 static inline int indexes_queueevent ( indexes_t *indexes_p, char *fpath, eventinfo_t *evinfo, queue_id_t queue_id )
115 {
116  g_hash_table_replace ( indexes_p->fpath2ei_coll_ht[queue_id], fpath, evinfo );
117  debug ( 3, "indexes_queueevent(indexes_p, \"%s\", evinfo, %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size ( indexes_p->fpath2ei_coll_ht[queue_id] ), queue_id );
118  return 0;
119 }
120 
121 static inline eventinfo_t *indexes_lookupinqueue ( indexes_t *indexes_p, const char *fpath, queue_id_t queue_id )
122 {
123  return ( eventinfo_t * ) g_hash_table_lookup ( indexes_p->fpath2ei_coll_ht[queue_id], fpath );
124 }
125 
126 static inline int indexes_queuelen ( indexes_t *indexes_p, queue_id_t queue_id )
127 {
128  return g_hash_table_size ( indexes_p->fpath2ei_coll_ht[queue_id] );
129 }
130 
131 static inline int indexes_removefromqueue ( indexes_t *indexes_p, char *fpath, queue_id_t queue_id )
132 {
133 // debug(3, "indexes_removefromqueue(indexes_p, \"%s\", %i).", fpath, queue_id);
134  g_hash_table_remove ( indexes_p->fpath2ei_coll_ht[queue_id], fpath );
135  debug ( 3, "indexes_removefromqueue(indexes_p, \"%s\", %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size ( indexes_p->fpath2ei_coll_ht[queue_id] ), queue_id );
136  return 0;
137 }
138 
139 static inline int indexes_addexclude ( indexes_t *indexes_p, char *fpath, eventinfo_flags_t flags, queue_id_t queue_id )
140 {
141  g_hash_table_replace ( indexes_p->exc_fpath_coll_ht[queue_id], fpath, GINT_TO_POINTER ( flags ) );
142  debug ( 3, "indexes_addexclude(indexes_p, \"%s\", %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size ( indexes_p->exc_fpath_coll_ht[queue_id] ), queue_id );
143  return 0;
144 }
145 
146 static inline int indexes_addexclude_aggr ( indexes_t *indexes_p, char *fpath, eventinfo_flags_t flags )
147 {
148  debug ( 3, "indexes_addexclude_aggr(indexes_p, \"%s\", %u).", fpath, flags );
149  gpointer flags_gp = g_hash_table_lookup ( indexes_p->exc_fpath_ht, fpath );
150 
151  if ( flags_gp != NULL )
152  flags |= GPOINTER_TO_INT ( flags_gp );
153 
154  // Removing extra flags
156  flags &= ~EVIF_CONTENTRECURSIVELY;
157 
158  g_hash_table_replace ( indexes_p->exc_fpath_ht, fpath, GINT_TO_POINTER ( flags ) );
159  debug ( 3, "indexes_addexclude_aggr(indexes_p, \"%s\", flags): %u.", fpath, flags );
160  return 0;
161 }
162 
163 static inline int indexes_outaggr_add ( indexes_t *indexes_p, char *outline, eventinfo_flags_t flags )
164 {
165  gpointer flags_gp = g_hash_table_lookup ( indexes_p->out_lines_aggr_ht, outline );
166 
167  if ( flags_gp != NULL )
168  flags |= GPOINTER_TO_INT ( flags_gp );
169 
170  // Removing extra flags
172  flags &= ~EVIF_CONTENTRECURSIVELY;
173 
174  g_hash_table_replace ( indexes_p->out_lines_aggr_ht, outline, GINT_TO_POINTER ( flags ) );
175  debug ( 3, "indexes_outaggr_aggr(indexes_p, \"%s\").", outline );
176  return 0;
177 }
178 
179 static inline fileinfo_t *indexes_fileinfo ( indexes_t *indexes_p, const char *fpath )
180 {
181  return ( fileinfo_t * ) g_hash_table_lookup ( indexes_p->fileinfo_ht, fpath );
182 }
183 
184 static inline int indexes_fileinfo_add ( indexes_t *indexes_p, const char *fpath_const, fileinfo_t *fi )
185 {
186  size_t fpathlen = strlen ( fpath_const );
187  debug ( 4, "indexes_add_wd(indexes_p, \"%s\", %p)", fpath_const, fpathlen );
188  char *fpath = xmalloc ( fpathlen + 1 );
189  memcpy ( fpath, fpath_const, fpathlen + 1 );
190  g_hash_table_insert ( indexes_p->fileinfo_ht, fpath, fi );
191  return 0;
192 }
193 
194 #endif
195 
indexes::fpath2ei_coll_ht
GHashTable * fpath2ei_coll_ht[QUEUE_MAX]
Definition: indexes.h:40
indexes::fpath2wd_ht
GHashTable * fpath2wd_ht
Definition: indexes.h:36
indexes
Definition: indexes.h:34
queue_id_t
enum queue_id queue_id_t
Definition: ctx.h:181
indexes::wd2fpath_ht
GHashTable * wd2fpath_ht
Definition: indexes.h:35
eventinfo_flags_t
enum eventinfo_flags eventinfo_flags_t
Definition: clsync.h:61
error
#define error(...)
Definition: error.h:36
EVIF_RECURSIVELY
@ EVIF_RECURSIVELY
Definition: clsync.h:58
indexes::nonthreaded_syncing_fpath2ei_ht
GHashTable * nonthreaded_syncing_fpath2ei_ht
Definition: indexes.h:42
indexes::exc_fpath_coll_ht
GHashTable * exc_fpath_coll_ht[QUEUE_MAX]
Definition: indexes.h:39
error.h
malloc.h
QUEUE_MAX
@ QUEUE_MAX
Definition: ctx.h:178
debug
#define debug(debug_level,...)
Definition: error.h:50
indexes::fileinfo_ht
GHashTable * fileinfo_ht
Definition: indexes.h:43
eventinfo
Definition: common.h:140
common.h
fileinfo::lst
stat64_t lst
Definition: indexes.h:30
indexes::out_lines_aggr_ht
GHashTable * out_lines_aggr_ht
Definition: indexes.h:41
EVIF_CONTENTRECURSIVELY
@ EVIF_CONTENTRECURSIVELY
Definition: clsync.h:59
stat64_t
struct stat64 stat64_t
Definition: port-hacks.h:65
fileinfo
Definition: indexes.h:29
indexes::fpath2ei_ht
GHashTable * fpath2ei_ht
Definition: indexes.h:37
queue_id
queue_id
Definition: ctx.h:172
indexes::exc_fpath_ht
GHashTable * exc_fpath_ht
Definition: indexes.h:38