smbnetfs  0.6.3
About: SMBNetFS is a Linux/FreeBSD filesystem that allow you to use samba/microsoft network in the same manner as the network neighborhood in Microsoft Windows.
  Fossies Dox: smbnetfs-0.6.3.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

neg_cache.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include <sys/time.h>
4 #include <pthread.h>
5 
6 #include "common.h"
7 #include "list.h"
8 #include "neg_cache.h"
9 
10 struct neg_cache{
13  struct timeval tv;
15  char hostname[1];
16 };
17 
18 
19 static int neg_cache_timeout = 3000;
20 static int neg_cache_enabled = 1;
21 
24 static pthread_mutex_t m_neg_cache = PTHREAD_MUTEX_INITIALIZER;
25 
26 
27 static void neg_cache_remove_outdate_tv(struct timeval *tv){
28  LIST *elem;
29  struct neg_cache *cache;
30  struct timeval res;
31 
32  while(1){
34  if (!is_valid_list_elem(&neg_cache_time_list, elem)) break;
35 
36  cache = list_entry(elem, struct neg_cache, time_entries);
37  if (timercmp(tv, &cache->tv, <)) goto bad_time;
38 
39  timersub(tv, &cache->tv, &res);
40  if (res.tv_usec / 1000 + res.tv_sec * 1000 < neg_cache_timeout) break;
41 
42  bad_time:
45  free(cache);
46  }
47 }
48 
49 static struct neg_cache * neg_cache_find_by_name(const char *name, size_t len){
50  LIST *elem;
51  struct neg_cache *cache;
52 
55  cache = list_entry(elem, struct neg_cache, usage_entries);
56  if ((strncmp(cache->hostname, name, len) == 0) && (cache->hostname[len] == '\0'))
57  return cache;
58  elem = elem->next;
59  }
60  return NULL;
61 }
62 
63 static inline void neg_cache_remove_outdate(void){
64  struct timeval tv;
65 
66  gettimeofday(&tv, NULL);
68 }
69 
70 int neg_cache_set_timeout(int timeout){
71  if (timeout <= 0) return 0;
72  DPRINTF(7, "timeout=%d\n", timeout);
73  pthread_mutex_lock(&m_neg_cache);
74  neg_cache_timeout = timeout;
76  pthread_mutex_unlock(&m_neg_cache);
77  return 1;
78 }
79 
80 int neg_cache_enable(int status){
81  DPRINTF(7, "status=%d\n", status);
82  pthread_mutex_lock(&m_neg_cache);
83  neg_cache_enabled = status;
84  if (!status) neg_cache_flush();
85  pthread_mutex_unlock(&m_neg_cache);
86  return 1;
87 }
88 
89 int neg_cache_check(const char *url){
90  int result = 0;
91  size_t len = 0;
92  struct neg_cache *cache;
93 
94  while(*url == '/') url++;
95  while((url[len] != '/') && (url[len] != '\0')) len++;
96  if (len == 0) return 0;
97 
98  pthread_mutex_lock(&m_neg_cache);
99  if (!neg_cache_enabled) goto end;
101  cache = neg_cache_find_by_name(url, len);
102  if (cache != NULL){
105  result = cache->errno_value;
106  }
107  end:
108  pthread_mutex_unlock(&m_neg_cache);
109  return result;
110 }
111 
112 int neg_cache_store(const char *url, int errno_value){
113  int result = 0;
114  size_t len = 0;
115  struct neg_cache *cache;
116  struct timeval tv;
117 
118  while(*url == '/') url++;
119  while((url[len] != '/') && (url[len] != '\0')) len++;
120  if (len == 0) return 0;
121 
122  pthread_mutex_lock(&m_neg_cache);
123  if (!neg_cache_enabled) goto end;
124 
125  gettimeofday(&tv, NULL);
127 
128  cache = neg_cache_find_by_name(url, len);
129  if (cache != NULL){
132  }else{
133  cache = malloc(sizeof(struct neg_cache) + len);
134  if (cache == NULL) goto end;
135  memset(cache, 0, sizeof(struct neg_cache) + len);
136  strncpy(cache->hostname, url, len);
137  }
138  cache->tv = tv;
139  cache->errno_value = errno_value;
142  result = 1;
143 
144  end:
145  pthread_mutex_unlock(&m_neg_cache);
146  return result;
147 }
148 
149 void neg_cache_flush(void){
150  LIST *elem;
151  struct neg_cache *cache;
152 
153  pthread_mutex_lock(&m_neg_cache);
154  while(1){
156  if (!is_valid_list_elem(&neg_cache_usage_list, elem)) break;
157 
158  cache = list_entry(elem, struct neg_cache, usage_entries);
161  free(cache);
162  }
163  pthread_mutex_unlock(&m_neg_cache);
164 }
#define DPRINTF(level, fmt, args...)
Definition: common.h:47
static int is_valid_list_elem(LIST *list, LIST *elem)
Definition: list.h:88
static LIST * last_list_elem(LIST *list)
Definition: list.h:20
static LIST * first_list_elem(LIST *list)
Definition: list.h:16
#define list_entry(ptr, type, member)
Definition: list.h:11
#define STATIC_LIST_INITIALIZER(list)
Definition: list.h:14
static void add_to_list(LIST *list, LIST *elem)
Definition: list.h:24
static void remove_from_list(LIST *list, LIST *elem)
Definition: list.h:77
static void neg_cache_remove_outdate(void)
Definition: neg_cache.c:63
static struct neg_cache * neg_cache_find_by_name(const char *name, size_t len)
Definition: neg_cache.c:49
int neg_cache_check(const char *url)
Definition: neg_cache.c:89
int neg_cache_enable(int status)
Definition: neg_cache.c:80
int neg_cache_store(const char *url, int errno_value)
Definition: neg_cache.c:112
static void neg_cache_remove_outdate_tv(struct timeval *tv)
Definition: neg_cache.c:27
int neg_cache_set_timeout(int timeout)
Definition: neg_cache.c:70
void neg_cache_flush(void)
Definition: neg_cache.c:149
static pthread_mutex_t m_neg_cache
Definition: neg_cache.c:24
static LIST neg_cache_time_list
Definition: neg_cache.c:23
static int neg_cache_enabled
Definition: neg_cache.c:20
static LIST neg_cache_usage_list
Definition: neg_cache.c:22
static int neg_cache_timeout
Definition: neg_cache.c:19
Definition: list.h:6
struct __LIST * next
Definition: list.h:7
LIST time_entries
Definition: neg_cache.c:12
struct timeval tv
Definition: neg_cache.c:13
char hostname[1]
Definition: neg_cache.c:15
LIST usage_entries
Definition: neg_cache.c:11
int errno_value
Definition: neg_cache.c:14