OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_ppb_file_ref.h" |
| 6 |
| 7 #include <string.h> |
| 8 #include <cmath> |
| 9 #include <string> |
| 10 |
| 11 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 12 #include "native_client/src/include/portability.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/object_serialize.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/plugin_callback.h" |
| 15 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 16 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 17 #include "ppapi/c/pp_completion_callback.h" |
| 18 #include "ppapi/c/pp_errors.h" |
| 19 #include "srpcgen/ppb_rpc.h" |
| 20 |
| 21 namespace ppapi_proxy { |
| 22 |
| 23 namespace { |
| 24 bool IsDirectoryTraversal(const std::string& path) { |
| 25 bool is_directory_traversal = true; |
| 26 // TODO(sanga): Do we need to guard against percent encoded strings as well? |
| 27 if (path.find("../") == std::string::npos) |
| 28 is_directory_traversal = false; |
| 29 return is_directory_traversal; |
| 30 } |
| 31 |
| 32 PP_Resource Create(PP_Resource file_system, |
| 33 const char* path) { |
| 34 DebugPrintf("PPB_FileRef::Create: file_system=%"NACL_PRIu32", path=%s\n", |
| 35 file_system, path); |
| 36 |
| 37 PP_Resource resource = kInvalidResourceId; |
| 38 |
| 39 if (IsDirectoryTraversal(path)) |
| 40 return kInvalidResourceId; |
| 41 |
| 42 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_Create( |
| 43 GetMainSrpcChannel(), |
| 44 file_system, |
| 45 strlen(path) + 1, // Add 1 to preserve terminating '\0' |
| 46 const_cast<char*>(path), |
| 47 &resource); |
| 48 DebugPrintf("PPB_FileRef::Create: %s\n", NaClSrpcErrorString(srpc_result)); |
| 49 |
| 50 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 51 return resource; |
| 52 return kInvalidResourceId; |
| 53 } |
| 54 |
| 55 PP_Bool IsFileRef(PP_Resource file_ref) { |
| 56 DebugPrintf("PPB_FileRef::IsFileRef: file_ref=%"NACL_PRIu32"\n", file_ref); |
| 57 |
| 58 int32_t is_file_ref = 0; |
| 59 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_IsFileRef( |
| 60 GetMainSrpcChannel(), |
| 61 file_ref, |
| 62 &is_file_ref); |
| 63 DebugPrintf("PPB_FileRef::IsFileRef: %s\n", NaClSrpcErrorString(srpc_result)); |
| 64 |
| 65 if ((srpc_result == NACL_SRPC_RESULT_OK) && is_file_ref) |
| 66 return PP_TRUE; |
| 67 return PP_FALSE; |
| 68 } |
| 69 |
| 70 PP_FileSystemType GetFileSystemType(PP_Resource file_ref) { |
| 71 DebugPrintf("PPB_FileRef::GetFileSystemType: file_ref=%"NACL_PRIu32"\n", |
| 72 file_ref); |
| 73 |
| 74 int32_t file_system_type = PP_FILESYSTEMTYPE_INVALID; |
| 75 NaClSrpcError srpc_result = |
| 76 PpbFileRefRpcClient::PPB_FileRef_GetFileSystemType( |
| 77 GetMainSrpcChannel(), |
| 78 file_ref, |
| 79 &file_system_type); |
| 80 DebugPrintf("PPB_FileRef::GetFileSystemType: %s\n", |
| 81 NaClSrpcErrorString(srpc_result)); |
| 82 |
| 83 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 84 return static_cast<PP_FileSystemType>(file_system_type); |
| 85 return PP_FILESYSTEMTYPE_INVALID; |
| 86 } |
| 87 |
| 88 PP_Var GetName(PP_Resource file_ref) { |
| 89 DebugPrintf("PPB_FileRef::GetName: file_ref=%"NACL_PRIu32"\n", file_ref); |
| 90 |
| 91 PP_Var name = PP_MakeUndefined(); |
| 92 nacl_abi_size_t length = kMaxVarSize; |
| 93 nacl::scoped_array<char> name_bytes(new char[length]); |
| 94 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_GetName( |
| 95 GetMainSrpcChannel(), |
| 96 file_ref, |
| 97 &length, |
| 98 name_bytes.get()); |
| 99 DebugPrintf("PPB_FileRef::GetName: %s\n", NaClSrpcErrorString(srpc_result)); |
| 100 |
| 101 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 102 if (DeserializeTo(GetMainSrpcChannel(), name_bytes.get(), length, |
| 103 1, // argc |
| 104 &name)) |
| 105 return name; |
| 106 } |
| 107 return PP_MakeUndefined(); |
| 108 } |
| 109 |
| 110 PP_Var GetPath(PP_Resource file_ref) { |
| 111 DebugPrintf("PPB_FileRef::GetPath: file_ref=%"NACL_PRIu32"\n", file_ref); |
| 112 |
| 113 PP_Var path = PP_MakeUndefined(); |
| 114 nacl_abi_size_t length = kMaxVarSize; |
| 115 nacl::scoped_array<char> path_bytes(new char[length]); |
| 116 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_GetPath( |
| 117 GetMainSrpcChannel(), |
| 118 file_ref, |
| 119 &length, |
| 120 path_bytes.get()); |
| 121 DebugPrintf("PPB_FileRef::GetPath: %s\n", |
| 122 NaClSrpcErrorString(srpc_result)); |
| 123 |
| 124 if (srpc_result == NACL_SRPC_RESULT_OK && |
| 125 DeserializeTo(GetMainSrpcChannel(), path_bytes.get(), length, |
| 126 1, // argc |
| 127 &path)) { |
| 128 return path; |
| 129 } |
| 130 return PP_MakeUndefined(); |
| 131 } |
| 132 |
| 133 PP_Resource GetParent(PP_Resource file_ref) { |
| 134 DebugPrintf("PPB_FileRef::GetParent: file_ref=%"NACL_PRIu32"\n", file_ref); |
| 135 |
| 136 PP_Resource parent = kInvalidResourceId; |
| 137 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_GetParent( |
| 138 GetMainSrpcChannel(), |
| 139 file_ref, |
| 140 &parent); |
| 141 DebugPrintf("PPB_FileRef::GetParent: %s\n", |
| 142 NaClSrpcErrorString(srpc_result)); |
| 143 |
| 144 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 145 return parent; |
| 146 return kInvalidResourceId; |
| 147 } |
| 148 |
| 149 int32_t MakeDirectory(PP_Resource directory_ref, |
| 150 PP_Bool make_ancestors, |
| 151 struct PP_CompletionCallback callback) { |
| 152 DebugPrintf("PPB_FileRef::MakeDirectory: directory_ref=%"NACL_PRIu32", " |
| 153 "make_ancestors=%s\n", directory_ref, |
| 154 (make_ancestors ? "true" : "false")); |
| 155 |
| 156 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback); |
| 157 int32_t pp_error = PP_ERROR_FAILED; |
| 158 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_MakeDirectory( |
| 159 GetMainSrpcChannel(), |
| 160 directory_ref, |
| 161 make_ancestors, |
| 162 callback_id, |
| 163 &pp_error); |
| 164 DebugPrintf("PPB_FileRef::MakeDirectory: %s\n", |
| 165 NaClSrpcErrorString(srpc_result)); |
| 166 |
| 167 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 168 return MayForceCallback(callback, pp_error); |
| 169 return PP_ERROR_FAILED; |
| 170 } |
| 171 |
| 172 int32_t Touch(PP_Resource file_ref, |
| 173 PP_Time last_access_time, |
| 174 PP_Time last_modified_time, |
| 175 struct PP_CompletionCallback callback) { |
| 176 DebugPrintf("PPB_FileRef::Touch: file_ref=%"NACL_PRIu32", " |
| 177 "last_access_time=%lf, last_modified_time=%lf\n", |
| 178 file_ref, last_access_time, last_modified_time); |
| 179 |
| 180 if (std::isnan(last_access_time) || |
| 181 std::isnan(last_modified_time)) { |
| 182 return PP_ERROR_FAILED; |
| 183 } |
| 184 |
| 185 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback); |
| 186 int32_t pp_error = PP_ERROR_FAILED; |
| 187 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_Touch( |
| 188 GetMainSrpcChannel(), |
| 189 file_ref, |
| 190 last_access_time, |
| 191 last_modified_time, |
| 192 callback_id, |
| 193 &pp_error); |
| 194 DebugPrintf("PPB_FileRef::Touch: %s\n", NaClSrpcErrorString(srpc_result)); |
| 195 |
| 196 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 197 return MayForceCallback(callback, pp_error); |
| 198 return PP_ERROR_FAILED; |
| 199 } |
| 200 |
| 201 int32_t Delete(PP_Resource file_ref, |
| 202 struct PP_CompletionCallback callback) { |
| 203 DebugPrintf("PPB_FileRef::Delete: file_ref=%"NACL_PRIu32"\n", file_ref); |
| 204 |
| 205 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback); |
| 206 int32_t pp_error = PP_ERROR_FAILED; |
| 207 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_Delete( |
| 208 GetMainSrpcChannel(), |
| 209 file_ref, |
| 210 callback_id, |
| 211 &pp_error); |
| 212 DebugPrintf("PPB_FileRef::Delete: %s\n", NaClSrpcErrorString(srpc_result)); |
| 213 |
| 214 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 215 return MayForceCallback(callback, pp_error); |
| 216 return PP_ERROR_FAILED; |
| 217 } |
| 218 |
| 219 int32_t Rename(PP_Resource file_ref, |
| 220 PP_Resource new_file_ref, |
| 221 struct PP_CompletionCallback callback) { |
| 222 DebugPrintf("PPB_FileRef::Rename: file_ref=%"NACL_PRIu32", " |
| 223 "new_file_ref=%"NACL_PRIu32"\n", file_ref, new_file_ref); |
| 224 |
| 225 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback); |
| 226 int32_t pp_error = PP_ERROR_FAILED; |
| 227 NaClSrpcError srpc_result = PpbFileRefRpcClient::PPB_FileRef_Rename( |
| 228 GetMainSrpcChannel(), |
| 229 file_ref, |
| 230 new_file_ref, |
| 231 callback_id, |
| 232 &pp_error); |
| 233 DebugPrintf("PPB_FileRef::Rename: %s\n", NaClSrpcErrorString(srpc_result)); |
| 234 |
| 235 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 236 return MayForceCallback(callback, pp_error); |
| 237 return PP_ERROR_FAILED; |
| 238 } |
| 239 } // namespace |
| 240 |
| 241 const PPB_FileRef* PluginFileRef::GetInterface() { |
| 242 static const PPB_FileRef file_ref_interface = { |
| 243 Create, |
| 244 IsFileRef, |
| 245 GetFileSystemType, |
| 246 GetName, |
| 247 GetPath, |
| 248 GetParent, |
| 249 MakeDirectory, |
| 250 Touch, |
| 251 Delete, |
| 252 Rename |
| 253 }; |
| 254 return &file_ref_interface; |
| 255 } |
| 256 } // namespace ppapi_proxy |
OLD | NEW |