Skip to content

Commit

Permalink
Remove all extended attributes on MacOSX after upload session abortion
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-guyot-infomaniak committed Oct 31, 2024
1 parent 0381016 commit f22ce02
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DriveUploadSession::DriveUploadSession(int driveDbId, std::shared_ptr<SyncDb> sy

DriveUploadSession::~DriveUploadSession() {
if (_vfsForceStatus && !_vfsForceStatus(getFilePath(), false, 100, true)) {
LOGW_WARN(getLogger(), L"Error in vfsForceStatus: " << Utility::formatSyncPath(getFilePath()).c_str());
LOGW_WARN(getLogger(), L"Error in vfsForceStatus: " << Utility::formatSyncPath(getFilePath()));
}
}

Expand Down Expand Up @@ -117,10 +117,18 @@ void DriveUploadSession::abort() {
setVfsForceStatusCallback(nullptr);

#ifdef __APPLE__
// Removing all extended attributes so that the file that failed to be uploaded is not identified as a dehydrated placeholder
// by the application when restarting the synchronization.

const SyncPath &localPath = getFilePath();
if (auto ioError = IoError::Success;
!IoHelper::removeXAttr(localPath, EXT_ATTR_STATUS, ioError) || ioError != IoError::NoSuchFileOrDirectory) {
LOGW_WARN(getLogger(), "Error in IoHelper::removeXAttr: " << Utility::formatIoError(localPath, ioError));
static const std::vector<const char *> attributes = {EXT_ATTR_STATUS, EXT_ATTR_PIN_STATE};

for (const auto attribute: attributes) {
if (auto ioError = IoError::Success;
!IoHelper::removeXAttr(localPath, EXT_ATTR_STATUS, ioError) || ioError != IoError::NoSuchFileOrDirectory) {
LOGW_WARN(getLogger(), "Error in IoHelper::removeXAttr with extended attribute "
<< attribute << L": " << Utility::formatIoError(localPath, ioError));
}
}
#endif
}
Expand Down
83 changes: 48 additions & 35 deletions src/server/vfs/mac/litesyncextconnector.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,8 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
// Set status
IoError ioError = IoError::Success;
if (!setXAttrValue(path, [@EXT_ATTR_STATUS UTF8String], EXT_ATTR_STATUS_ONLINE, ioError)) {
const std::wstring ioErrorMsg = Utility::s2ws(IoHelper::ioError2StdString(ioError));
LOGW_WARN(_logger,
L"Call to setXAttrValue failed - " << Utility::formatPath(path).c_str() << L" Error: " << ioErrorMsg.c_str());
L"Call to setXAttrValue failed: " << Utility::formatIoError(path, ioError));
return false;
}

Expand All @@ -1077,9 +1076,8 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
[@EXT_ATTR_PIN_STATE UTF8String],
EXT_ATTR_PIN_STATE_UNPINNED,
ioError)) {
const std::wstring ioErrorMsg = Utility::s2ws(IoHelper::ioError2StdString(ioError));
LOGW_WARN(_logger,
L"Call to setXAttrValue failed - " << Utility::formatPath(path).c_str() << L" Error: " << ioErrorMsg.c_str());
L"Call to setXAttrValue failed: " << Utility::formatIoError(path, ioError));
return false;
}

Expand All @@ -1091,12 +1089,12 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
bool exists = false;
if (!Utility::setFileDates(QStr2Path(path), std::make_optional<KDC::SyncTime>(fileStat->st_birthtimespec.tv_sec),
std::make_optional<KDC::SyncTime>(fileStat->st_mtimespec.tv_sec), false, exists)) {
LOGW_WARN(_logger, L"Call to Utility::setFileDates failed - " << Utility::formatPath(path).c_str());
LOGW_WARN(_logger, L"Call to Utility::setFileDates failed - " << Utility::formatPath(path));
return false;
}

if (!exists) {
LOGW_DEBUG(_logger, L"Item doesn't exist - " << Utility::formatPath(path).c_str());
LOGW_DEBUG(_logger, L"Item doesn't exist - " << Utility::formatPath(path));
return false;
}

Expand Down Expand Up @@ -1136,8 +1134,8 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
NSInteger errorCode = error ? error.code : 0;
if (error) {
LOGW_WARN(_logger,
L"Failed to get attributes - " << Utility::formatPath(filePath).c_str()
<< L" errno=" << errorCode);
L"Failed to get attributes - " << Utility::formatPath(filePath) << L" errno="
<< errorCode);
return false;
}

