| OLD | NEW |
| 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #include "nacl_mounts/mount_http.h" | 6 #include "nacl_mounts/mount_http.h" |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <ctype.h> | 8 #include <ctype.h> |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <fcntl.h> | 10 #include <fcntl.h> |
| 11 #include <ppapi/c/pp_errors.h> | 11 #include <ppapi/c/pp_errors.h> |
| 12 #include <stdio.h> | 12 #include <stdio.h> |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 #include <sys/stat.h> | 14 #include <sys/stat.h> |
| 15 #include <sys/types.h> | 15 #include <sys/types.h> |
| 16 #include <vector> | 16 #include <vector> |
| 17 #include "nacl_mounts/mount_node_dir.h" | 17 #include "nacl_mounts/mount_node_dir.h" |
| 18 #include "nacl_mounts/osinttypes.h" | 18 #include "nacl_mounts/osinttypes.h" |
| 19 #include "utils/auto_lock.h" | 19 #include "utils/auto_lock.h" |
| 20 | 20 |
| 21 #if defined(WIN32) | 21 #if defined(WIN32) |
| 22 #define snprintf _snprintf | 22 #define snprintf _snprintf |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 static const int USR_ID = 1001; | |
| 26 static const int GRP_ID = 1002; | |
| 27 | |
| 28 | 25 |
| 29 typedef std::vector<char *> StringList_t; | 26 typedef std::vector<char *> StringList_t; |
| 30 static size_t SplitString(char *str, const char *delim, StringList_t* list) { | 27 static size_t SplitString(char *str, const char *delim, StringList_t* list) { |
| 31 char *item = strtok(str, delim); | 28 char *item = strtok(str, delim); |
| 32 | 29 |
| 33 list->clear(); | 30 list->clear(); |
| 34 while (item) { | 31 while (item) { |
| 35 list->push_back(item); | 32 list->push_back(item); |
| 36 item = strtok(NULL, delim); | 33 item = strtok(NULL, delim); |
| 37 } | 34 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 public: | 153 public: |
| 157 virtual int FSync(); | 154 virtual int FSync(); |
| 158 virtual int GetDents(size_t offs, struct dirent* pdir, size_t count); | 155 virtual int GetDents(size_t offs, struct dirent* pdir, size_t count); |
| 159 virtual int GetStat(struct stat* stat); | 156 virtual int GetStat(struct stat* stat); |
| 160 virtual int Read(size_t offs, void* buf, size_t count); | 157 virtual int Read(size_t offs, void* buf, size_t count); |
| 161 virtual int Truncate(size_t size); | 158 virtual int Truncate(size_t size); |
| 162 virtual int Write(size_t offs, const void* buf, size_t count); | 159 virtual int Write(size_t offs, const void* buf, size_t count); |
| 163 virtual size_t GetSize(); | 160 virtual size_t GetSize(); |
| 164 | 161 |
| 165 protected: | 162 protected: |
| 166 MountNodeHttp(Mount* mount, int ino, int dev, const std::string& url); | 163 MountNodeHttp(Mount* mount, const std::string& url); |
| 167 virtual int Close(); | |
| 168 | 164 |
| 169 private: | 165 private: |
| 170 bool OpenUrl(const char* method, | 166 bool OpenUrl(const char* method, |
| 171 StringMap_t* request_headers, | 167 StringMap_t* request_headers, |
| 172 PP_Resource* out_loader, | 168 PP_Resource* out_loader, |
| 173 PP_Resource* out_request, | 169 PP_Resource* out_request, |
| 174 PP_Resource* out_response, | 170 PP_Resource* out_response, |
| 175 int32_t* out_statuscode, | 171 int32_t* out_statuscode, |
| 176 StringMap_t* out_response_headers); | 172 StringMap_t* out_response_headers); |
| 177 | 173 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 errno = ENOSYS; | 352 errno = ENOSYS; |
| 357 return -1; | 353 return -1; |
| 358 } | 354 } |
| 359 | 355 |
| 360 size_t MountNodeHttp::GetSize() { | 356 size_t MountNodeHttp::GetSize() { |
| 361 // TODO(binji): This value should be cached properly; i.e. obey the caching | 357 // TODO(binji): This value should be cached properly; i.e. obey the caching |
| 362 // headers returned by the server. | 358 // headers returned by the server. |
| 363 return stat_.st_size; | 359 return stat_.st_size; |
| 364 } | 360 } |
| 365 | 361 |
| 366 MountNodeHttp::MountNodeHttp(Mount* mount, int ino, int dev, | 362 MountNodeHttp::MountNodeHttp(Mount* mount, const std::string& url) |
| 367 const std::string& url) | 363 : MountNode(mount), |
| 368 : MountNode(mount, ino, dev), | |
| 369 url_(url) { | 364 url_(url) { |
| 370 } | 365 } |
| 371 | 366 |
| 372 | |
| 373 int MountNodeHttp::Close() { | |
| 374 return 0; | |
| 375 } | |
| 376 | |
| 377 bool MountNodeHttp::OpenUrl(const char* method, | 367 bool MountNodeHttp::OpenUrl(const char* method, |
| 378 StringMap_t* request_headers, | 368 StringMap_t* request_headers, |
| 379 PP_Resource* out_loader, | 369 PP_Resource* out_loader, |
| 380 PP_Resource* out_request, | 370 PP_Resource* out_request, |
| 381 PP_Resource* out_response, | 371 PP_Resource* out_response, |
| 382 int32_t* out_statuscode, | 372 int32_t* out_statuscode, |
| 383 StringMap_t* out_response_headers) { | 373 StringMap_t* out_response_headers) { |
| 384 // Assume lock_ is already held. | 374 // Assume lock_ is already held. |
| 385 | 375 |
| 386 PepperInterface* ppapi = mount_->ppapi(); | 376 PepperInterface* ppapi = mount_->ppapi(); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 NodeMap_t::iterator iter = node_cache_.find(path.Join()); | 454 NodeMap_t::iterator iter = node_cache_.find(path.Join()); |
| 465 if (iter != node_cache_.end()) { | 455 if (iter != node_cache_.end()) { |
| 466 return iter->second; | 456 return iter->second; |
| 467 } | 457 } |
| 468 | 458 |
| 469 // If we can't find the node in the cache, create it | 459 // If we can't find the node in the cache, create it |
| 470 std::string url = url_root_ + (path.IsAbsolute() ? | 460 std::string url = url_root_ + (path.IsAbsolute() ? |
| 471 path.Range(1, path.Size()) : | 461 path.Range(1, path.Size()) : |
| 472 path.Join()); | 462 path.Join()); |
| 473 | 463 |
| 474 MountNodeHttp* node = new MountNodeHttp(this, num_nodes_++, dev_, url); | 464 MountNodeHttp* node = new MountNodeHttp(this, url); |
| 475 if (!node->Init(mode, USR_ID, GRP_ID) || (0 != node->GetStat(NULL))) { | 465 if (!node->Init(mode) || (0 != node->GetStat(NULL))) { |
| 476 node->Release(); | 466 node->Release(); |
| 477 return NULL; | 467 return NULL; |
| 478 } | 468 } |
| 479 | 469 |
| 480 MountNodeDir* parent = FindOrCreateDir(path.Parent()); | 470 MountNodeDir* parent = FindOrCreateDir(path.Parent()); |
| 481 node_cache_[path.Join()] = node; | 471 node_cache_[path.Join()] = node; |
| 482 parent->AddChild(path.Basename(), node); | 472 parent->AddChild(path.Basename(), node); |
| 483 return node; | 473 return node; |
| 484 } | 474 } |
| 485 | 475 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 } | 583 } |
| 594 | 584 |
| 595 MountNodeDir* MountHttp::FindOrCreateDir(const Path& path) { | 585 MountNodeDir* MountHttp::FindOrCreateDir(const Path& path) { |
| 596 std::string strpath = path.Join(); | 586 std::string strpath = path.Join(); |
| 597 NodeMap_t::iterator iter = node_cache_.find(strpath); | 587 NodeMap_t::iterator iter = node_cache_.find(strpath); |
| 598 if (iter != node_cache_.end()) { | 588 if (iter != node_cache_.end()) { |
| 599 return static_cast<MountNodeDir*>(iter->second); | 589 return static_cast<MountNodeDir*>(iter->second); |
| 600 } | 590 } |
| 601 | 591 |
| 602 // If the node does not exist, create it, and add it to the node cache | 592 // If the node does not exist, create it, and add it to the node cache |
| 603 MountNodeDir* node = new MountNodeDir(this, num_nodes_, dev_); | 593 MountNodeDir* node = new MountNodeDir(this); |
| 604 node->Init(S_IREAD, USR_ID, GRP_ID); | 594 node->Init(S_IREAD); |
| 605 node_cache_[strpath] = node; | 595 node_cache_[strpath] = node; |
| 606 | 596 |
| 607 // If not the root node, find the parent node and add it to the parent | 597 // If not the root node, find the parent node and add it to the parent |
| 608 if (!path.Top()) { | 598 if (!path.Top()) { |
| 609 MountNodeDir* parent = FindOrCreateDir(path.Parent()); | 599 MountNodeDir* parent = FindOrCreateDir(path.Parent()); |
| 610 parent->AddChild(path.Basename(), node); | 600 parent->AddChild(path.Basename(), node); |
| 611 } | 601 } |
| 612 | 602 |
| 613 return node; | 603 return node; |
| 614 } | 604 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 default: | 645 default: |
| 656 fprintf(stderr, "Unable to parse write %s for %s.\n", modestr, name); | 646 fprintf(stderr, "Unable to parse write %s for %s.\n", modestr, name); |
| 657 return false; | 647 return false; |
| 658 } | 648 } |
| 659 | 649 |
| 660 Path path(name); | 650 Path path(name); |
| 661 std::string url = url_root_ + (path.IsAbsolute() ? | 651 std::string url = url_root_ + (path.IsAbsolute() ? |
| 662 path.Range(1, path.Size()) : | 652 path.Range(1, path.Size()) : |
| 663 path.Join()); | 653 path.Join()); |
| 664 | 654 |
| 665 MountNode* node = new MountNodeHttp(this, num_nodes_, dev_, url); | 655 MountNode* node = new MountNodeHttp(this, url); |
| 666 node->Init(mode, USR_ID, GRP_ID); | 656 node->Init(mode); |
| 667 node->stat_.st_size = atoi(lenstr); | 657 node->stat_.st_size = atoi(lenstr); |
| 668 | 658 |
| 669 MountNodeDir* dir_node = FindOrCreateDir(path.Parent()); | 659 MountNodeDir* dir_node = FindOrCreateDir(path.Parent()); |
| 670 dir_node->AddChild(path.Basename(), node); | 660 dir_node->AddChild(path.Basename(), node); |
| 671 | 661 |
| 672 std::string pname = path.Join(); | 662 std::string pname = path.Join(); |
| 673 node_cache_[pname] = node; | 663 node_cache_[pname] = node; |
| 674 } | 664 } |
| 675 } | 665 } |
| 676 | 666 |
| 677 return true; | 667 return true; |
| 678 } | 668 } |
| 679 | 669 |
| 680 char *MountHttp::LoadManifest(const std::string& manifestName) { | 670 char *MountHttp::LoadManifest(const std::string& manifestName) { |
| 681 Path manifestPath(manifestName); | 671 Path manifestPath(manifestName); |
| 682 MountNode* manifiestNode = Open(manifestPath, O_RDONLY); | 672 MountNode* manifiestNode = Open(manifestPath, O_RDONLY); |
| 683 | 673 |
| 684 if (manifiestNode) { | 674 if (manifiestNode) { |
| 685 char *text = new char[manifiestNode->GetSize() + 1]; | 675 char *text = new char[manifiestNode->GetSize() + 1]; |
| 686 off_t len = manifiestNode->Read(0, text, manifiestNode->GetSize()); | 676 off_t len = manifiestNode->Read(0, text, manifiestNode->GetSize()); |
| 687 manifiestNode->Release(); | 677 manifiestNode->Release(); |
| 688 | 678 |
| 689 text[len] = 0; | 679 text[len] = 0; |
| 690 return text; | 680 return text; |
| 691 } | 681 } |
| 692 | 682 |
| 693 fprintf(stderr, "Could not open manifest: %s\n", manifestName.c_str()); | 683 fprintf(stderr, "Could not open manifest: %s\n", manifestName.c_str()); |
| 694 return NULL; | 684 return NULL; |
| 695 } | 685 } |
| OLD | NEW |