Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/googledrivefs/googledrivefs.cc

Issue 2156503002: [NaCl SDK] Expose Google Drive to nacl_io. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "nacl_io/googledrivefs/googledrivefs.h"
6
7 #include <stdint.h>
8
9 #include <limits>
10
11 #include "nacl_io/filesystem.h"
12 #include "nacl_io/statuscode.h"
13 #include "nacl_io/googledrivefs/googledrivefs_node.h"
14 #include "nacl_io/googledrivefs/googledrivefs_util.h"
15
16 namespace nacl_io {
17
18 GoogleDriveFs::GoogleDriveFs() {}
19
20 Error GoogleDriveFs::Init(const FsInitArgs& args) {
21 Error error = Filesystem::Init(args);
22 if (error) {
23 return error;
24 }
25
26 if (!ppapi()) {
27 return ENOSYS;
28 }
29
30 StringMap_t::const_iterator token_it = args.string_map.find("token");
31
32 if (token_it == args.string_map.end()) {
33 return EINVAL;
34 }
35
36 token_ = token_it->second;
37
38 return 0;
39 }
40
41 Error GoogleDriveFs::OpenWithMode(const Path& path,
42 int open_flags,
43 mode_t mode,
44 ScopedNode* out_node) {
45 ScopedNode node(new GoogleDriveFsNode(this, path));
46 Error error = node->Init(open_flags);
47 if (error) {
48 return error;
49 }
50
51 *out_node = node;
52
53 return 0;
54 }
55
56 Error GoogleDriveFs::Mkdir(const Path& path, int permissions) {
57 if (path.IsRoot()) {
58 return EEXIST;
59 }
60
61 std::string parent_dir_id;
62 Error error = RequestParentDirId(path, static_cast<GoogleDriveFs*>(this),
63 &parent_dir_id);
64 if (error) {
65 return error;
66 }
67
68 std::string item_id;
69 bool is_dir_type;
70 error = RequestItemIdAndItemType(parent_dir_id, path.Basename(),
71 static_cast<GoogleDriveFs*>(this), &item_id,
72 &is_dir_type);
73
74 // mkdir does not create a directory when a directory or a file with
75 // path.Basename() already exists.
76 if (!error) {
77 return EEXIST;
78 } else if (error != ENOENT) {
79 return error;
80 }
81
82 RequestUrlParams p;
83
84 p.url = DRIVE_URL;
85 p.method = "POST";
86
87 AddHeaders("Content-type", "application/json", &p.headers);
88 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
89
90 AddBody("{", &p.body);
91 AddBody(" \"name\": \"" + path.Basename() + "\",", &p.body);
92 AddBody(
93 std::string(" \"mimeType\": \"") + FOLDER_MIME_TYPE + std::string("\","),
94 &p.body);
95 AddBody(" \"parents\": [", &p.body);
96 AddBody(" \"" + parent_dir_id + "\"", &p.body);
97 AddBody(" ]", &p.body);
98 AddBody("}", &p.body);
99
100 ScopedResource url_response_info_resource(ppapi());
101 error = LoadUrl(ppapi(), p, &url_response_info_resource);
102 if (error) {
103 return error;
104 }
105
106 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
107 STATUSCODE_OK) {
108 return EPERM;
109 }
110
111 return 0;
112 }
113
114 Error GoogleDriveFs::Rmdir(const Path& path) {
115 if (path.IsRoot()) {
116 return EEXIST;
117 }
118
119 std::string parent_dir_id;
120 Error error = RequestParentDirId(path, static_cast<GoogleDriveFs*>(this),
121 &parent_dir_id);
122 if (error) {
123 return error;
124 }
125
126 std::string item_id;
127 bool is_dir_type;
128 error = RequestItemIdAndItemType(parent_dir_id, path.Basename(),
129 static_cast<GoogleDriveFs*>(this), &item_id,
130 &is_dir_type);
131 if (error) {
132 return error;
133 }
134
135 if (!is_dir_type) {
136 return ENOTDIR;
137 }
138
139 bool is_empty_dir;
140 error = IsEmptyDir(item_id, &is_empty_dir);
141 if (error) {
142 return error;
143 }
144
145 if (!is_empty_dir) {
146 return ENOTEMPTY;
147 }
148
149 RequestUrlParams p;
150
151 p.url = DRIVE_URL;
152 AddUrlPath(item_id, &p.url);
153
154 p.method = "DELETE";
155
156 AddHeaders("Content-type", "application/json", &p.headers);
157 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
158
159 ScopedResource url_response_info_resource(ppapi());
160 error = LoadUrl(ppapi(), p, &url_response_info_resource);
161 if (error) {
162 return error;
163 }
164
165 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
166 STATUSCODE_NO_CONTENT) {
167 return EPERM;
168 }
169
170 return 0;
171 }
172
173 Error GoogleDriveFs::Rename(const Path& path, const Path& newPath) {
174 // TODO: support rename
175 LOG_ERROR("rename not supported.");
176 return EPERM;
177 }
178
179 Error GoogleDriveFs::Unlink(const Path& path) {
180 // TODO: support unlink
181 LOG_ERROR("unlink not supported.");
182 return EPERM;
183 }
184
185 Error GoogleDriveFs::Remove(const Path& path) {
186 // TODO: support remove
187 LOG_ERROR("remove not supported.");
188 return EPERM;
189 }
190
191 Error GoogleDriveFs::IsEmptyDir(const std::string& dir_id,
192 bool* out_is_empty_dir) {
193 RequestUrlParams p;
194
195 p.url = DRIVE_URL;
196 AddUrlFirstQueryParameter("q", ParentEqualClause(dir_id), &p.url);
197
198 p.method = "GET";
199
200 AddHeaders("Content-type", "application/json", &p.headers);
201 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
202
203 ScopedResource url_response_info_resource(ppapi());
204 Error error = LoadUrl(ppapi(), p, &url_response_info_resource);
205 if (error) {
206 return error;
207 }
208
209 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
210 STATUSCODE_OK) {
211 return EPERM;
212 }
213
214 std::string output;
215 error = ReadResponseBody(ppapi(), url_response_info_resource.pp_resource(),
216 std::numeric_limits<int32_t>::max(), &output);
217 if (error) {
218 return error;
219 }
220
221 std::string name_value;
222 size_t name_index;
223 error =
224 GetValueStringAndValuePos(output, "name", 0, &name_value, &name_index);
225 if (error && error != EINVAL) {
226 return error;
227 }
228
229 *out_is_empty_dir = (error == EINVAL);
230
231 return 0;
232 }
233
234 std::string GoogleDriveFs::token() {
235 return token_;
236 }
237
238 } // namespace nacl_io
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698