1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #include "common/errno.h"
7 #include "common/safe_io.h"
9 #include "include/types.h"
11 #include "rgw_common.h"
12 #include "rgw_rados.h"
13 #include "rgw_tools.h"
15 #define dout_subsys ceph_subsys_rgw
17 #define READ_CHUNK_LEN (512 * 1024)
19 static std::map
<std::string
, std::string
>* ext_mime_map
;
21 int rgw_put_system_obj(RGWRados
*rgwstore
, const rgw_pool
& pool
, const string
& oid
, const char *data
, size_t size
, bool exclusive
,
22 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, map
<string
, bufferlist
> *pattrs
)
24 map
<string
,bufferlist
> no_attrs
;
28 rgw_raw_obj
obj(pool
, oid
);
30 int ret
= rgwstore
->put_system_obj(NULL
, obj
, data
, size
, exclusive
, NULL
, *pattrs
, objv_tracker
, set_mtime
);
33 ret
= rgwstore
->create_pool(pool
);
35 ret
= rgwstore
->put_system_obj(NULL
, obj
, data
, size
, exclusive
, NULL
, *pattrs
, objv_tracker
, set_mtime
);
41 int rgw_get_system_obj(RGWRados
*rgwstore
, RGWObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& key
, bufferlist
& bl
,
42 RGWObjVersionTracker
*objv_tracker
, real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
,
43 rgw_cache_entry_info
*cache_info
, boost::optional
<obj_version
> refresh_version
)
45 bufferlist::iterator iter
;
46 int request_len
= READ_CHUNK_LEN
;
47 rgw_raw_obj
obj(pool
, key
);
49 obj_version original_readv
;
50 if (objv_tracker
&& !objv_tracker
->read_version
.empty()) {
51 original_readv
= objv_tracker
->read_version
;
55 RGWRados::SystemObject
source(rgwstore
, obj_ctx
, obj
);
56 RGWRados::SystemObject::Read
rop(&source
);
58 rop
.stat_params
.attrs
= pattrs
;
59 rop
.stat_params
.lastmod
= pmtime
;
61 int ret
= rop
.stat(objv_tracker
);
65 rop
.read_params
.cache_info
= cache_info
;
67 ret
= rop
.read(0, request_len
- 1, bl
, objv_tracker
, refresh_version
);
68 if (ret
== -ECANCELED
) {
70 if (!original_readv
.empty()) {
71 /* we were asked to read a specific obj_version, failed */
75 objv_tracker
->read_version
.clear();
77 source
.invalidate_state();
83 if (ret
< request_len
)
92 int rgw_delete_system_obj(RGWRados
*rgwstore
, const rgw_pool
& pool
, const string
& oid
,
93 RGWObjVersionTracker
*objv_tracker
)
95 rgw_raw_obj
obj(pool
, oid
);
96 return rgwstore
->delete_system_obj(obj
, objv_tracker
);
99 void parse_mime_map_line(const char *start
, const char *end
)
101 char line
[end
- start
+ 1];
102 strncpy(line
, start
, end
- start
);
103 line
[end
- start
] = '\0';
105 #define DELIMS " \t\n\r"
110 char *mime
= strsep(&l
, DELIMS
);
116 ext
= strsep(&l
, DELIMS
);
118 (*ext_mime_map
)[ext
] = mime
;
124 void parse_mime_map(const char *buf
)
126 const char *start
= buf
, *end
= buf
;
128 while (*end
&& *end
!= '\n') {
131 parse_mime_map_line(start
, end
);
137 static int ext_mime_map_init(CephContext
*cct
, const char *ext_map
)
139 int fd
= open(ext_map
, O_RDONLY
);
144 ldout(cct
, 0) << __func__
<< " failed to open file=" << ext_map
145 << " : " << cpp_strerror(-ret
) << dendl
;
150 ret
= fstat(fd
, &st
);
153 ldout(cct
, 0) << __func__
<< " failed to stat file=" << ext_map
154 << " : " << cpp_strerror(-ret
) << dendl
;
158 buf
= (char *)malloc(st
.st_size
+ 1);
161 ldout(cct
, 0) << __func__
<< " failed to allocate buf" << dendl
;
165 ret
= safe_read(fd
, buf
, st
.st_size
+ 1);
166 if (ret
!= st
.st_size
) {
167 // huh? file size has changed?
168 ldout(cct
, 0) << __func__
<< " raced! will retry.." << dendl
;
171 return ext_mime_map_init(cct
, ext_map
);
173 buf
[st
.st_size
] = '\0';
183 const char *rgw_find_mime_by_ext(string
& ext
)
185 map
<string
, string
>::iterator iter
= ext_mime_map
->find(ext
);
186 if (iter
== ext_mime_map
->end())
189 return iter
->second
.c_str();
192 int rgw_tools_init(CephContext
*cct
)
194 ext_mime_map
= new std::map
<std::string
, std::string
>;
195 ext_mime_map_init(cct
, cct
->_conf
->rgw_mime_types_file
.c_str());
196 // ignore errors; missing mime.types is not fatal
200 void rgw_tools_cleanup()
203 ext_mime_map
= nullptr;