1 /* 2 * Copyright 2009-2017 Alibaba Cloud All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <string> 18 #include <algorithm> 19 #ifdef _WIN32 20 #include <codecvt> 21 #endif 22 #include <alibabacloud/oss/Const.h> 23 #include "ResumableBaseWorker.h" 24 #include "../utils/FileSystemUtils.h" 25 #include "utils/Utils.h" 26 27 using namespace AlibabaCloud::OSS; 28 29 ResumableBaseWorker(uint64_t objectSize,uint64_t partSize)30ResumableBaseWorker::ResumableBaseWorker(uint64_t objectSize, uint64_t partSize) : 31 hasRecord_(false), 32 objectSize_(objectSize), 33 consumedSize_(0), 34 partSize_(partSize) 35 { 36 } 37 validate(OssError & err)38int ResumableBaseWorker::validate(OssError& err) 39 { 40 genRecordPath(); 41 42 if (hasRecordPath()) { 43 if (0 != loadRecord()) { 44 removeRecordFile(); 45 } 46 } 47 48 if (hasRecord_) { 49 if (0 != validateRecord()) { 50 removeRecordFile(); 51 if (0 != prepare(err)) { 52 return -1; 53 } 54 } 55 } 56 else { 57 if (0 != prepare(err)) { 58 return -1; 59 } 60 } 61 return 0; 62 } 63 determinePartSize()64void ResumableBaseWorker::determinePartSize() 65 { 66 uint64_t partSize = partSize_; 67 uint64_t objectSize = objectSize_; 68 uint64_t partCount = (objectSize - 1) / partSize + 1; 69 while (partCount > PartNumberUpperLimit) { 70 partSize = partSize * 2; 71 partCount = (objectSize - 1) / partSize + 1; 72 } 73 74 partSize_ = partSize; 75 } 76 hasRecordPath()77bool ResumableBaseWorker::hasRecordPath() 78 { 79 return !(recordPath_.empty() && recordPathW_.empty()); 80 } 81 removeRecordFile()82void ResumableBaseWorker::removeRecordFile() 83 { 84 if (!recordPath_.empty()) { 85 RemoveFile(recordPath_); 86 } 87 #ifdef _WIN32 88 if (!recordPathW_.empty()) { 89 RemoveFile(recordPathW_); 90 } 91 #endif 92 } 93 94 #ifdef _WIN32 95 toString(const std::wstring & str)96std::string ResumableBaseWorker::toString(const std::wstring& str) 97 { 98 std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; 99 return conv.to_bytes(str); 100 } 101 toWString(const std::string & str)102std::wstring ResumableBaseWorker::toWString(const std::string& str) 103 { 104 std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; 105 return conv.from_bytes(str); 106 } 107 108 #else 109 toString(const std::wstring & str)110std::string ResumableBaseWorker::toString(const std::wstring& str) 111 { 112 UNUSED_PARAM(str); 113 return ""; 114 } 115 toWString(const std::string & str)116std::wstring ResumableBaseWorker::toWString(const std::string& str) 117 { 118 UNUSED_PARAM(str); 119 return L""; 120 } 121 122 #endif 123 124