~ubuntu-branches/ubuntu/raring/ceph/raring

« back to all changes in this revision

Viewing changes to src/rgw/rgw_tools.cc

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2012-02-05 10:07:38 UTC
  • mfrom: (1.1.7) (0.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120205100738-00s0bxx93mamy8tk
Tags: 0.41-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
#define READ_CHUNK_LEN (16 * 1024)
14
14
 
15
 
int rgw_put_obj(string& uid, rgw_bucket& bucket, string& oid, const char *data, size_t size)
 
15
static map<string, string> ext_mime_map;
 
16
 
 
17
int rgw_put_obj(string& uid, rgw_bucket& bucket, string& oid, const char *data, size_t size, bool exclusive, map<string, bufferlist> *pattrs)
16
18
{
17
 
  map<string,bufferlist> attrs;
 
19
  map<string,bufferlist> no_attrs;
 
20
  if (!pattrs)
 
21
    pattrs = &no_attrs;
18
22
 
19
23
  rgw_obj obj(bucket, oid);
20
24
 
21
 
  int ret = rgwstore->put_obj(NULL, obj, data, size, NULL, attrs);
 
25
  int ret = rgwstore->put_obj(NULL, obj, data, size, exclusive, NULL, *pattrs);
22
26
 
23
27
  if (ret == -ENOENT) {
24
 
    ret = rgwstore->create_bucket(uid, bucket, attrs, true); //all callers are using system buckets
 
28
    ret = rgwstore->create_bucket(uid, bucket, no_attrs, true); //all callers are using system buckets
25
29
    if (ret >= 0)
26
 
      ret = rgwstore->put_obj(NULL, obj, data, size, NULL, attrs);
 
30
      ret = rgwstore->put_obj(NULL, obj, data, size, exclusive, NULL, *pattrs);
27
31
  }
28
32
 
29
33
  return ret;
38
42
  bufferlist::iterator iter;
39
43
  int request_len = READ_CHUNK_LEN;
40
44
  rgw_obj obj(bucket, key);
41
 
  ret = rgwstore->prepare_get_obj(ctx, obj, 0, NULL, NULL, NULL,
 
45
  ret = rgwstore->prepare_get_obj(ctx, obj, NULL, NULL, NULL, NULL,
42
46
                                  NULL, NULL, NULL, NULL, NULL, NULL, &handle, &err);
43
47
  if (ret < 0)
44
48
    return ret;
62
66
  return ret;
63
67
}
64
68
 
65
 
 
 
69
void parse_mime_map_line(const char *start, const char *end)
 
70
{
 
71
  char line[end - start + 1];
 
72
  strncpy(line, start, end - start);
 
73
  line[end - start] = '\0';
 
74
  char *l = line;
 
75
#define DELIMS " \t\n\r"
 
76
 
 
77
  while (isspace(*l))
 
78
    l++;
 
79
 
 
80
  char *mime = strsep(&l, DELIMS);
 
81
  if (!mime)
 
82
    return;
 
83
 
 
84
  char *ext;
 
85
  do {
 
86
    ext = strsep(&l, DELIMS);
 
87
    if (ext && *ext) {
 
88
      ext_mime_map[ext] = mime;
 
89
    }
 
90
  } while (ext);
 
91
}
 
92
 
 
93
 
 
94
void parse_mime_map(const char *buf)
 
95
{
 
96
  const char *start = buf, *end = buf;
 
97
  while (*end) {
 
98
    while (*end && *end != '\n') {
 
99
      end++;
 
100
    }
 
101
    parse_mime_map_line(start, end);
 
102
    end++;
 
103
    start = end;
 
104
  }
 
105
}
 
106
 
 
107
static int ext_mime_map_init(const char *ext_map)
 
108
{
 
109
  int fd = open(ext_map, O_RDONLY);
 
110
  char *buf = NULL;
 
111
  int ret;
 
112
  if (fd < 0) {
 
113
    ret = -errno;
 
114
    dout(0) << "ext_mime_map_init(): failed to open file=" << ext_map << " ret=" << ret << dendl;
 
115
    return ret;
 
116
  }
 
117
 
 
118
  struct stat st;
 
119
  ret = fstat(fd, &st);
 
120
  if (ret < 0) {
 
121
    ret = -errno;
 
122
    dout(0) << "ext_mime_map_init(): failed to stat file=" << ext_map << " ret=" << ret << dendl;
 
123
    goto done;
 
124
  }
 
125
 
 
126
  buf = (char *)malloc(st.st_size + 1);
 
127
  if (!buf) {
 
128
    ret = -ENOMEM;
 
129
    dout(0) << "ext_mime_map_init(): failed to allocate buf" << dendl;
 
130
    goto done;
 
131
  }
 
132
 
 
133
  ret = read(fd, buf, st.st_size + 1);
 
134
  if (ret != st.st_size) {
 
135
    // huh? file size has changed, what are the odds?
 
136
    dout(0) << "ext_mime_map_init(): raced! will retry.." << dendl;
 
137
    close(fd);
 
138
    return ext_mime_map_init(ext_map);
 
139
  }
 
140
  buf[st.st_size] = '\0';
 
141
 
 
142
  parse_mime_map(buf);
 
143
  ret = 0;
 
144
done:
 
145
  free(buf);
 
146
  close(fd);
 
147
  return ret;
 
148
}
 
149
 
 
150
const char *rgw_find_mime_by_ext(string& ext)
 
151
{
 
152
  map<string, string>::iterator iter = ext_mime_map.find(ext);
 
153
  if (iter == ext_mime_map.end())
 
154
    return NULL;
 
155
 
 
156
  return iter->second.c_str();
 
157
}
 
158
 
 
159
int rgw_tools_init(CephContext *cct)
 
160
{
 
161
  int ret = ext_mime_map_init(cct->_conf->rgw_mime_types_file.c_str());
 
162
  if (ret < 0)
 
163
    return ret;
 
164
 
 
165
  return 0;
 
166
}