Line data Source code
1 : //
2 : // Author: Vladimir Migashko <migashko@gmail.com>, (C) 2012
3 : //
4 : // Copyright: See COPYING file that comes with this distribution
5 : //
6 :
7 : #ifndef VSET_VSET_BUFFER_PERSISTENT_FILESYNC_AD_TRUNCATE_HPP
8 : #define VSET_VSET_BUFFER_PERSISTENT_FILESYNC_AD_TRUNCATE_HPP
9 :
10 : #include <vset/buffer/tags.hpp>
11 : #include <vset/buffer/persistent/tags.hpp>
12 : #include <unistd.h>
13 :
14 : namespace vset { namespace buffer { namespace persistent{ namespace filesync{
15 :
16 :
17 3 : struct ad_truncate
18 : {
19 : template<typename T>
20 2 : void operator()( T& t, size_t s)
21 : {
22 : typedef typename T::aspect::template advice_cast<_head_type_>::type head_type;
23 2 : head_type* head = t.get_aspect().template get<_head_>()(t);
24 2 : size_t size = head->size();
25 2 : size_t data_offset = head->offset();
26 :
27 2 : t.get_aspect().template get<_head_>()(t)->set_capacity(s);
28 2 : if (s < size )
29 1 : t.get_aspect().template get<_head_>()(t)->set_size(s);
30 :
31 2 : t.get_aspect().template get<_sync_>()(t);
32 2 : std::string filename = t.get_aspect().template get<_file_name_>();
33 2 : t.get_aspect().template get<_close_>()(t);
34 2 : if ( -1 == ::truncate(filename.c_str(), static_cast<off_t>(data_offset + s) ) )
35 0 : throw std::runtime_error(strerror(errno));
36 2 : t.get_aspect().template get<_open_>()(t, filename);
37 2 : }
38 : };
39 :
40 :
41 : }}}}
42 :
43 : #endif
|