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

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, this, &parent_dir_id);
63 if (error) {
64 return error;
65 }
66
67 std::string item_id;
68 bool is_dir_type;
69 error = RequestItemIdAndItemType(parent_dir_id, path.Basename(), this,
70 &item_id, &is_dir_type);
71
72 // mkdir does not create a directory when a directory or a file with
73 // path.Basename() already exists.
74 if (!error) {
75 return EEXIST;
76 } else if (error != ENOENT) {
77 return error;
78 }
79
80 RequestUrlParams p;
81
82 p.url = DRIVE_URL;
83 p.method = "POST";
84
85 AddHeaders("Content-type", "application/json", &p.headers);
86 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
87
88 AddBody("{", &p.body);
89 AddBody(" \"name\": \"" + path.Basename() + "\",", &p.body);
90 AddBody(
91 std::string(" \"mimeType\": \"") + FOLDER_MIME_TYPE + std::string("\","),
92 &p.body);
93 AddBody(" \"parents\": [", &p.body);
94 AddBody(" \"" + parent_dir_id + "\"", &p.body);
95 AddBody(" ]", &p.body);
96 AddBody("}", &p.body);
97
98 ScopedResource url_response_info_resource(ppapi());
99 error = LoadUrl(ppapi(), p, &url_response_info_resource);
100 if (error) {
101 return error;
102 }
103
104 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
105 STATUSCODE_OK) {
106 return EPERM;
107 }
108
109 return 0;
110 }
111
112 Error GoogleDriveFs::Rmdir(const Path& path) {
113 if (path.IsRoot()) {
114 return EEXIST;
115 }
116
117 std::string parent_dir_id;
118 Error error = RequestParentDirId(path, this, &parent_dir_id);
119 if (error) {
120 return error;
121 }
122
123 std::string item_id;
124 bool is_dir_type;
125 error = RequestItemIdAndItemType(parent_dir_id, path.Basename(), this,
126 &item_id, &is_dir_type);
127 if (error) {
128 return error;
129 }
130
131 if (!is_dir_type) {
132 return ENOTDIR;
133 }
134
135 bool is_empty_dir;
136 error = IsEmptyDir(item_id, &is_empty_dir);
137 if (error) {
138 return error;
139 }
140
141 if (!is_empty_dir) {
142 return ENOTEMPTY;
143 }
144
145 RequestUrlParams p;
146
147 p.url = DRIVE_URL;
148 AddUrlPath(item_id, &p.url);
149
150 p.method = "DELETE";
151
152 AddHeaders("Content-type", "application/json", &p.headers);
153 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
154
155 ScopedResource url_response_info_resource(ppapi());
156 error = LoadUrl(ppapi(), p, &url_response_info_resource);
157 if (error) {
158 return error;
159 }
160
161 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
162 STATUSCODE_NO_CONTENT) {
163 return EPERM;
164 }
165
166 return 0;
167 }
168
169 Error GoogleDriveFs::Rename(const Path& path, const Path& newPath) {
170 // TODO: support rename
171 LOG_ERROR("rename not supported.");
172 return EPERM;
173 }
174
175 Error GoogleDriveFs::Unlink(const Path& path) {
176 // TODO: support unlink
177 LOG_ERROR("unlink not supported.");
178 return EPERM;
179 }
180
181 Error GoogleDriveFs::Remove(const Path& path) {
182 // TODO: support remove
183 LOG_ERROR("remove not supported.");
184 return EPERM;
185 }
186
187 Error GoogleDriveFs::IsEmptyDir(const std::string& dir_id,
188 bool* out_is_empty_dir) {
189 RequestUrlParams p;
190
191 p.url = DRIVE_URL;
192 AddUrlFirstQueryParameter("q", ParentEqualClause(dir_id), &p.url);
193
194 p.method = "GET";
195
196 AddHeaders("Content-type", "application/json", &p.headers);
197 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
198
199 ScopedResource url_response_info_resource(ppapi());
200 Error error = LoadUrl(ppapi(), p, &url_response_info_resource);
201 if (error) {
202 return error;
203 }
204
205 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
206 STATUSCODE_OK) {
207 return EPERM;
208 }
209
210 std::string output;
211 error = ReadResponseBody(ppapi(), url_response_info_resource.pp_resource(),
212 std::numeric_limits<int32_t>::max(), &output);
213 if (error) {
214 return error;
215 }
216
217 std::string name_value;
218 size_t name_index;
219 error =
220 GetValueStringAndValuePos(output, "name", 0, &name_value, &name_index);
221 if (error && error != EINVAL) {
222 return error;
223 }
224
225 *out_is_empty_dir = (error == EINVAL);
226
227 return 0;
228 }
229
230 std::string GoogleDriveFs::token() {
231 return token_;
232 }
233
234 } // namespace nacl_io
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698