File: config.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (470 lines) | stat: -rw-r--r-- 13,072 bytes parent folder | download | duplicates (4)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/***************************************************************************
 *  lib/mng/config.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
 *  Copyright (C) 2007, 2009 Johannes Singler <singler@ira.uka.de>
 *  Copyright (C) 2008, 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
 *  Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <fstream>
#include <stxxl/bits/common/error_handling.h>
#include <stxxl/bits/config.h>
#include <stxxl/bits/io/file.h>
#include <stxxl/bits/mng/config.h>
#include <stxxl/bits/common/utils.h>
#include <stxxl/version.h>

#if STXXL_WINDOWS
   #ifndef NOMINMAX
    #define NOMINMAX
  #endif
  #include <windows.h>
#else
  #include <unistd.h>
#endif

STXXL_BEGIN_NAMESPACE

static inline bool exist_file(const std::string& path)
{
    //STXXL_MSG("Checking " << path << " for disk configuration.");
    std::ifstream in(path.c_str());
    return in.good();
}

config::~config()
{
    for (disk_list_type::const_iterator it = disks_list.begin();
         it != disks_list.end(); it++)
    {
        if (it->delete_on_exit)
        {
            STXXL_ERRMSG("Removing disk file: " << it->path);
            unlink(it->path.c_str());
        }
    }
}

void config::initialize()
{
    // if disks_list is empty, then try to load disk configuration files
    if (disks_list.size() == 0)
    {
        find_config();
    }

    m_max_device_id = 0;

    is_initialized = true;
}

void config::find_config()
{
    // check several locations for disk configuration files

    // check STXXLCFG environment path
    const char* stxxlcfg = getenv("STXXLCFG");
    if (stxxlcfg && exist_file(stxxlcfg))
        return load_config_file(stxxlcfg);

#if !STXXL_WINDOWS
    // read environment, unix style
    const char* hostname = getenv("HOSTNAME");
    const char* home = getenv("HOME");
    const char* suffix = "";
#else
    // read environment, windows style
    const char* hostname = getenv("COMPUTERNAME");
    const char* home = getenv("APPDATA");
    const char* suffix = ".txt";
#endif

    // check current directory
    {
        std::string basepath = "./.stxxl";

        if (hostname && exist_file(basepath + "." + hostname + suffix))
            return load_config_file(basepath + "." + hostname + suffix);

        if (exist_file(basepath + suffix))
            return load_config_file(basepath + suffix);
    }

    // check home directory
    if (home)
    {
        std::string basepath = std::string(home) + "/.stxxl";

        if (hostname && exist_file(basepath + "." + hostname + suffix))
            return load_config_file(basepath + "." + hostname + suffix);

        if (exist_file(basepath + suffix))
            return load_config_file(basepath + suffix);
    }

    // load default configuration
    load_default_config();
}

void config::load_default_config()
{
    STXXL_ERRMSG("Warning: no config file found.");
    STXXL_ERRMSG("Using default disk configuration.");
#if !STXXL_WINDOWS
    disk_config entry1("/var/tmp/stxxl", 1000 * 1024 * 1024, "syscall");
    entry1.delete_on_exit = true;
    entry1.autogrow = true;
#else
    disk_config entry1("", 1000 * 1024 * 1024, "wincall");
    entry1.delete_on_exit = true;
    entry1.autogrow = true;

    char* tmpstr = new char[255];
    if (GetTempPath(255, tmpstr) == 0)
        STXXL_THROW_WIN_LASTERROR(resource_error, "GetTempPath()");
    entry1.path = tmpstr;
    entry1.path += "stxxl.tmp";
    delete[] tmpstr;
#endif
    disks_list.push_back(entry1);

    // no flash disks
    first_flash = (unsigned int)disks_list.size();
}

void config::load_config_file(const std::string& config_path)
{
    std::vector<disk_config> flash_list;
    std::ifstream cfg_file(config_path.c_str());

    if (!cfg_file)
        return load_default_config();

    std::string line;

    while (std::getline(cfg_file, line))
    {
        // skip comments
        if (line.size() == 0 || line[0] == '#') continue;

        disk_config entry;
        entry.parse_line(line); // throws on errors

        if (!entry.flash)
            disks_list.push_back(entry);
        else
            flash_list.push_back(entry);
    }
    cfg_file.close();

    // put flash devices after regular disks
    first_flash = (unsigned int)disks_list.size();
    disks_list.insert(disks_list.end(), flash_list.begin(), flash_list.end());

    if (disks_list.empty()) {
        STXXL_THROW(std::runtime_error,
                    "No disks found in '" << config_path << "'.");
    }
}

//! Returns automatic physical device id counter
unsigned int config::get_max_device_id()
{
    return m_max_device_id;
}

//! Returns next automatic physical device id counter
unsigned int config::get_next_device_id()
{
    return m_max_device_id++;
}

//! Update the automatic physical device id counter
void config::update_max_device_id(unsigned int devid)
{
    if (m_max_device_id < devid + 1)
        m_max_device_id = devid + 1;
}

uint64 config::total_size() const
{
    assert(is_initialized);

    uint64 total_size = 0;

    for (disk_list_type::const_iterator it = disks_list.begin();
         it != disks_list.end(); it++)
    {
        total_size += it->size;
    }

    return total_size;
}

////////////////////////////////////////////////////////////////////////////////

disk_config::disk_config()
    : size(0),
      autogrow(false),
      delete_on_exit(false),
      direct(DIRECT_TRY),
      flash(false),
      queue(file::DEFAULT_QUEUE),
      device_id(file::DEFAULT_DEVICE_ID),
      raw_device(false),
      unlink_on_open(false),
      queue_length(0)
{ }

disk_config::disk_config(const std::string& _path, uint64 _size,
                         const std::string& _io_impl)
    : path(_path),
      size(_size),
      io_impl(_io_impl),
      autogrow(false),
      delete_on_exit(false),
      direct(DIRECT_TRY),
      flash(false),
      queue(file::DEFAULT_QUEUE),
      device_id(file::DEFAULT_DEVICE_ID),
      raw_device(false),
      unlink_on_open(false),
      queue_length(0)
{
    parse_fileio();
}

disk_config::disk_config(const std::string& line)
    : size(0),
      autogrow(false),
      delete_on_exit(false),
      direct(DIRECT_TRY),
      flash(false),
      queue(file::DEFAULT_QUEUE),
      device_id(file::DEFAULT_DEVICE_ID),
      raw_device(false),
      unlink_on_open(false),
      queue_length(0)
{
    parse_line(line);
}

void disk_config::parse_line(const std::string& line)
{
    // split off disk= or flash=
    std::vector<std::string> eqfield = split(line, "=", 2, 2);

    if (eqfield[0] == "disk") {
        flash = false;
    }
    else if (eqfield[0] == "flash") {
        flash = true;
    }
    else {
        STXXL_THROW(std::runtime_error,
                    "Unknown configuration token " << eqfield[0]);
    }

    // *** Set Default Extra Options ***

    autogrow = false;
    delete_on_exit = false;
    direct = DIRECT_TRY;
    // flash is already set
    queue = file::DEFAULT_QUEUE;
    device_id = file::DEFAULT_DEVICE_ID;
    unlink_on_open = false;

    // *** Save Basic Options ***

    // split at commands, at least 3 fields
    std::vector<std::string> cmfield = split(eqfield[1], ",", 3, 3);

    // path:
    path = cmfield[0];
    // replace ### -> pid in path
    {
        std::string::size_type pos;
        if ((pos = path.find("###")) != std::string::npos)
        {
#if !STXXL_WINDOWS
            int pid = getpid();
#else
            DWORD pid = GetCurrentProcessId();
#endif
            path.replace(pos, 3, to_str(pid));
        }
    }

    // size: (default unit MiB)
    if (!parse_SI_IEC_size(cmfield[1], size, 'M')) {
        STXXL_THROW(std::runtime_error,
                    "Invalid disk size '" << cmfield[1] << "' in disk configuration file.");
    }

    if (size == 0) {
        autogrow = true;
        delete_on_exit = true;
    }

    // io_impl:
    io_impl = cmfield[2];
    parse_fileio();
}

void disk_config::parse_fileio()
{
    // skip over leading spaces
    size_t leadspace = io_impl.find_first_not_of(' ');
    if (leadspace > 0)
        io_impl = io_impl.substr(leadspace);

    // split off extra fileio parameters
    size_t spacepos = io_impl.find(' ');
    if (spacepos == std::string::npos)
        return; // no space in fileio

    // *** Parse Extra Fileio Parameters ***

    std::string paramstr = io_impl.substr(spacepos + 1);
    io_impl = io_impl.substr(0, spacepos);

    std::vector<std::string> param = split(paramstr, " ");

    for (std::vector<std::string>::const_iterator p = param.begin();
         p != param.end(); ++p)
    {
        // split at equal sign
        std::vector<std::string> eq = split(*p, "=", 2, 2);

        // *** PLEASE try to keep the elseifs sorted by parameter name!
        if (*p == "") {
            // skip blank options
        }
        else if (*p == "autogrow")
        {
            // TODO: which fileio implementation support autogrow?
            autogrow = true;
        }
        else if (*p == "delete" || *p == "delete_on_exit")
        {
            delete_on_exit = true;
        }
        else if (*p == "direct" || *p == "nodirect" || eq[0] == "direct")
        {
            // io_impl is not checked here, but I guess that is okay for DIRECT
            // since it depends highly platform _and_ build-time configuration.

            if (*p == "direct") direct = DIRECT_ON;          // force ON
            else if (*p == "nodirect") direct = DIRECT_OFF;  // force OFF
            else if (eq[1] == "off") direct = DIRECT_OFF;
            else if (eq[1] == "try") direct = DIRECT_TRY;
            else if (eq[1] == "on") direct = DIRECT_ON;
            else if (eq[1] == "no") direct = DIRECT_OFF;
            else if (eq[1] == "yes") direct = DIRECT_ON;
            else
            {
                STXXL_THROW(std::runtime_error,
                            "Invalid parameter '" << *p << "' in disk configuration file.");
            }
        }
        else if (eq[0] == "queue")
        {
            if (io_impl == "linuxaio") {
                STXXL_THROW(std::runtime_error, "Parameter '" << *p << "' invalid for fileio '" << io_impl << "' in disk configuration file.");
            }

            char* endp;
            queue = (int)strtoul(eq[1].c_str(), &endp, 10);
            if (endp && *endp != 0) {
                STXXL_THROW(std::runtime_error,
                            "Invalid parameter '" << *p << "' in disk configuration file.");
            }
        }
        else if (eq[0] == "device_id" || eq[0] == "devid")
        {
            char* endp;
            device_id = (int)strtoul(eq[1].c_str(), &endp, 10);
            if (endp && *endp != 0) {
                STXXL_THROW(std::runtime_error,
                            "Invalid parameter '" << *p << "' in disk configuration file.");
            }
        }
        else if (*p == "raw_device")
        {
            if (!(io_impl == "syscall")) {
                STXXL_THROW(std::runtime_error, "Parameter '" << *p << "' invalid for fileio '" << io_impl << "' in disk configuration file.");
            }

            raw_device = true;
        }
        else if (*p == "unlink" || *p == "unlink_on_open")
        {
            if (!(io_impl == "syscall" || io_impl == "linuxaio" ||
                  io_impl == "mmap" || io_impl == "wbtl"))
            {
                STXXL_THROW(std::runtime_error, "Parameter '" << *p << "' invalid for fileio '" << io_impl << "' in disk configuration file.");
            }

            unlink_on_open = true;
        }
        else
        {
            STXXL_THROW(std::runtime_error,
                        "Invalid optional parameter '" << *p << "' in disk configuration file.");
        }
    }
}

std::string disk_config::fileio_string() const
{
    std::ostringstream oss;

    oss << io_impl;

    if (autogrow)
        oss << " autogrow";

    if (delete_on_exit)
        oss << " delete_on_exit";

    // tristate direct variable: OFF, TRY, ON
    if (direct == DIRECT_OFF)
        oss << " direct=off";
    else if (direct == DIRECT_TRY)
        ; // silenced: oss << " direct=try";
    else if (direct == DIRECT_ON)
        oss << " direct=on";
    else
        STXXL_THROW(std::runtime_error, "Invalid setting for 'direct' option.");

    if (flash)
        oss << " flash";

    if (queue != file::DEFAULT_QUEUE && queue != file::DEFAULT_LINUXAIO_QUEUE)
        oss << " queue=" << queue;

    if (device_id != file::DEFAULT_DEVICE_ID)
        oss << " devid=" << device_id;

    if (raw_device)
        oss << " raw_device";

    if (unlink_on_open)
        oss << " unlink_on_open";

    if (queue_length != 0)
        oss << " queue_length=" << queue_length;

    return oss.str();
}

STXXL_END_NAMESPACE
// vim: et:ts=4:sw=4