~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/srv/csm/shl/LocalSpace.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - LocalSpace.cpp                                                          -
 
3
// - afnix:csm module - abstract workspace class implementation              -
 
4
// ---------------------------------------------------------------------------
 
5
// - This program is free software;  you can redistribute it  and/or  modify -
 
6
// - it provided that this copyright notice is kept intact.                  -
 
7
// -                                                                         -
 
8
// - This program  is  distributed in  the hope  that it will be useful, but -
 
9
// - without  any  warranty;  without  even   the   implied    warranty   of -
 
10
// - merchantability or fitness for a particular purpose.  In no event shall -
 
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
 
12
// - special damages arising in any way out of the use of this software.     -
 
13
// ---------------------------------------------------------------------------
 
14
// - copyright (c) 1999-2015 amaury darsch                                   -
 
15
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Date.hpp"
 
18
#include "Vector.hpp"
 
19
#include "System.hpp"
 
20
#include "Utility.hpp"
 
21
#include "FileInfo.hpp"
 
22
#include "InputFile.hpp"
 
23
#include "Directory.hpp"
 
24
#include "QuarkZone.hpp"
 
25
#include "Exception.hpp"
 
26
#include "OutputFile.hpp"
 
27
#include "LocalSpace.hpp"
 
28
 
 
29
namespace afnix {
 
30
 
 
31
  // -------------------------------------------------------------------------
 
32
  // - private section                                                       -
 
33
  // -------------------------------------------------------------------------
 
34
 
 
35
  // the local space temporary sub directory
 
36
  static const String LSP_TSUB_DEF = ".local-space-tmp";
 
37
 
 
38
  // map a workspace zone
 
39
  static inline String lsp_map_zone (const String& root, const String& zone) {
 
40
    return System::join (root, zone);
 
41
  }
 
42
 
 
43
  // map a workspace file name by zone
 
44
  static inline String lsp_map_path (const String& root, const String& zone,
 
45
                                     const String& name) {
 
46
    String zpth = lsp_map_zone (root, zone);
 
47
    String path = System::join (zpth, name);
 
48
    return path;
 
49
  }
 
50
 
 
51
  // map a workspace path by root, zone and uri
 
52
  static inline String lsp_uri_path (const String& root, const String& zone,
 
53
                                     const Uri& uri) {
 
54
    // check the uri scheme
 
55
    String schm = uri.getscheme ();
 
56
    if (schm != WorkSpace::URI_SCHM_DEF) {
 
57
      throw Exception ("workspace-error", 
 
58
                       "invalid uri scheme in local space", schm);
 
59
    }
 
60
    // get the full path
 
61
    String name = uri.getpath().rsubstr (1);
 
62
    String path = lsp_map_path (root, zone, name);
 
63
    return path;
 
64
  }
 
65
 
 
66
  // -------------------------------------------------------------------------
 
67
  // - class section                                                         -
 
68
  // -------------------------------------------------------------------------
 
69
 
 
70
  // create a default localspace
 
71
 
 
72
  LocalSpace::LocalSpace (void) {
 
73
    // preset root and flags
 
74
    d_root = System::join (System::gettdir (), LSP_TSUB_DEF);
 
75
    d_tdir = d_root;
 
76
    d_gtmp = true;
 
77
    // clean old directory if any
 
78
    Directory::rmall (d_tdir);
 
79
    // make sure the root directory exists
 
80
    if (System::isdir (d_root) == false) System::mkdir (d_root);
 
81
    if (System::isdir (d_root) == false) {
 
82
      throw Exception ("workspace-error", "cannot create local root directory",
 
83
                       d_root);
 
84
    }
 
85
  }
 
86
 
 
87
  // create a localspace by root directory
 
88
 
 
89
  LocalSpace::LocalSpace (const String& root) {
 
90
    d_root = root;
 
91
    d_tdir = System::join (d_root, LSP_TSUB_DEF);
 
92
    d_gtmp = false;
 
93
    // make sure the root directory exists
 
94
    if (System::isdir (d_root) == false) System::mhdir (d_root);
 
95
    if (System::isdir (d_root) == false) {
 
96
      throw Exception ("workspace-error", "cannot create local root directory",
 
97
                       d_root);
 
98
    }
 
99
  }
 
100
 
 
101
  // create a localspace by name, info and root directory
 
102
 
 
103
  LocalSpace::LocalSpace (const String& name, const String& info,
 
104
                          const String& root) : WorkSpace (name, info) {
 
105
    d_root = root;
 
106
    d_tdir = System::join (d_root, LSP_TSUB_DEF);
 
107
    d_gtmp = false;
 
108
    // make sure the root directory exists
 
109
    if (System::isdir (d_root) == false) System::mhdir (d_root);
 
110
    if (System::isdir (d_root) == false) {
 
111
      throw Exception ("workspace-error", "cannot create local root directory",
 
112
                       d_root);
 
113
    }
 
114
  }
 
115
 
 
116
  // destroy the local space
 
117
 
 
118
  LocalSpace::~LocalSpace (void) {
 
119
   if (d_gtmp == true) Directory::rmall (d_tdir);
 
120
  }
 
121
 
 
122
  // return the class name
 
123
 
 
124
  String LocalSpace::repr (void) const {
 
125
    return "LocalSpace";
 
126
  }
 
127
 
 
128
  // get the root directoryy
 
129
 
 
130
  String LocalSpace::getroot (void) const {
 
131
    rdlock ();
 
132
    try {
 
133
      String result = d_root;
 
134
      unlock ();
 
135
      return result;
 
136
    } catch (...) {
 
137
      unlock ();
 
138
      throw;
 
139
    }
 
140
  }
 
141
 
 
142
  // check if a zone exists
 
143
 
 
144
  bool LocalSpace::iszone (const String& zone) const {
 
145
    rdlock ();
 
146
    try {
 
147
      // map the zone path
 
148
      String zpth = lsp_map_zone (d_root, zone);
 
149
      // check for a zone
 
150
      bool result = System::isdir (zpth);
 
151
      unlock ();
 
152
      return result;
 
153
    } catch (...) {
 
154
      unlock ();
 
155
      throw;
 
156
    }
 
157
  }
 
158
 
 
159
  // add a new working zone by name
 
160
  
 
161
  bool LocalSpace::addzone (const String& zone) {
 
162
    wrlock ();
 
163
    try {
 
164
      // get the use path
 
165
      String path = System::join (d_root, zone);
 
166
      // check if the directory already exists
 
167
      if (System::isdir (path) == true) {
 
168
        unlock ();
 
169
        return true;
 
170
      }
 
171
      // try to create the directory
 
172
      bool result = System::mkdir (path);
 
173
      unlock ();
 
174
      return result;
 
175
    } catch (...) {
 
176
      unlock ();
 
177
      throw;
 
178
    }
 
179
  }
 
180
 
 
181
  // check if an entity exists by zone and uri
 
182
 
 
183
  bool LocalSpace::exists (const String& zone, const Uri& uri) const {
 
184
    rdlock ();
 
185
    try {
 
186
      // map the uri to a path
 
187
      String path = lsp_uri_path (d_root, zone, uri);
 
188
      // check for valid path
 
189
      bool result = System::isfile (path);
 
190
      unlock ();
 
191
      return result;
 
192
    } catch (...) {
 
193
      unlock ();
 
194
      throw;
 
195
    }
 
196
  }
 
197
 
 
198
  // map a name to a workspace uri - no public mapping
 
199
 
 
200
  String LocalSpace::towuri (const String& zone, const String& name) const {
 
201
    rdlock ();
 
202
    try {
 
203
      // map the name by zone
 
204
      String path = lsp_map_path (d_root, zone, name);
 
205
      // normalize it
 
206
      String result = Uri::nrmname (path);
 
207
      unlock ();
 
208
      return result;
 
209
    } catch (...) {
 
210
      unlock ();
 
211
      throw;
 
212
    }
 
213
  }
 
214
 
 
215
  // map a name to a normalized uri
 
216
 
 
217
  String LocalSpace::tonuri (const String& zone, const String& name) const {
 
218
    rdlock ();
 
219
    try {
 
220
      // map the name by zone
 
221
      String path = lsp_map_path (d_root, zone, name);
 
222
      // check for existence
 
223
      if ((System::isfile (path) == false) && (d_zpub.isnil () == false)) {
 
224
        // rebuild path in the public zone
 
225
        path = lsp_map_path (d_root, d_zpub, name);
 
226
        if (System::isfile (path) == false) {
 
227
          unlock ();
 
228
          return "";
 
229
        }
 
230
      }
 
231
      // normalize it
 
232
      String result = Uri::nrmname (path);
 
233
      unlock ();
 
234
      return result;
 
235
    } catch (...) {
 
236
      unlock ();
 
237
      throw;
 
238
    }
 
239
  }
 
240
 
 
241
  // get a workspace zone file list
 
242
 
 
243
  Strvec* LocalSpace::getfiles (const String& zone) const {
 
244
    rdlock ();
 
245
    try {
 
246
      // check if the zone exists
 
247
      if (iszone (zone) == false) {
 
248
        unlock ();
 
249
        return nilp;
 
250
      }
 
251
      // map the zone
 
252
      String zpth = lsp_map_zone (d_root, zone);
 
253
      // locally map it as a directory
 
254
      Directory zdir (zpth);
 
255
      // get the file list
 
256
      Strvec* result = zdir.getfiles ();
 
257
      unlock ();
 
258
      return result;
 
259
    } catch (...) {
 
260
      unlock ();
 
261
      throw;
 
262
    }
 
263
  }
 
264
 
 
265
  // get a workspace zone file table
 
266
 
 
267
  PrintTable* LocalSpace::tofptbl (const String& zone) const {
 
268
    rdlock ();
 
269
    PrintTable* ptbl = nilp;
 
270
    try {
 
271
      // prepare the print table
 
272
      ptbl = new PrintTable (3);
 
273
      ptbl->sethead (0, "Name");
 
274
      ptbl->sethead (1, "Size");
 
275
      ptbl->sethead (2, "Modification Time");
 
276
      // get the file list by zone
 
277
      Strvec* flst = getfiles (zone);
 
278
      if (flst == nilp) {
 
279
        unlock ();
 
280
        return ptbl;
 
281
      }
 
282
      // loop in the list
 
283
      long flen = flst->length ();
 
284
      for (long k = 0; k < flen; k++) {
 
285
        // get the file name and path
 
286
        String name = flst->get (k);
 
287
        String path = lsp_map_path (d_root, zone, name);
 
288
        // get the file information
 
289
        FileInfo info (path);
 
290
        Date     mtim (info.mtime ());
 
291
        // set the table
 
292
        long row = ptbl->add ();
 
293
        ptbl->set (row, 0, name);
 
294
        ptbl->set (row, 1, Utility::tostring (info.length ()));
 
295
        ptbl->set (row, 2, mtim.toiso (true));
 
296
      }
 
297
      unlock ();
 
298
      return ptbl;
 
299
    } catch (...) {
 
300
      delete ptbl;
 
301
      unlock ();
 
302
      throw;
 
303
    }
 
304
  }
 
305
 
 
306
  // get an input stream by zone and uri
 
307
 
 
308
  InputStream* LocalSpace::getis (const String& zone, const Uri& uri) const {
 
309
    rdlock ();
 
310
    try {
 
311
      // map the uri to a path
 
312
      String path = lsp_uri_path (d_root, zone, uri);
 
313
      // create a local input stream
 
314
      InputStream* is = new InputFile (path);
 
315
      unlock ();
 
316
      return is;
 
317
    } catch (...) {
 
318
      unlock ();
 
319
      throw;
 
320
    }
 
321
  }
 
322
 
 
323
  // get an output stream by zone and uri
 
324
 
 
325
  OutputStream* LocalSpace::getos (const String& zone, const Uri& uri) const {
 
326
    rdlock ();
 
327
    try {
 
328
      // map the uri to a path
 
329
      String path = lsp_uri_path (d_root, zone, uri);
 
330
      // create a local input stream
 
331
      OutputStream* os = new OutputFile (path);
 
332
      unlock ();
 
333
      return os;
 
334
    } catch (...) {
 
335
      unlock ();
 
336
      throw;
 
337
    }
 
338
  }
 
339
 
 
340
  // -------------------------------------------------------------------------
 
341
  // - object section                                                        -
 
342
  // -------------------------------------------------------------------------
 
343
 
 
344
  // the quark zone
 
345
  static const long QUARK_ZONE_LENGTH = 1;
 
346
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
347
 
 
348
  // the object supported quarks
 
349
  static const long QUARK_GETROOT  = zone.intern ("get-root");
 
350
 
 
351
  // create a new object in a generic way
 
352
 
 
353
  Object* LocalSpace::mknew (Vector* argv) {
 
354
    long argc = (argv == nilp) ? 0 : argv->length ();
 
355
    // check for 0 arguments
 
356
    if (argc == 0) return new LocalSpace;
 
357
    // check for 1 argument
 
358
    if (argc == 1) {
 
359
      String root = argv->getstring (0);
 
360
      return new LocalSpace (root);
 
361
    }
 
362
    // check for 3 arguments
 
363
    if (argc == 3) {
 
364
      String name = argv->getstring (0);
 
365
      String info = argv->getstring (1);
 
366
      String root = argv->getstring (2);
 
367
      return new LocalSpace (name, info, root);
 
368
    }
 
369
    throw Exception ("argument-error",
 
370
                     "too many argument with local space ");
 
371
  }
 
372
 
 
373
  // return true if the given quark is defined
 
374
 
 
375
  bool LocalSpace::isquark (const long quark, const bool hflg) const {
 
376
    rdlock ();
 
377
    if (zone.exists (quark) == true) {
 
378
      unlock ();
 
379
      return true;
 
380
    }
 
381
    bool result = hflg ? WorkSpace::isquark (quark, hflg) : false;
 
382
    unlock ();
 
383
    return result;
 
384
  }
 
385
 
 
386
  // apply this object with a set of arguments and a quark
 
387
  
 
388
  Object* LocalSpace::apply (Runnable* robj, Nameset* nset, const long quark,
 
389
                            Vector* argv) {
 
390
    // get the number of arguments
 
391
    long argc = (argv == nilp) ? 0 : argv->length ();
 
392
    
 
393
    // check for 0 argument
 
394
    if (argc == 0) {
 
395
      if (quark == QUARK_GETROOT) return new String (getroot ());
 
396
    }
 
397
    // call the workspace method
 
398
    return WorkSpace::apply (robj, nset, quark, argv);
 
399
  }
 
400
}