aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/fsamanager.cpp
blob: 8816ea2a4b8f6cd4299d43d50ab606635fbc3bf7 (plain) (blame)
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/08/20
 * @version $Id$
 * @file    fsamanager.cpp
 * @brief
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "fsamanager.h"

#ifdef HAVE_CURL
#include <stdio.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#endif



namespace fsa {

// {{{ FSAManager::~FSAManager()

FSAManager::~FSAManager()
{
  for(LibraryIterator it=_library.begin(); it!=_library.end();++it){
    delete it->second;
  }
}

// }}}
// {{{ FSAManager::load()

bool FSAManager::load(const std::string &id, const std::string &url)
{
  std::string file=url;

#if ((__GNUG__ == 3 && __GNUC_MINOR__ >= 1) || __GNUG__ > 3)
  if(!url.compare(0,7,"http://"))
#else
  if(!url.compare("http://",0,7))
#endif
  {
    unsigned int pos=url.find_last_of('/');
    if(pos==url.size()-1) return false;
    _cacheLock.lock();
    file=_cacheDir;
    _cacheLock.unlock();
    if(file.size()>0 && file[file.size()-1]!='/') file+='/';
    file+=url.substr(pos+1);
    if(!getUrl(url,file)) return false;
  }

  FSA::Handle *newdict = new FSA::Handle(file);
  if(!newdict->isOk()){
    delete newdict;
    return false;
  }

  _lock.wrLock();
  {
    LibraryIterator it = _library.find(id);
    if(it!=_library.end()){
      delete it->second;
      it->second = newdict;
    }
    else
      _library.insert(Library::value_type(id,newdict));
  }
  _lock.unlock();

  return true;
}

// }}}
// {{{ FSAManager::getUrl()

bool FSAManager::getUrl(const std::string &url, const std::string &file)
{
#ifdef HAVE_CURL
  CURL *curl_handle;
  FILE *filehandle;
  long  response_code;

  filehandle = fopen(file.c_str(),"r");
  if(filehandle!=NULL){
    fclose(filehandle);
    return true;
  }

  filehandle = fopen(file.c_str(),"w");
  if(filehandle==NULL)
    return false;

  curl_handle  = curl_easy_init();

  curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)filehandle);
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libfsa-url-agent/0.1");

  curl_easy_perform(curl_handle);

  curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &response_code);

  curl_easy_cleanup(curl_handle);

  fclose(filehandle);

  if(response_code!=200){
    unlink(file.c_str());
    return false;
  }

  return true;
#else  // HAVE_CURL
  (void)url;(void)file;
  return false;
#endif // HAVE_CURL
}

// }}}
// {{{ FSAManager::get()

FSA::Handle* FSAManager::get(const std::string &id) const
{
  FSA::Handle *newhandle=NULL;
  _lock.rdLock();
  {
    LibraryConstIterator it = _library.find(id);
    if(it!=_library.end()){
      newhandle = new FSA::Handle(*(it->second));
    }
  }
  _lock.unlock();
  return newhandle;
}

// }}}
// {{{ FSAManager::drop()

void FSAManager::drop(const std::string &id)
{
  _lock.wrLock();
  {
    LibraryIterator it = _library.find(id);
    if(it!=_library.end()){
      delete it->second;
      _library.erase(it);
    }
  }
  _lock.unlock();
}

// }}}
// {{{ FSAManager::clear()

void FSAManager::clear()
{
  _lock.wrLock();
  {
    for(LibraryIterator it = _library.begin(); it!=_library.end(); ++it)
      delete it->second;
    _library.clear();
  }
  _lock.unlock();
}

// }}}
// {{{ FSAManager::setCacheDir()

void FSAManager::setCacheDir(const std::string &dir)
{
  _cacheLock.lock();
  _cacheDir = dir;
  _cacheLock.unlock();
}

// }}}

} // namespace fsa