1
// ---------------------------------------------------------------------------
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. -
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
// ---------------------------------------------------------------------------
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"
31
// -------------------------------------------------------------------------
32
// - private section -
33
// -------------------------------------------------------------------------
35
// the local space temporary sub directory
36
static const String LSP_TSUB_DEF = ".local-space-tmp";
38
// map a workspace zone
39
static inline String lsp_map_zone (const String& root, const String& zone) {
40
return System::join (root, zone);
43
// map a workspace file name by zone
44
static inline String lsp_map_path (const String& root, const String& zone,
46
String zpth = lsp_map_zone (root, zone);
47
String path = System::join (zpth, name);
51
// map a workspace path by root, zone and uri
52
static inline String lsp_uri_path (const String& root, const String& zone,
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);
61
String name = uri.getpath().rsubstr (1);
62
String path = lsp_map_path (root, zone, name);
66
// -------------------------------------------------------------------------
68
// -------------------------------------------------------------------------
70
// create a default localspace
72
LocalSpace::LocalSpace (void) {
73
// preset root and flags
74
d_root = System::join (System::gettdir (), LSP_TSUB_DEF);
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",
87
// create a localspace by root directory
89
LocalSpace::LocalSpace (const String& root) {
91
d_tdir = System::join (d_root, LSP_TSUB_DEF);
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",
101
// create a localspace by name, info and root directory
103
LocalSpace::LocalSpace (const String& name, const String& info,
104
const String& root) : WorkSpace (name, info) {
106
d_tdir = System::join (d_root, LSP_TSUB_DEF);
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",
116
// destroy the local space
118
LocalSpace::~LocalSpace (void) {
119
if (d_gtmp == true) Directory::rmall (d_tdir);
122
// return the class name
124
String LocalSpace::repr (void) const {
128
// get the root directoryy
130
String LocalSpace::getroot (void) const {
133
String result = d_root;
142
// check if a zone exists
144
bool LocalSpace::iszone (const String& zone) const {
148
String zpth = lsp_map_zone (d_root, zone);
150
bool result = System::isdir (zpth);
159
// add a new working zone by name
161
bool LocalSpace::addzone (const String& zone) {
165
String path = System::join (d_root, zone);
166
// check if the directory already exists
167
if (System::isdir (path) == true) {
171
// try to create the directory
172
bool result = System::mkdir (path);
181
// check if an entity exists by zone and uri
183
bool LocalSpace::exists (const String& zone, const Uri& uri) const {
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);
198
// map a name to a workspace uri - no public mapping
200
String LocalSpace::towuri (const String& zone, const String& name) const {
203
// map the name by zone
204
String path = lsp_map_path (d_root, zone, name);
206
String result = Uri::nrmname (path);
215
// map a name to a normalized uri
217
String LocalSpace::tonuri (const String& zone, const String& name) const {
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) {
232
String result = Uri::nrmname (path);
241
// get a workspace zone file list
243
Strvec* LocalSpace::getfiles (const String& zone) const {
246
// check if the zone exists
247
if (iszone (zone) == false) {
252
String zpth = lsp_map_zone (d_root, zone);
253
// locally map it as a directory
254
Directory zdir (zpth);
256
Strvec* result = zdir.getfiles ();
265
// get a workspace zone file table
267
PrintTable* LocalSpace::tofptbl (const String& zone) const {
269
PrintTable* ptbl = nilp;
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);
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 ());
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));
306
// get an input stream by zone and uri
308
InputStream* LocalSpace::getis (const String& zone, const Uri& uri) const {
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);
323
// get an output stream by zone and uri
325
OutputStream* LocalSpace::getos (const String& zone, const Uri& uri) const {
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);
340
// -------------------------------------------------------------------------
341
// - object section -
342
// -------------------------------------------------------------------------
345
static const long QUARK_ZONE_LENGTH = 1;
346
static QuarkZone zone (QUARK_ZONE_LENGTH);
348
// the object supported quarks
349
static const long QUARK_GETROOT = zone.intern ("get-root");
351
// create a new object in a generic way
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
359
String root = argv->getstring (0);
360
return new LocalSpace (root);
362
// check for 3 arguments
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);
369
throw Exception ("argument-error",
370
"too many argument with local space ");
373
// return true if the given quark is defined
375
bool LocalSpace::isquark (const long quark, const bool hflg) const {
377
if (zone.exists (quark) == true) {
381
bool result = hflg ? WorkSpace::isquark (quark, hflg) : false;
386
// apply this object with a set of arguments and a quark
388
Object* LocalSpace::apply (Runnable* robj, Nameset* nset, const long quark,
390
// get the number of arguments
391
long argc = (argv == nilp) ? 0 : argv->length ();
393
// check for 0 argument
395
if (quark == QUARK_GETROOT) return new String (getroot ());
397
// call the workspace method
398
return WorkSpace::apply (robj, nset, quark, argv);