STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
create_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/create_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2008, 2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
19 #include <stxxl/bits/io/io.h>
20 #include <stxxl/bits/mng/config.h>
21 
22 #include <ostream>
23 #include <stdexcept>
24 
25 
27 
28 file * create_file(const std::string& io_impl,
29  const std::string& filename,
30  int options, int physical_device_id, int disk_allocator_id)
31 {
32  // construct temporary disk_config structure
33  disk_config cfg(filename, 0, io_impl);
34  cfg.queue = physical_device_id;
35  cfg.direct =
36  (options& file::REQUIRE_DIRECT) ? disk_config::DIRECT_ON :
37  (options& file::DIRECT) ? disk_config::DIRECT_TRY :
38  disk_config::DIRECT_OFF;
39 
40  return create_file(cfg, options, disk_allocator_id);
41 }
42 
43 file * create_file(disk_config& cfg, int mode, int disk_allocator_id)
44 {
45  // apply disk_config settings to open mode
46 
47  mode &= ~(file::DIRECT | file::REQUIRE_DIRECT); // clear DIRECT and REQUIRE_DIRECT
48 
49  switch (cfg.direct) {
50  case disk_config::DIRECT_OFF:
51  break;
52  case disk_config::DIRECT_TRY:
53  mode |= file::DIRECT;
54  break;
55  case disk_config::DIRECT_ON:
56  mode |= file::DIRECT | file::REQUIRE_DIRECT;
57  break;
58  }
59 
60  // *** Select fileio Implementation
61 
62  if (cfg.io_impl == "syscall")
63  {
64  ufs_file_base* result =
65  new syscall_file(cfg.path, mode, cfg.queue, disk_allocator_id);
66  result->lock();
67 
68  // if marked as device but file is not -> throw!
69  if (cfg.raw_device && !result->is_device())
70  {
71  delete result;
72  STXXL_THROW(io_error, "Disk " << cfg.path << " was expected to be raw block device, but it is a normal file!");
73  }
74 
75  // if is raw_device -> get size and remove some flags.
76  if (result->is_device())
77  {
78  // if device
79  cfg.raw_device = true;
80  cfg.size = result->size();
81  cfg.autogrow = cfg.delete_on_exit = cfg.unlink_on_open = false;
82  }
83 
84  if (cfg.unlink_on_open)
85  result->unlink();
86 
87  return result;
88  }
89  else if (cfg.io_impl == "fileperblock_syscall")
90  {
92  new fileperblock_file<syscall_file>(cfg.path, mode, cfg.queue, disk_allocator_id);
93  result->lock();
94  return result;
95  }
96  else if (cfg.io_impl == "memory")
97  {
98  mem_file* result = new mem_file(cfg.queue, disk_allocator_id);
99  result->lock();
100  return result;
101  }
102 #if STXXL_HAVE_MMAP_FILE
103  else if (cfg.io_impl == "mmap")
104  {
105  ufs_file_base* result =
106  new mmap_file(cfg.path, mode, cfg.queue, disk_allocator_id);
107  result->lock();
108 
109  if (cfg.unlink_on_open)
110  result->unlink();
111 
112  return result;
113  }
114  else if (cfg.io_impl == "fileperblock_mmap")
115  {
117  new fileperblock_file<mmap_file>(cfg.path, mode, cfg.queue, disk_allocator_id);
118  result->lock();
119  return result;
120  }
121 #endif
122 #if STXXL_HAVE_SIMDISK_FILE
123  else if (cfg.io_impl == "simdisk")
124  {
125  mode &= ~(file::DIRECT | file::REQUIRE_DIRECT); // clear the DIRECT flag, this file is supposed to be on tmpfs
126  ufs_file_base* result =
127  new sim_disk_file(cfg.path, mode, cfg.queue, disk_allocator_id);
128  result->lock();
129  return result;
130  }
131 #endif
132 #if STXXL_HAVE_WINCALL_FILE
133  else if (cfg.io_impl == "wincall")
134  {
135  wfs_file_base* result =
136  new wincall_file(cfg.path, mode, cfg.queue, disk_allocator_id);
137  result->lock();
138  return result;
139  }
140  else if (cfg.io_impl == "fileperblock_wincall")
141  {
143  new fileperblock_file<wincall_file>(cfg.path, mode, cfg.queue, disk_allocator_id);
144  result->lock();
145  return result;
146  }
147 #endif
148 #if STXXL_HAVE_BOOSTFD_FILE
149  else if (cfg.io_impl == "boostfd")
150  {
151  boostfd_file* result =
152  new boostfd_file(cfg.path, mode, cfg.queue, disk_allocator_id);
153  result->lock();
154  return result;
155  }
156  else if (cfg.io_impl == "fileperblock_boostfd")
157  {
159  new fileperblock_file<boostfd_file>(cfg.path, mode, cfg.queue, disk_allocator_id);
160  result->lock();
161  return result;
162  }
163 #endif
164 #if STXXL_HAVE_WBTL_FILE
165  else if (cfg.io_impl == "wbtl")
166  {
167  ufs_file_base* backend =
168  new syscall_file(cfg.path, mode, -1, -1); // FIXME: ID
169  wbtl_file* result =
170  new stxxl::wbtl_file(backend, 16 * 1024 * 1024, 2, cfg.queue, disk_allocator_id);
171  result->lock();
172 
173  if (cfg.unlink_on_open)
174  backend->unlink();
175 
176  return result;
177  }
178 #endif
179 
180  STXXL_THROW(std::runtime_error,
181  "Unsupported disk I/O implementation '" << cfg.io_impl << "'.");
182 }
183 
185 // vim: et:ts=4:sw=4
void lock()
Locks file for reading and writing (acquires a lock in the file system).
file * create_file(const std::string &io_impl, const std::string &filename, int options, int physical_device_id=file::DEFAULT_QUEUE, int disk_allocator_id=file::NO_ALLOCATOR)
create fileio object from io_impl string and a few parameters
Definition: create_file.cpp:28
int queue
select request queue for disk. Use different queues for files on different disks. queue=-1 -&gt; default...
Definition: config.h:92
bool unlink_on_open
unlink file immediately after opening (available on most Unix)
Definition: config.h:98
#define STXXL_THROW(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message]&quot;.
Implementation of disk emulation.
Definition: simdisk_file.h:115
void lock()
Locks file for reading and writing (acquires a lock in the file system).
void lock()
Locks file for reading and writing (acquires a lock in the file system).
Definition: mem_file.cpp:58
bool delete_on_exit
delete file on program exit (default for autoconfigurated files)
Definition: config.h:80
Defines interface of file.
Definition: file.h:52
Implementation of memory mapped access file.
Definition: mmap_file.h:32
std::string path
the file path used by the io implementation
Definition: config.h:42
Encapsulate the configuration of one &quot;disk&quot;. The disk is actually a file I/O object which block_manag...
Definition: config.h:35
void unlink()
unlink file without closing it.
bool autogrow
autogrow file if more disk space is needed, automatically set if size == 0.
Definition: config.h:77
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void lock()
Locks file for reading and writing (acquires a lock in the file system).
Implementation of file based on other files, dynamically allocate one file per block. Allows for dynamic disk space consumption.
Implementation of file based on buffered writes and block remapping via a translation layer...
Definition: wbtl_file.h:37
Implementation of file based on Windows native I/O calls.
Definition: wincall_file.h:40
Implementation of file based on new[] and memcpy.
Definition: mem_file.h:27
bool raw_device
turned on by syscall fileio when the path points to a raw block device
Definition: config.h:95
Base for Windows file system implementations.
Definition: wfs_file_base.h:34
Base for UNIX file system implementations.
Definition: ufs_file_base.h:34
enum stxxl::disk_config::direct_type direct
Implementation of file based on UNIX syscalls.
Definition: syscall_file.h:28
offset_type size()
Returns size of the file.
std::string io_impl
io implementation to access file
Definition: config.h:48
uint64 size
file size to initially allocate
Definition: config.h:45
virtual void lock()
Locks file for reading and writing (acquires a lock in the file system).
bool is_device() const
return true if file is special device node
#define STXXL_END_NAMESPACE
Definition: namespace.h:17