1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/**
* @file mega/file.h
* @brief Classes for transferring files
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#ifndef MEGA_FILE_H
#define MEGA_FILE_H 1
#include "filesystem.h"
#include <cstdint>
#ifdef ENABLE_SYNC
#include "syncinternals/mac_computation_state.h"
#endif
namespace mega {
enum class CollisionResolution : uint8_t
{
Begin = 1,
Overwrite = 1,
RenameNewWithN = 2,
RenameExistingToOldN = 3,
End = 4,
};
constexpr unsigned FILE_MAX_RETRIES = 16;
constexpr unsigned FILE_IO_MAX_RETRIES = 6;
constexpr unsigned FILE_SYNC_MAX_RETRIES = 8;
class TransferDbCommitter; // Forward declaration
// File is the base class for an upload or download, as managed by the SDK core.
// Each Transfer consists of a list of File that all have the same content and fingerprint
struct MEGA_API File: public FileFingerprint
{
// set localfilename in attached transfer
virtual void prepare(FileSystemAccess&);
// file transfer dispatched, expect updates/completion/failure
virtual void start();
// progress update
virtual void progress();
// transfer completion
virtual void completed(Transfer*, putsource_t source);
// transfer terminated before completion (cancelled, failed too many times)
virtual void terminated(error e);
// return true if the transfer should keep trying (limited to 16)
// return false to delete the transfer
virtual bool failed(error, MegaClient*);
// update localname
virtual void updatelocalname() {}
void sendPutnodesOfUpload(
MegaClient* client,
UploadHandle fileAttrMatchHandle,
std::string&& fileAttr,
const UploadToken& ultoken,
const FileNodeKey& newFileKey,
putsource_t source,
NodeHandle ovHandle,
std::function<void(const Error&,
targettype_t,
vector<NewNode>&,
bool targetOverride,
int tag,
const std::map<std::string, std::string>& fileHandles)>&& completion,
const m_time_t* overrideMtime,
bool canChangeVault,
std::optional<Pitag> pitag = std::nullopt);
void sendPutnodesToCloneNode(
MegaClient* client,
Node* nodeToClone,
putsource_t source,
NodeHandle ovHandle,
std::function<void(const Error&,
targettype_t,
vector<NewNode>&,
bool targetOverride,
int tag,
const std::map<std::string, std::string>& fileHandles)>&& completion,
bool canChangeVault);
void setCollisionResolution(CollisionResolution collisionResolution) { mCollisionResolution = collisionResolution; }
CollisionResolution getCollisionResolution() const { return mCollisionResolution; }
// generic filename for this transfer
void displayname(string*);
string displayname();
// normalized name (UTF-8 with unescaped special chars)
string name;
// local filename (must be set upon injection for uploads, can be set in start() for downloads)
// now able to be updated from the syncs thread, should the nodes move during upload/download
static mutex localname_mutex;
LocalPath localname_multithreaded;
LocalPath getLocalname() const;
void setLocalname(const LocalPath&);
// source/target node handle
NodeHandle h;
// previous node, if any
std::shared_ptr<Node> previousNode;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4201) // nameless struct
#endif
struct
{
// source handle private?
bool hprivate : 1;
// source handle foreign?
bool hforeign : 1;
// is this part of a sync transfer?
bool syncxfer : 1;
// is the source file temporary?
bool temporaryfile : 1;
// remember if the sync is from an inshare
bool fromInsycShare : 1;
};
#ifdef _MSC_VER
#pragma warning(pop)
#endif
VersioningOption mVersioningOption = NoVersioning;
// private auth to access the node
string privauth;
// public auth to access the node
string pubauth;
// chat auth to access the node
char *chatauth;
// if !hprivate, filekey and size must be valid
byte filekey[FILENODEKEYLENGTH]{};
// for remote file drops: uid or e-mail address of recipient
string targetuser;
// transfer linkage
Transfer* transfer;
file_list::iterator file_it{};
File();
virtual ~File();
// serialize the File object
bool serialize(string*) const override;
static File* unserialize(string*);
// tag of the file transfer
int tag;
// set the token true to cause cancellation of this transfer (this file of the transfer)
CancelToken cancelToken;
// True if this is a FUSE transfer.
virtual bool isFuseTransfer() const;
// relevant only for downloads (GET); do not override anywhere else
virtual bool undelete() const { return false; }
// Set this file's logical path.
void logicalPath(LocalPath logicalPath);
// Retrieve this file's logical path.
LocalPath logicalPath() const;
std::optional<Pitag> getPitag() const
{
return mPitag;
};
void setPitag(Pitag pitag)
{
mPitag = pitag;
};
private:
CollisionResolution mCollisionResolution;
// The file's logical path.
LocalPath mLogicalPath;
std::optional<Pitag> mPitag;
};
class SyncThreadsafeState;
struct CloudNode;
struct SyncTransfer_inClient: public File
{
// self-destruct after completion
void completed(Transfer*, putsource_t) override;
void terminated(error) override;
// We will be passing a raw pointer to this object
// into the tranfer system on the client thread.
// this member prevents that becoming a dangling pointer
// should the sync no longer require it. So we set this
// member just before startxfer, and reset it on completed()/terminated()
shared_ptr<SyncTransfer_inClient> selfKeepAlive;
shared_ptr<SyncThreadsafeState> syncThreadSafeState;
// Why was the transfer failed/terminated?
error mError = API_OK;
std::atomic<bool> wasTerminated{false};
std::atomic<bool> wasFileTransferCompleted{false};
std::atomic<bool> wasRequesterAbandoned{false};
enum class AttributeOnlyUpdate : std::uint8_t
{
None = 0,
MtimeOnly,
CrcOnly,
};
std::atomic<AttributeOnlyUpdate> attributeOnlyUpdate{AttributeOnlyUpdate::None};
// Whether the terminated SyncTransfer_inClient was already notified to the apps/in the logs
std::atomic<bool> terminatedReasonAlreadyKnown{false};
std::optional<int64_t> mMetaMac{std::nullopt};
};
struct SyncDownload_inClient: public SyncTransfer_inClient
{
shared_ptr<FileDistributor> downloadDistributor;
// set sync-specific temp filename, update treestate
void prepare(FileSystemAccess&) override;
bool failed(error, MegaClient*) override;
SyncDownload_inClient(CloudNode& n,
const LocalPath&,
bool fromInshare,
shared_ptr<SyncThreadsafeState> stss,
const FileFingerprint& overwriteFF,
const int64_t metamac,
const AttributeOnlyUpdate attributeOnlyUpdate);
~SyncDownload_inClient();
// True if we could copy (or move) the download into place.
bool wasDistributed = false;
FileFingerprint okToOverwriteFF;
};
struct SyncUpload_inClient : SyncTransfer_inClient, std::enable_shared_from_this<SyncUpload_inClient>
{
// This class is part of the client's Transfer system (ie, works in the client's thread)
// The sync system keeps a shared_ptr to it. Whichever system finishes with it last actually deletes it
SyncUpload_inClient(NodeHandle targetFolder,
const LocalPath& fullPath,
const string& nodeName,
const FileFingerprint& ff,
shared_ptr<SyncThreadsafeState> stss,
handle fsid,
const LocalPath& localname,
bool fromInshare,
const int64_t metamac,
const AttributeOnlyUpdate attributeOnlyUpdate);
~SyncUpload_inClient();
void prepare(FileSystemAccess&) override;
void completed(Transfer*, putsource_t) override;
void updateFingerprintMtime(const m_time_t newMtime);
void updateFingerprint(const FileFingerprint& newFingerprint);
/* UpSync operation can be one of the following:
* - putnodes of upload
* - put nodes of clone
* - update mtime of node in cloud
*/
std::atomic<bool> upsyncStarted{false};
std::atomic<bool> upsyncFailed{false};
std::atomic<bool> wasStarted{false};
std::atomic<bool> wasUpsyncCompleted{false};
// Valid when wasUpsyncCompleted is true.
NodeHandle upsyncResultHandle;
handle sourceFsid = UNDEF;
LocalPath sourceLocalname;
// once the upload completes these are set. todo: should we dynamically allocate space for these, save RAM for mass transfer cases?
UploadHandle uploadHandle;
UploadToken uploadToken;
FileNodeKey fileNodeKey;
std::string fileAttr;
void fullUpload(MegaClient& client,
mega::TransferDbCommitter& committer,
const VersioningOption vo,
const bool queueFirst);
void cloneNode(MegaClient& client,
std::shared_ptr<Node> cloneNodeCandidate,
const NodeHandle ovHandleIfShortcut);
bool updateNodeMtime(MegaClient* client,
std::shared_ptr<Node> node,
const m_time_t newMtime,
std::function<void(NodeHandle, Error)>&& completion);
void sendPutnodesOfUpload(MegaClient* client, NodeHandle ovHandle);
void sendPutnodesToCloneNode(MegaClient* client, NodeHandle ovHandle, Node* nodeToClone);
#ifdef ENABLE_SYNC
/**
* @brief State for async MAC computation when looking for clone candidates.
*
* Used to pre-compute MAC before calling findCloneNodeCandidate, avoiding
* blocking the client thread for large files.
*/
std::shared_ptr<MacComputationState> macComputation;
#endif
};
/**
* @struct DelayedSyncUpload
* @brief Represents an upload task that is delayed for throttling purposes.
*
* This struct encapsulates the details of an upload task that is queued for later
* processing due to throttling conditions.
*/
struct DelayedSyncUpload
{
/**
* @brief Weak pointer to the upload client responsible for this task.
*
* This prevents holding a strong reference to the upload, allowing it to be safely
* cleaned up if no longer valid before the task is processed.
*/
std::weak_ptr<SyncUpload_inClient> mWeakUpload;
/**
* @brief Versioning option for the upload task.
*/
VersioningOption mVersioningOption;
/**
* @brief Flag indicating if this upload should be queued first in the client.
*/
bool mQueueFirst;
/**
* @brief Node handle representing a shortcut for the upload.
*/
NodeHandle mOvHandleIfShortcut;
/**
* @brief Constructs a DelayedUpload instance.
*
* @param upload Shared pointer to the upload owned by the LocalNode.
* For the other params, see LocalNode::queueClientUpload()
*/
DelayedSyncUpload(std::shared_ptr<SyncUpload_inClient> upload,
const VersioningOption vo,
const bool queueFirst,
const NodeHandle ovHandleIfShortcut):
mWeakUpload(std::move(upload)),
mVersioningOption(vo),
mQueueFirst(queueFirst),
mOvHandleIfShortcut(ovHandleIfShortcut)
{}
};
} // namespace
#endif
|