Expand All @@ -1148,8 +1146,9 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co

// Copy tmp file content to file
@try {
LOGW_INFO(_logger, L"Copying temp file - from " << Utility::formatPath(tmpFilePath).c_str() << L" to "
<< Utility::formatPath(filePath).c_str());
LOGW_INFO(_logger,
L"Copying temp file - from " << Utility::formatPath(tmpFilePath)
<< L" to " << Utility::formatPath(filePath));

NSFileHandle *tmpFileHandle = [NSFileHandle fileHandleForReadingAtPath:tmpFilePath.toNSString()];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath.toNSString()];
Expand All @@ -1160,21 +1159,21 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
if ((buffer = [tmpFileHandle readDataUpToLength:COPY_CHUNK_SIZE error:&error]) == nil) {
errorCode = error ? error.code : 0;
LOGW_ERROR(_logger,
L"Error while reading tmp file - "
<< Utility::formatPath(tmpFilePath).c_str() << L" error="
<< errorCode);
L"Error while reading tmp file - " << Utility::formatPath(
tmpFilePath) << L" error=" << errorCode);
break;
}
errorCode = error ? error.code : 0;

if (buffer.length == 0) {
// Nothing else to read
break;
}

if (![fileHandle writeData:buffer error:&error]) {
errorCode = error ? error.code : 0;
LOGW_ERROR(_logger, L"Error while writing to file - " << Utility::formatPath(filePath).c_str()
<< L" error=" << errorCode);
LOGW_ERROR(_logger,
L"Error while writing to file - " << Utility::formatPath(
filePath) << L" error=" << errorCode);
break;
}
}
Expand All @@ -1183,17 +1182,19 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
[tmpFileHandle closeFile];
[fileHandle closeFile];
} @catch (NSException *e) {
LOGW_WARN(_logger, L"Could not copy tmp file - from path="
<< QStr2WStr(tmpFilePath).c_str() << L" to " << Utility::formatPath(filePath).c_str()
<< Utility::s2ws(std::string([e.name UTF8String])).c_str());
LOGW_WARN(_logger,
L"Could not copy tmp file - from path="
<< QStr2WStr(tmpFilePath) << L" to " << Utility::formatPath(filePath)
<< Utility::s2ws(std::string([e.name UTF8String])));
return false;
}

// Set attributes
if (![[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:filePath.toNSString() error:&error]) {
errorCode = error ? error.code : 0;
LOGW_WARN(_logger,
L"Could not set attributes to file - " << Utility::formatPath(filePath).c_str() << L" errno=" << errorCode);
L"Could not set attributes to file - " << Utility::formatPath(filePath)
<< L" errno=" << errorCode);
return false;
}

Expand All @@ -1203,25 +1204,29 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
}

if (!_private->updateFetchStatus(filePath, QString("OK"))) {
LOGW_WARN(_logger, L"Call to updateFetchStatus failed - " << Utility::formatPath(filePath).c_str());
LOGW_WARN(_logger,
L"Call to updateFetchStatus failed - " << Utility::formatPath(filePath));
return false;
}

// Set file dates
bool exists = false;
if (!Utility::setFileDates(QStr2Path(filePath), std::make_optional<KDC::SyncTime>(creationDate), std::nullopt, false,
exists)) {
LOGW_WARN(_logger, L"Call to Utility::setFileDates failed - " << Utility::formatPath(filePath).c_str());
LOGW_WARN(_logger,
L"Call to Utility::setFileDates failed - "
<< Utility::formatPath(filePath));
return false;
}

