STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileperblock_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/fileperblock_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2008, 2009 Johannes Singler <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #include <cassert>
15 #include <cstdio>
16 #include <iomanip>
17 #include <sstream>
18 #include <string>
19 
24 #include <stxxl/bits/config.h>
28 #include <stxxl/bits/io/file.h>
33 #include <stxxl/bits/io/request.h>
37 #include <stxxl/bits/namespace.h>
38 #include <stxxl/bits/unused.h>
39 #include <stxxl/bits/verbose.h>
40 #include "ufs_platform.h"
41 
43 
44 template <class base_file_type>
46  const std::string& filename_prefix,
47  int mode,
48  int queue_id,
49  int allocator_id,
50  unsigned int device_id)
51  : file(device_id),
52  disk_queued_file(queue_id, allocator_id),
53  filename_prefix(filename_prefix),
54  mode(mode),
55  current_size(0),
56  lock_file_created(false),
57  lock_file(filename_prefix + "_fpb_lock", mode, queue_id)
58 { }
59 
60 template <class base_file_type>
62 {
63  if (lock_file_created)
64  {
65  if (::remove((filename_prefix + "_fpb_lock").c_str()) != 0)
66  STXXL_ERRMSG("remove() error on path=" << filename_prefix << "_fpb_lock error=" << strerror(errno));
67  }
68 }
69 
70 template <class base_file_type>
72 {
73  std::ostringstream name;
74  //enough for 1 billion blocks
75  name << filename_prefix << "_fpb_" << std::setw(20) << std::setfill('0') << offset;
76  return name.str();
77 }
78 
79 template <class base_file_type>
82 {
83  base_file_type base_file(filename_for_block(offset), mode, get_queue_id());
84  base_file.set_size(bytes);
85  base_file.serve(buffer, 0, bytes, type);
86 }
87 
88 template <class base_file_type>
90 {
91  if (!lock_file_created)
92  {
93  //create lock file and fill it with one page, an empty file cannot be locked
94  const int page_size = STXXL_BLOCK_ALIGN;
95  void* one_page = aligned_alloc<STXXL_BLOCK_ALIGN>(page_size);
96 #if STXXL_WITH_VALGRIND
97  memset(one_page, 0, page_size);
98 #endif
99  lock_file.set_size(page_size);
100  request_ptr r = lock_file.awrite(one_page, 0, page_size);
101  r->wait();
102  aligned_dealloc<STXXL_BLOCK_ALIGN>(one_page);
103  lock_file_created = true;
104  }
105  lock_file.lock();
106 }
107 
108 template <class base_file_type>
110 {
111  STXXL_UNUSED(length);
112 #ifdef STXXL_FILEPERBLOCK_NO_DELETE
113  if (::truncate(filename_for_block(offset).c_str(), 0) != 0)
114  STXXL_ERRMSG("truncate() error on path=" << filename_for_block(offset) << " error=" << strerror(errno));
115 #else
116  if (::remove(filename_for_block(offset).c_str()) != 0)
117  STXXL_ERRMSG("remove() error on path=" << filename_for_block(offset) << " error=" << strerror(errno));
118 #endif
119 
120  STXXL_VERBOSE2("discard " << offset << " + " << length);
121 }
122 
123 template <class base_file_type>
125 {
126  std::string original(filename_for_block(offset));
127  filename.insert(0, original.substr(0, original.find_last_of("/") + 1));
128  if (::remove(filename.c_str()) != 0)
129  STXXL_ERRMSG("remove() error on path=" << filename << " error=" << strerror(errno));
130 
131  if (::rename(original.c_str(), filename.c_str()) != 0)
132  STXXL_ERRMSG("rename() error on path=" << filename << " to=" << original << " error=" << strerror(errno));
133 
134 #if !STXXL_WINDOWS
135  //TODO: implement on Windows
136  if (::truncate(filename.c_str(), length) != 0) {
137  STXXL_THROW_ERRNO(io_error, "Error doing truncate()");
138  }
139 #else
140  STXXL_UNUSED(length);
141 #endif
142 }
143 
144 template <class base_file_type>
146 {
147  return "fileperblock";
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////
151 
152 template class fileperblock_file<syscall_file>;
153 
154 #if STXXL_HAVE_MMAP_FILE
155 template class fileperblock_file<mmap_file>;
156 #endif
157 
158 #if STXXL_HAVE_WINCALL_FILE
159 template class fileperblock_file<wincall_file>;
160 #endif
161 
162 #if STXXL_HAVE_BOOSTFD_FILE
163 template class fileperblock_file<boostfd_file>;
164 #endif
165 
virtual void discard(offset_type offset, offset_type length)
Frees the specified region. Actually deletes the corresponding file if the whole thing is deleted...
#define STXXL_VERBOSE2(x)
Definition: verbose.h:121
virtual void export_files(offset_type offset, offset_type length, std::string filename)
Rename the file corresponding to the offset such that it is out of reach for deleting.
#define STXXL_BLOCK_ALIGN
Definition: request.h:33
virtual void serve(void *buffer, offset_type offset, size_type bytes, request::request_type type)
request::size_type size_type
the size of a request
Definition: file.h:62
virtual void wait(bool measure_time=true)=0
Suspends calling thread until completion of the request.
Defines interface of file.
Definition: file.h:56
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:22
Implementation of some file methods based on serving_request.
Implementation of file based on other files, dynamically allocate one file per block. Allows for dynamic disk space consumption.
#define STXXL_ERRMSG(x)
Definition: verbose.h:94
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message] : [errno message]&quot;.
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.h:96
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:60
virtual void lock()
Locks file for reading and writing (acquires a lock in the file system).
std::string filename_for_block(offset_type offset)
Constructs a file name for a given block.
const char * io_type() const
Identifies the type of I/O implementation.
#define STXXL_END_NAMESPACE
Definition: namespace.h:17