if (!exists) {
LOGW_DEBUG(_logger, L"Item doesn't exist - " << Utility::formatPath(filePath).c_str());
LOGW_DEBUG(_logger, L"Item does not exist: " << Utility::formatPath(filePath));
return false;
}
} else {
// Set status
int progress = static_cast<int>(ceil(float(completed) / fileSize * 100));
const int progress = static_cast<int>(
ceil(float(completed) / static_cast<float>(fileSize) * 100));
if (!vfsSetStatus(filePath, localSyncPath, true, progress)) {
return false;
}
Expand All @@ -1233,7 +1238,7 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co

bool LiteSyncExtConnector::vfsCancelHydrate(const QString &filePath) {
if (!_private->updateFetchStatus(filePath, QString("CANCEL"))) {
LOGW_WARN(_logger, L"Call to updateFetchStatus failed - " << Utility::formatPath(filePath).c_str());
LOGW_WARN(_logger, L"Call to updateFetchStatus failed - " << Utility::formatPath(filePath));
return false;
}
return true;
Expand All @@ -1242,7 +1247,8 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
bool LiteSyncExtConnector::vfsSetThumbnail(const QString &absoluteFilePath, const QPixmap &pixmap) {
// Set thumbnail
if (!_private->setThumbnail(absoluteFilePath, pixmap)) {
LOGW_WARN(_logger, L"Call to setThumbnail failed - " << Utility::formatPath(absoluteFilePath).c_str());
LOGW_WARN(_logger,
L"Call to setThumbnail failed - " << Utility::formatPath(absoluteFilePath));
}

return true;
Expand Down Expand Up @@ -1328,21 +1334,26 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co

fd = open(stdPath.c_str(), FWRITE);
if (fd == -1) {
LOGW_WARN(_logger, L"Call to open failed - " << Utility::formatPath(absoluteFilePath).c_str() << L" errno=" << errno);
LOGW_WARN(_logger,
L"Call to open failed - " << Utility::formatPath(absoluteFilePath)
<< L" errno=" << errno);
*error = QObject::tr("Call to open failed - path=%1").arg(absoluteFilePath);
return false;
}
}

if (ftruncate(fd, fileStat->st_size) == -1) {
LOGW_WARN(_logger,
L"Call to ftruncate failed - " << Utility::formatPath(absoluteFilePath).c_str() << L" errno=" << errno);
L"Call to ftruncate failed - " << Utility::formatPath(absoluteFilePath)
<< L" errno=" << errno);
*error = QObject::tr("Call to ftruncate failed - path=%1").arg(absoluteFilePath);
return false;
}

if (close(fd) == -1) {
LOGW_WARN(_logger, L"Call to close failed - " << Utility::formatPath(absoluteFilePath).c_str() << L" errno=" << errno);
LOGW_WARN(_logger,
L"Call to close failed - " << Utility::formatPath(absoluteFilePath) << L" errno="
<< errno);
*error = QObject::tr("Call to close failed - path=%1").arg(absoluteFilePath);
return false;
}
Expand All @@ -1351,12 +1362,14 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
bool exists;
if (!Utility::setFileDates(QStr2Path(absoluteFilePath), fileStat->st_birthtimespec.tv_sec, fileStat->st_mtimespec.tv_sec,
false, exists)) {
LOGW_WARN(_logger, L"Call to Utility::setFileDates failed - " << Utility::formatPath(absoluteFilePath).c_str());
LOGW_WARN(_logger,
L"Call to Utility::setFileDates failed - "
<< Utility::formatPath(absoluteFilePath));
return false;
}

if (!exists) {
LOGW_DEBUG(_logger, L"Item doesn't exist - " << Utility::formatPath(absoluteFilePath).c_str());
LOGW_DEBUG(_logger, L"Item doesn't exist - " << Utility::formatPath(absoluteFilePath));
return false;
}

Expand Down Expand Up @@ -1389,8 +1402,8 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co

int roundedProgress = progress;
if (isSyncing) {
int stepWidth = 100 / SYNC_STEPS;
roundedProgress = static_cast<int>(ceil(float(progress) / stepWidth) * stepWidth);
const int stepWidth = 100 / SYNC_STEPS;
roundedProgress = static_cast<int>(ceil(float(progress) / float(stepWidth)) * stepWidth);
}

if (isSyncing != isSyncingCurrent || roundedProgress != progressCurrent || isHydrated != isHydratedCurrent) {
Expand All @@ -1408,7 +1421,7 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co
}

if (!sendStatusToFinder(path, isSyncing, roundedProgress, isHydrated)) {
LOGW_WARN(_logger, L"Call to sendStatusToFinder failed - " << Utility::formatPath(path).c_str());
LOGW_WARN(_logger, L"Call to sendStatusToFinder failed - " << Utility::formatPath(path));
return false;
}

Expand Down Expand Up @@ -1526,7 +1539,7 @@ bool checkIoErrorAndLogIfNeeded(IoError ioError, const std::string &itemType, co

QString status;
if (isSyncing) {
int stepWidth = 100 / SYNC_STEPS;
const int stepWidth = 100 / SYNC_STEPS;
status = QString("SYNC_%1").arg(ceil(float(progress) / stepWidth) * stepWidth);
} else if (isHydrated) {
status = "OK";
Expand Down

0 comments on commit f22ce02

Please sign in to comment.