12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216 |
- #include <kopano/platform.h>
- #include <stdexcept>
- #include <climits>
- #include <mapidefs.h>
- #include <cerrno>
- #include <algorithm>
- #include <kopano/lockhelper.hpp>
- #include <fcntl.h>
- #include <zlib.h>
- #include <ECSerializer.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include "ECAttachmentStorage.h"
- #include "SOAPUtils.h"
- #include <kopano/ECLogger.h>
- #include <kopano/MAPIErrors.h>
- #include <mapitags.h>
- #include <kopano/stringutil.h>
- #include "StreamUtil.h"
- #include "ECS3Attachment.h"
- namespace KC {
- #define CHUNK_SIZE (384 * 1024)
- #define ZLIB_BUFFER_SIZE std::max(CHUNK_SIZE, 128 * 1024)
- ECAttachmentStorage::ECAttachmentStorage(ECDatabase *lpDatabase, unsigned int ulCompressionLevel)
- : m_lpDatabase(lpDatabase)
- {
- m_ulRef = 0;
- m_bFileCompression = ulCompressionLevel != 0;
- if (ulCompressionLevel > Z_BEST_COMPRESSION)
- ulCompressionLevel = Z_BEST_COMPRESSION;
- m_CompressionLevel = stringify(ulCompressionLevel);
- }
- ULONG ECAttachmentStorage::AddRef() {
- scoped_lock lk(m_refcnt_lock);
- return ++m_ulRef;
- }
- ULONG ECAttachmentStorage::Release() {
- ulock_normal l_ref(m_refcnt_lock);
- ULONG ulRef = --m_ulRef;
- l_ref.unlock();
- if (m_ulRef == 0)
- delete this;
- return ulRef;
- }
- ECRESULT ECAttachmentStorage::CreateAttachmentStorage(ECDatabase *lpDatabase,
- ECConfig *lpConfig, ECAttachmentStorage **lppAttachmentStorage)
- {
- ECAttachmentStorage *lpAttachmentStorage = NULL;
- if (lpDatabase == NULL) {
- ec_log_err("ECAttachmentStorage::CreateAttachmentStorage(): DB not available yet");
- return KCERR_DATABASE_ERROR;
- }
- const char *ans = lpConfig->GetSetting("attachment_storage");
- const char *dir = lpConfig->GetSetting("attachment_path");
- if (dir == NULL) {
- ec_log_err("No attachment_path set despite attachment_storage=files. Falling back to database attachments.");
- ans = NULL;
- }
- if (ans != NULL && strcmp(ans, "files") == 0) {
- const char *const sync_files_par = lpConfig->GetSetting("attachment_files_fsync");
- bool sync_files = sync_files_par == NULL || strcasecmp(sync_files_par, "yes") == 0;
- const char *comp = lpConfig->GetSetting("attachment_compression");
- unsigned int complvl = (comp == NULL) ? 0 : strtoul(comp, NULL, 0);
- lpAttachmentStorage = new ECFileAttachment(lpDatabase, dir, complvl, sync_files);
- #ifdef HAVE_LIBS3_H
- } else if (ans != NULL && strcmp(ans, "s3") == 0) {
- try {
- lpAttachmentStorage = new ECS3Attachment(lpDatabase,
- lpConfig->GetSetting("attachment_s3_protocol"),
- lpConfig->GetSetting("attachment_s3_uristyle"),
- lpConfig->GetSetting("attachment_s3_accesskeyid"),
- lpConfig->GetSetting("attachment_s3_secretaccesskey"),
- lpConfig->GetSetting("attachment_s3_bucketname"),
- lpConfig->GetSetting("attachment_s3_region"),
- lpConfig->GetSetting("attachment_path"),
- strtol(lpConfig->GetSetting("attachment_compression"), NULL, 0));
- } catch (std::runtime_error &e) {
- ec_log_warn("Cannot instantiate ECS3Attachment: %s", e.what());
- return KCERR_DATABASE_ERROR;
- }
- #endif
- } else {
- lpAttachmentStorage = new ECDatabaseAttachment(lpDatabase);
- }
- lpAttachmentStorage->AddRef();
- *lppAttachmentStorage = lpAttachmentStorage;
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::GetSingleInstanceId(ULONG ulObjId, ULONG ulTag, ULONG *lpulInstanceId)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- strQuery =
- "SELECT `instanceid` "
- "FROM `singleinstances` "
- "WHERE `hierarchyid` = " + stringify(ulObjId) + " AND `tag` = " + stringify(ulTag) + " LIMIT 1";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::GetSingleInstanceId(): DoSelect() failed %x", er);
- return er;
- }
- lpDBRow = m_lpDatabase->FetchRow(lpDBResult);
- if (lpDBRow == nullptr || lpDBRow[0] == nullptr)
-
- return KCERR_NOT_FOUND;
- if (lpulInstanceId)
- *lpulInstanceId = atoi(lpDBRow[0]);
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::GetSingleInstanceIds(const std::list<ULONG> &lstObjIds, std::list<ULONG> *lstAttachIds)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- std::list<ULONG> lstInstanceIds;
-
- if (lstObjIds.empty())
- return erSuccess;
- strQuery =
- "SELECT DISTINCT `instanceid` "
- "FROM `singleinstances` "
- "WHERE `hierarchyid` IN (";
- for (auto i = lstObjIds.cbegin(); i != lstObjIds.cend(); ++i) {
- if (i != lstObjIds.cbegin())
- strQuery += ",";
- strQuery += stringify(*i);
- }
- strQuery += ")";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess)
- return er;
- while ((lpDBRow = m_lpDatabase->FetchRow(lpDBResult)) != NULL) {
- if (lpDBRow[0] == NULL) {
- ec_log_err("ECAttachmentStorage::GetSingleInstanceIds(): column contains NULL");
- return KCERR_DATABASE_ERROR;
- }
- lstInstanceIds.push_back(atoi(lpDBRow[0]));
- }
- lstAttachIds->swap(lstInstanceIds);
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::SetSingleInstanceId(ULONG ulObjId, ULONG ulInstanceId, ULONG ulTag)
- {
- std::string strQuery;
-
- if (!ExistAttachmentInstance(ulInstanceId))
- return KCERR_UNABLE_TO_COMPLETE;
-
- strQuery =
- "REPLACE INTO `singleinstances` (`instanceid`, `hierarchyid`, `tag`) VALUES"
- "(" + stringify(ulInstanceId) + ", " + stringify(ulObjId) + ", " + stringify(ulTag) + ")";
- return m_lpDatabase->DoInsert(strQuery, reinterpret_cast<unsigned int *>(&ulInstanceId));
- }
- ECRESULT ECAttachmentStorage::GetSingleInstanceParents(ULONG ulInstanceId, std::list<ULONG> *lplstObjIds)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- std::list<ULONG> lstObjIds;
- strQuery =
- "SELECT DISTINCT `hierarchyid` "
- "FROM `singleinstances` "
- "WHERE `instanceid` = " + stringify(ulInstanceId);
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess)
- return er;
- while ((lpDBRow = m_lpDatabase->FetchRow(lpDBResult)) != NULL) {
- if (lpDBRow[0] == NULL) {
- ec_log_err("ECAttachmentStorage::GetSingleInstanceParents(): column contains NULL");
- return KCERR_DATABASE_ERROR;
- }
- lstObjIds.push_back(atoi(lpDBRow[0]));
- }
- lplstObjIds->swap(lstObjIds);
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::IsOrphanedSingleInstance(ULONG ulInstanceId, bool *bOrphan)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- strQuery =
- "SELECT `instanceid` "
- "FROM `singleinstances` "
- "WHERE `instanceid` = " + stringify(ulInstanceId) + " "
- "LIMIT 1";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess)
- return er;
- lpDBRow = m_lpDatabase->FetchRow(lpDBResult);
-
- *bOrphan = (!lpDBRow || !lpDBRow[0]);
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::GetOrphanedSingleInstances(const std::list<ULONG> &lstInstanceIds, std::list<ULONG> *lplstOrphanedInstanceIds)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- strQuery =
- "SELECT DISTINCT `instanceid` "
- "FROM `singleinstances` "
- "WHERE `instanceid` IN ( ";
- for (auto i = lstInstanceIds.cbegin(); i != lstInstanceIds.cend(); ++i) {
- if (i != lstInstanceIds.cbegin())
- strQuery += ",";
- strQuery += stringify(*i);
- }
- strQuery += ")";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::GetOrphanedSingleInstances(): DoSelect failed %x", er);
- return er;
- }
-
- lplstOrphanedInstanceIds->assign(lstInstanceIds.begin(), lstInstanceIds.end());
-
- while ((lpDBRow = m_lpDatabase->FetchRow(lpDBResult)) != NULL) {
- if (lpDBRow[0] == NULL) {
- ec_log_err("ECAttachmentStorage::GetOrphanedSingleInstances(): column contains NULL");
- return KCERR_DATABASE_ERROR;
- }
- lplstOrphanedInstanceIds->remove(atoi(lpDBRow[0]));
- }
- return erSuccess;
- }
- bool ECAttachmentStorage::ExistAttachment(ULONG ulObjId, ULONG ulPropId)
- {
- ECRESULT er = erSuccess;
- ULONG ulInstanceId = 0;
-
- er = GetSingleInstanceId(ulObjId, ulPropId, &ulInstanceId);
- if (er != erSuccess)
- return false;
- return ExistAttachmentInstance(ulInstanceId);
- }
- ECRESULT ECAttachmentStorage::LoadAttachment(struct soap *soap, ULONG ulObjId, ULONG ulPropId, size_t *lpiSize, unsigned char **lppData)
- {
- ECRESULT er;
- ULONG ulInstanceId = 0;
-
- er = GetSingleInstanceId(ulObjId, ulPropId, &ulInstanceId);
- if (er != erSuccess)
- return er;
- er = LoadAttachmentInstance(soap, ulInstanceId, lpiSize, lppData);
- if (er != erSuccess)
- return er;
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::LoadAttachment(ULONG ulObjId, ULONG ulPropId, size_t *lpiSize, ECSerializer *lpSink)
- {
- ECRESULT er;
- ULONG ulInstanceId = 0;
-
- er = GetSingleInstanceId(ulObjId, ulPropId, &ulInstanceId);
- if (er != erSuccess)
- return er;
- er = LoadAttachmentInstance(ulInstanceId, lpiSize, lpSink);
- if (er != erSuccess)
- return er;
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::SaveAttachment(ULONG ulObjId, ULONG ulPropId, bool bDeleteOld, size_t iSize, unsigned char *lpData, ULONG *lpulInstanceId)
- {
- ECRESULT er;
- ULONG ulInstanceId = 0;
- std::string strQuery;
- if (lpData == NULL)
- return KCERR_INVALID_PARAMETER;
- if (bDeleteOld) {
-
- er = DeleteAttachment(ulObjId, ulPropId, true);
- if (er != erSuccess)
- return er;
- }
-
- strQuery =
- "INSERT INTO `singleinstances` (`hierarchyid`, `tag`) VALUES"
- "(" + stringify(ulObjId) + ", " + stringify(ulPropId) + ")";
- er = m_lpDatabase->DoInsert(strQuery, (unsigned int*)&ulInstanceId);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::SaveAttachment(): DoInsert failed %x", er);
- return er;
- }
- er = SaveAttachmentInstance(ulInstanceId, ulPropId, iSize, lpData);
- if (er != erSuccess)
- return er;
- if (lpulInstanceId)
- *lpulInstanceId = ulInstanceId;
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::SaveAttachment(ULONG ulObjId, ULONG ulPropId, bool bDeleteOld, size_t iSize, ECSerializer *lpSource, ULONG *lpulInstanceId)
- {
- ECRESULT er;
- ULONG ulInstanceId = 0;
- std::string strQuery;
- if (bDeleteOld) {
-
- er = DeleteAttachment(ulObjId, ulPropId, true);
- if (er != erSuccess)
- return er;
- }
-
- strQuery =
- "INSERT INTO `singleinstances` (`hierarchyid`, `tag`) VALUES"
- "(" + stringify(ulObjId) + ", " + stringify(ulPropId) + ")";
- er = m_lpDatabase->DoInsert(strQuery, (unsigned int*)&ulInstanceId);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::SaveAttachment(): DoInsert failed %x", er);
- return er;
- }
- er = SaveAttachmentInstance(ulInstanceId, ulPropId, iSize, lpSource);
- if (er != erSuccess)
- return er;
- if (lpulInstanceId)
- *lpulInstanceId = ulInstanceId;
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::SaveAttachment(ULONG ulObjId, ULONG ulPropId, bool bDeleteOld, ULONG ulInstanceId, ULONG *lpulInstanceId)
- {
- ECRESULT er;
- ULONG ulOldAttachId = 0;
- if (bDeleteOld) {
-
- if (GetSingleInstanceId(ulObjId, ulPropId, &ulOldAttachId) == erSuccess &&
- ulOldAttachId == ulInstanceId)
-
- return erSuccess;
- er = DeleteAttachment(ulObjId, ulPropId, true);
- if (er != erSuccess)
- return er;
- }
- er = SetSingleInstanceId(ulObjId, ulInstanceId, ulPropId);
- if (er != erSuccess)
- return er;
-
- *lpulInstanceId = ulInstanceId;
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::CopyAttachment(ULONG ulObjId, ULONG ulNewObjId)
- {
- ECRESULT er;
- std::string strQuery;
-
- strQuery =
- "INSERT INTO `singleinstances` (`hierarchyid`, `instanceid`, `tag`) "
- "SELECT " + stringify(ulNewObjId) + ", `instanceid`, `tag` "
- "FROM `singleinstances` "
- "WHERE `hierarchyid` = " + stringify(ulObjId);
- er = m_lpDatabase->DoInsert(strQuery);
- if (er != erSuccess)
- ec_log_err("ECAttachmentStorage::CopyAttachment(): DoInsert failed %x", er);
- return er;
- }
- ECRESULT ECAttachmentStorage::DeleteAttachments(const std::list<ULONG> &lstDeleteObjects)
- {
- ECRESULT er = erSuccess;
- std::list<ULONG> lstAttachments;
- std::list<ULONG> lstDeleteAttach;
- std::string strQuery;
-
- er = GetSingleInstanceIds(lstDeleteObjects, &lstAttachments);
- if (er != erSuccess)
- return er;
-
- if (lstAttachments.empty())
- return er;
-
- strQuery =
- "DELETE FROM `singleinstances` "
- "WHERE `hierarchyid` IN (";
- for (auto i = lstDeleteObjects.cbegin(); i != lstDeleteObjects.cend(); ++i) {
- if (i != lstDeleteObjects.cbegin())
- strQuery += ",";
- strQuery += stringify(*i);
- }
- strQuery += ")";
- er = m_lpDatabase->DoDelete(strQuery);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::DeleteAttachments(): DoDelete failed %x", er);
- return er;
- }
-
- er = GetOrphanedSingleInstances(lstAttachments, &lstDeleteAttach);
- if (er != erSuccess)
- return er;
- if (!lstDeleteAttach.empty()) {
- er = DeleteAttachmentInstances(lstDeleteAttach, false);
- if (er != erSuccess)
- return er;
- }
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::DeleteAttachment(ULONG ulObjId, ULONG ulPropId) {
- return DeleteAttachment(ulObjId, ulPropId, false);
- }
- ECRESULT ECAttachmentStorage::DeleteAttachment(ULONG ulObjId, ULONG ulPropId, bool bReplace)
- {
- ECRESULT er;
- ULONG ulInstanceId = 0;
- bool bOrphan = false;
- std::string strQuery;
-
- er = GetSingleInstanceId(ulObjId, ulPropId, &ulInstanceId);
- if (er != erSuccess) {
- if (er == KCERR_NOT_FOUND)
- er = erSuccess;
- return er;
- }
-
- strQuery =
- "DELETE FROM `singleinstances` "
- "WHERE `hierarchyid` = " + stringify(ulObjId) + " "
- "AND `tag` = " + stringify(ulPropId);
- er = m_lpDatabase->DoDelete(strQuery);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::DeleteAttachment(): DoDelete failed %x", er);
- return er;
- }
-
- if (IsOrphanedSingleInstance(ulInstanceId, &bOrphan) == erSuccess && bOrphan) {
- er = DeleteAttachmentInstance(ulInstanceId, bReplace);
- if (er != erSuccess)
- return er;
- }
- return erSuccess;
- }
- ECRESULT ECAttachmentStorage::GetSize(ULONG ulObjId, ULONG ulPropId, size_t *lpulSize)
- {
- ECRESULT er;
- ULONG ulInstanceId = 0;
-
- er = GetSingleInstanceId(ulObjId, ulPropId, &ulInstanceId);
- if (er == KCERR_NOT_FOUND) {
- *lpulSize = 0;
- return erSuccess;
- } else if (er != erSuccess) {
- return er;
- }
- return GetSizeInstance(ulInstanceId, lpulSize);
- }
- ECDatabaseAttachment::ECDatabaseAttachment(ECDatabase *lpDatabase) :
- ECAttachmentStorage(lpDatabase, 0)
- {
- }
- bool ECDatabaseAttachment::ExistAttachmentInstance(ULONG ulInstanceId)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- strQuery = "SELECT instanceid FROM lob WHERE instanceid = " + stringify(ulInstanceId) + " LIMIT 1";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::ExistAttachmentInstance(): DoSelect failed %x", er);
- return false;
- }
- lpDBRow = m_lpDatabase->FetchRow(lpDBResult);
- if (!lpDBRow || !lpDBRow[0])
- return false;
- return true;
- }
- ECRESULT ECDatabaseAttachment::LoadAttachmentInstance(struct soap *soap, ULONG ulInstanceId, size_t *lpiSize, unsigned char **lppData)
- {
- ECRESULT er = erSuccess;
- size_t iSize = 0;
- size_t iReadSize = 0;
- unsigned char *lpData = NULL;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- DB_LENGTHS lpDBLen = NULL;
-
- strQuery = "SELECT SUM(LENGTH(val_binary)) FROM lob WHERE instanceid = " + stringify(ulInstanceId);
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::LoadAttachmentInstance(): DoSelect failed %x", er);
- goto exit;
- }
- lpDBRow = m_lpDatabase->FetchRow(lpDBResult);
- if (lpDBRow == NULL || lpDBRow[0] == NULL) {
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECDatabaseAttachment::LoadAttachmentInstance(): no row returned");
- goto exit;
- }
- iSize = strtoul(lpDBRow[0], NULL, 0);
-
- strQuery = "SELECT val_binary FROM lob WHERE instanceid = " + stringify(ulInstanceId) + " ORDER BY chunkid";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::LoadAttachmentInstance(): DoSelect(2) failed %x", er);
- goto exit;
- }
- lpData = s_alloc<unsigned char>(soap, iSize);
- while ((lpDBRow = m_lpDatabase->FetchRow(lpDBResult))) {
- if (lpDBRow[0] == NULL) {
-
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECDatabaseAttachment::LoadAttachmentInstance(): column contained NULL");
- goto exit;
- }
- lpDBLen = m_lpDatabase->FetchRowLengths(lpDBResult);
- memcpy(lpData + iReadSize, lpDBRow[0], lpDBLen[0]);
- iReadSize += lpDBLen[0];
- }
- *lpiSize = iReadSize;
- *lppData = lpData;
- exit:
- if (er != erSuccess && !soap)
- delete [] lpData;
- return er;
- }
- ECRESULT ECDatabaseAttachment::LoadAttachmentInstance(ULONG ulInstanceId, size_t *lpiSize, ECSerializer *lpSink)
- {
- ECRESULT er = erSuccess;
- size_t iReadSize = 0;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
- DB_LENGTHS lpDBLen = NULL;
-
- strQuery = "SELECT val_binary FROM lob WHERE instanceid = " + stringify(ulInstanceId) + " ORDER BY chunkid";
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::LoadAttachmentInstance(): DoSelect failed %x", er);
- return er;
- }
- while ((lpDBRow = m_lpDatabase->FetchRow(lpDBResult))) {
- if (lpDBRow[0] == NULL) {
-
- ec_log_err("ECDatabaseAttachment::LoadAttachmentInstance(): column contained NULL");
- return KCERR_DATABASE_ERROR;
- }
- lpDBLen = m_lpDatabase->FetchRowLengths(lpDBResult);
- er = lpSink->Write(lpDBRow[0], 1, lpDBLen[0]);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::LoadAttachmentInstance(): Write failed %x", er);
- return er;
- }
- iReadSize += lpDBLen[0];
- }
- *lpiSize = iReadSize;
- return erSuccess;
- }
- ECRESULT ECDatabaseAttachment::SaveAttachmentInstance(ULONG ulInstanceId, ULONG ulPropId, size_t iSize, unsigned char *lpData)
- {
- ECRESULT er;
- std::string strQuery;
-
- size_t iSizeLeft = iSize;
- size_t iPtr = 0;
- size_t ulChunk = 0;
- do {
- size_t iChunkSize = iSizeLeft < CHUNK_SIZE ? iSizeLeft : CHUNK_SIZE;
- strQuery = (std::string)"INSERT INTO lob (instanceid, chunkid, tag, val_binary) VALUES (" +
- stringify(ulInstanceId) + ", " + stringify(ulChunk) + ", " + stringify(ulPropId) +
- ", " + m_lpDatabase->EscapeBinary(lpData + iPtr, iChunkSize) + ")";
- er = m_lpDatabase->DoInsert(strQuery);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::SaveAttachmentInstance(): DoInsert failed %x", er);
- return er;
- }
- ++ulChunk;
- iSizeLeft -= iChunkSize;
- iPtr += iChunkSize;
- } while (iSizeLeft > 0);
- return erSuccess;
- }
- ECRESULT ECDatabaseAttachment::SaveAttachmentInstance(ULONG ulInstanceId, ULONG ulPropId, size_t iSize, ECSerializer *lpSource)
- {
- ECRESULT er;
- std::string strQuery;
- unsigned char szBuffer[CHUNK_SIZE] = {0};
-
- size_t iSizeLeft = iSize;
- size_t ulChunk = 0;
- while (iSizeLeft > 0) {
- size_t iChunkSize = iSizeLeft < CHUNK_SIZE ? iSizeLeft : CHUNK_SIZE;
- er = lpSource->Read(szBuffer, 1, iChunkSize);
- if (er != erSuccess)
- return er;
- strQuery = (std::string)"INSERT INTO lob (instanceid, chunkid, tag, val_binary) VALUES (" +
- stringify(ulInstanceId) + ", " + stringify(ulChunk) + ", " + stringify(ulPropId) +
- ", " + m_lpDatabase->EscapeBinary(szBuffer, iChunkSize) + ")";
- er = m_lpDatabase->DoInsert(strQuery);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::SaveAttachmentInstance(): DoInsert failed %x", er);
- return er;
- }
- ++ulChunk;
- iSizeLeft -= iChunkSize;
- }
- return erSuccess;
- }
- ECRESULT ECDatabaseAttachment::DeleteAttachmentInstances(const std::list<ULONG> &lstDeleteInstances, bool bReplace)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- strQuery = (std::string)"DELETE FROM lob WHERE instanceid IN (";
- for (auto iterDel = lstDeleteInstances.cbegin();
- iterDel != lstDeleteInstances.cend(); ++iterDel) {
- if (iterDel != lstDeleteInstances.cbegin())
- strQuery += ",";
- strQuery += stringify(*iterDel);
- }
- strQuery += ")";
- er = m_lpDatabase->DoDelete(strQuery);
- return er;
- }
- ECRESULT ECDatabaseAttachment::DeleteAttachmentInstance(ULONG ulInstanceId, bool bReplace)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- strQuery = (std::string)"DELETE FROM lob WHERE instanceid=" + stringify(ulInstanceId);
- er = m_lpDatabase->DoDelete(strQuery);
- return er;
- }
- ECRESULT ECDatabaseAttachment::GetSizeInstance(ULONG ulInstanceId, size_t *lpulSize, bool *lpbCompressed)
- {
- ECRESULT er = erSuccess;
- std::string strQuery;
- DB_RESULT lpDBResult;
- DB_ROW lpDBRow = NULL;
-
- strQuery = "SELECT SUM(LENGTH(val_binary)) FROM lob WHERE instanceid = " + stringify(ulInstanceId);
- er = m_lpDatabase->DoSelect(strQuery, &lpDBResult);
- if (er != erSuccess) {
- ec_log_err("ECAttachmentStorage::GetSizeInstance(): DoSelect failed %x", er);
- return er;
- }
- lpDBRow = m_lpDatabase->FetchRow(lpDBResult);
- if (lpDBRow == NULL || lpDBRow[0] == NULL) {
- ec_log_err("ECDatabaseAttachment::GetSizeInstance(): now row or column contained NULL");
- return KCERR_DATABASE_ERROR;
- }
-
- *lpulSize = strtoul(lpDBRow[0], NULL, 0);
- if (lpbCompressed)
- *lpbCompressed = false;
- return erSuccess;
- }
- ECRESULT ECDatabaseAttachment::Begin()
- {
- return erSuccess;
- }
- ECRESULT ECDatabaseAttachment::Commit()
- {
- return erSuccess;
- }
- ECRESULT ECDatabaseAttachment::Rollback()
- {
- return erSuccess;
- }
- ECFileAttachment::ECFileAttachment(ECDatabase *lpDatabase,
- const std::string &basepath, unsigned int ulCompressionLevel,
- const bool force_changes_to_disk) :
- ECAttachmentStorage(lpDatabase, ulCompressionLevel),
- m_basepath(basepath)
- {
- if (m_basepath.empty())
- m_basepath = "/var/lib/kopano";
- this -> force_changes_to_disk = force_changes_to_disk;
- if (force_changes_to_disk) {
- m_dirp = opendir(m_basepath.c_str());
- if (m_dirp)
- m_dirFd = dirfd(m_dirp);
- if (m_dirFd == -1)
- ec_log_warn("Problem opening directory file \"%s\": %s - attachment storage atomicity not guaranteed", m_basepath.c_str(), strerror(errno));
- }
- attachment_size_safety_limit = 512 * 1024 * 1024;
- }
- ECFileAttachment::~ECFileAttachment()
- {
- if (m_dirp != NULL)
- closedir(m_dirp);
- if (m_bTransaction)
- assert(false);
- }
- bool ECFileAttachment::ExistAttachmentInstance(ULONG ulInstanceId)
- {
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression);
- struct stat st;
- if (stat(filename.c_str(), &st) == -1) {
- filename = CreateAttachmentFilename(ulInstanceId, !m_bFileCompression);
- if (stat(filename.c_str(), &st) == -1)
- return false;
- }
- return true;
- }
- static ssize_t gzread_retry(gzFile fp, void *data, size_t uclen)
- {
- ssize_t read_total = 0;
- auto buf = static_cast<char *>(data);
- if (uclen == 0)
-
- return 0;
- while (uclen > 0) {
- int chunk_size = (uclen > INT_MAX) ? INT_MAX : uclen;
- int ret = gzread(fp, buf, chunk_size);
-
- int saved_errno = errno;
- int zerror;
- const char *zerrstr = gzerror(fp, &zerror);
- if (ret < 0 && zerror == Z_ERRNO &&
- (saved_errno == EINTR || saved_errno == EAGAIN))
-
- continue;
- if (ret < 0 && zerror == Z_ERRNO) {
- ec_log_err("gzread: %s (%d): %s",
- zerrstr, zerror, strerror(saved_errno));
- return ret;
- }
- if (ret < 0) {
- ec_log_err("gzread: %s (%d)", zerrstr, zerror);
- return ret;
- }
- if (ret == 0)
-
- break;
- buf += ret;
- read_total += ret;
- uclen -= ret;
- }
- return read_total;
- }
- static ssize_t gzwrite_retry(gzFile fp, const void *data, size_t uclen)
- {
- size_t wrote_total = 0;
- auto buf = static_cast<const char *>(data);
- if (uclen == 0)
-
- return 0;
- while (uclen > 0) {
- int chunk_size = (uclen > INT_MAX) ? INT_MAX : uclen;
- int ret = gzwrite(fp, buf, chunk_size);
- int saved_errno = errno;
- int zerror;
- const char *zerrstr = gzerror(fp, &zerror);
- if (ret < 0 && zerror == Z_ERRNO &&
- (saved_errno == EINTR || saved_errno == EAGAIN))
-
- continue;
- if (ret < 0 && zerror == Z_ERRNO) {
- ec_log_err("gzwrite: %s (%d): %s",
- zerrstr, zerror, strerror(saved_errno));
- return ret;
- }
- if (ret < 0) {
- ec_log_err("gzwrite: %s (%d)", zerrstr, zerror);
- return ret;
- }
- if (ret == 0)
-
- break;
- buf += ret;
- wrote_total += ret;
- uclen -= ret;
- }
- return wrote_total;
- }
- bool ECFileAttachment::VerifyInstanceSize(const ULONG instanceId, const size_t expectedSize, const std::string & filename) {
- bool bCompressed = false;
- size_t ulSize = 0;
- if (GetSizeInstance(instanceId, &ulSize, &bCompressed) != erSuccess) {
- ec_log_debug("ECFileAttachment::VerifyInstanceSize(): Failed verifying size of \"%s\"", filename.c_str());
- return false;
- }
- if (ulSize != expectedSize) {
- ec_log_debug("ECFileAttachment::VerifyInstanceSize(): Uncompressed size unexpected for \"%s\": expected %lu, got %lu.", filename.c_str(), static_cast<unsigned long>(expectedSize), static_cast<unsigned long>(ulSize));
- return false;
- }
- if (ulSize > attachment_size_safety_limit)
- ec_log_debug("ECFileAttachment::VerifyInstanceSize(): Overly large file (%lu/%lu): \"%s\"", static_cast<unsigned long>(expectedSize), static_cast<unsigned long>(ulSize), filename.c_str());
- return true;
- }
- ECRESULT ECFileAttachment::LoadAttachmentInstance(struct soap *soap, ULONG ulInstanceId, size_t *lpiSize, unsigned char **lppData) {
- ECRESULT er = erSuccess;
- string filename;
- unsigned char *lpData = NULL;
- bool bCompressed = false;
- int fd = -1;
- gzFile gzfp = NULL;
- *lpiSize = 0;
- filename = CreateAttachmentFilename(ulInstanceId, bCompressed);
- fd = open(filename.c_str(), O_RDONLY);
- if (fd < 0 && errno != ENOENT) {
-
- ec_log_err("K-1561: cannot open attachment \"%s\": %s", filename.c_str(), strerror(errno));
- return KCERR_NO_ACCESS;
- } else if (fd < 0) {
-
- bCompressed = true;
- filename = CreateAttachmentFilename(ulInstanceId, bCompressed);
- fd = open(filename.c_str(), O_RDONLY);
- if (fd < 0) {
- ec_log_err("K-1562: cannot open attachment \"%s\": %s", filename.c_str(), strerror(errno));
- return KCERR_NOT_FOUND;
- }
- }
- my_readahead(fd);
-
- if (bCompressed) {
- unsigned char *temp = NULL;
-
- gzfp = gzdopen(fd, "rb");
- if (!gzfp) {
-
-
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): cannot gzopen attachment \"%s\": %s", filename.c_str(), strerror(errno));
- er = KCERR_UNKNOWN;
- goto exit;
- }
- #if ZLIB_VERNUM >= 0x1240
- if (gzbuffer(gzfp, ZLIB_BUFFER_SIZE) == -1)
- ec_log_warn("gzbuffer failed");
- #endif
- size_t memory_block_size = 0;
- for(;;)
- {
- int ret = -1;
- if (memory_block_size == *lpiSize) {
- if (memory_block_size)
- memory_block_size *= 2;
- else
- memory_block_size = CHUNK_SIZE;
- auto new_temp = static_cast<unsigned char *>(realloc(temp, memory_block_size));
- if (!new_temp) {
-
- free(temp);
- temp = NULL;
- *lpiSize = 0;
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Out of memory while reading \"%s\"", filename.c_str());
- er = KCERR_UNABLE_TO_COMPLETE;
- goto exit;
- }
- temp = new_temp;
- }
- ret = gzread_retry(gzfp, &temp[*lpiSize], memory_block_size - *lpiSize);
- if (ret < 0) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Error while gzreading attachment data from \"%s\"", filename.c_str());
-
-
- *lpiSize = 0;
- break;
- }
- if (ret == 0)
- break;
- *lpiSize += ret;
- if (*lpiSize >= attachment_size_safety_limit) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Size safety limit (%lu) reached for \"%s\" (compressed)", static_cast<unsigned long>(attachment_size_safety_limit), filename.c_str());
-
-
- *lpiSize = 0;
- break;
- }
- }
- if (er == erSuccess)
- (void)VerifyInstanceSize(ulInstanceId, *lpiSize, filename);
- if (er == erSuccess) {
- lpData = s_alloc<unsigned char>(soap, *lpiSize);
- memcpy(lpData, temp, *lpiSize);
- }
- free(temp);
- }
- else {
- ssize_t lReadSize = 0;
- struct stat st;
- if (fstat(fd, &st) == -1)
- {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Error while doing fstat on \"%s\": %s", filename.c_str(), strerror(errno));
-
- *lpiSize = 0;
- lpData = s_alloc<unsigned char>(soap, *lpiSize);
- *lppData = lpData;
- goto exit;
- }
- *lpiSize = st.st_size;
- if (*lpiSize >= attachment_size_safety_limit) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Size safety limit (%lu) reached for \"%s\" (uncompressed)", static_cast<unsigned long>(attachment_size_safety_limit), filename.c_str());
-
- *lpiSize = 0;
- lpData = s_alloc<unsigned char>(soap, *lpiSize);
- *lppData = lpData;
- goto exit;
- }
- lpData = s_alloc<unsigned char>(soap, *lpiSize);
-
- lReadSize = read_retry(fd, lpData, *lpiSize);
- if (lReadSize < 0) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Error while reading attachment data from \"%s\": %s", filename.c_str(), strerror(errno));
-
- *lpiSize = 0;
- *lppData = lpData;
- goto exit;
- }
- if (lReadSize != static_cast<ssize_t>(*lpiSize)) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Short read while reading attachment data from \"%s\": expected %lu, got %lu.", filename.c_str(), static_cast<unsigned long>(*lpiSize), static_cast<unsigned long>(lReadSize));
-
- *lpiSize = 0;
- *lppData = lpData;
- goto exit;
- }
- }
- *lppData = lpData;
- exit:
- if (er != erSuccess)
- delete [] lpData;
- if (gzfp)
- {
- gzclose(gzfp);
- fd = -1;
- }
- if (fd != -1)
- close(fd);
- return er;
- }
- ECRESULT ECFileAttachment::LoadAttachmentInstance(ULONG ulInstanceId, size_t *lpiSize, ECSerializer *lpSink) {
- ECRESULT er = erSuccess;
- string filename;
- bool bCompressed = false;
- char buffer[CHUNK_SIZE];
- int fd = -1;
- gzFile gzfp = NULL;
- *lpiSize = 0;
- filename = CreateAttachmentFilename(ulInstanceId, bCompressed);
- fd = open(filename.c_str(), O_RDONLY);
- if (fd < 0 && errno != ENOENT) {
-
- ec_log_err("K-1563: cannot open attachment \"%s\": %s", filename.c_str(), strerror(errno));
- return KCERR_NO_ACCESS;
- } else if (fd < 0) {
-
- bCompressed = true;
- filename = CreateAttachmentFilename(ulInstanceId, bCompressed);
- fd = open(filename.c_str(), O_RDONLY);
- if (fd < 0) {
- ec_log_err("K-1564: cannot open attachment \"%s\": %s", filename.c_str(), strerror(errno));
- return KCERR_NOT_FOUND;
- }
- }
- my_readahead(fd);
- if (bCompressed) {
-
- gzfp = gzdopen(fd, "rb");
- if (!gzfp) {
- er = KCERR_UNKNOWN;
- goto exit;
- }
- #if ZLIB_VERNUM >= 0x1240
- if (gzbuffer(gzfp, ZLIB_BUFFER_SIZE) == -1)
- ec_log_warn("gzbuffer failed");
- #endif
- for(;;) {
- ssize_t lReadNow = gzread_retry(gzfp, buffer, sizeof(buffer));
- if (lReadNow < 0) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(): Error while gzreading attachment data from \"%s\".", filename.c_str());
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
- if (lReadNow == 0)
- break;
- lpSink->Write(buffer, 1, lReadNow);
- *lpiSize += lReadNow;
- }
- if (er == erSuccess)
- (void)VerifyInstanceSize(ulInstanceId, *lpiSize, filename);
- }
- else {
- for(;;) {
- ssize_t lReadNow = read_retry(fd, buffer, sizeof(buffer));
- if (lReadNow < 0) {
- ec_log_err("ECFileAttachment::LoadAttachmentInstance(SOAP): Error while reading attachment data from \"%s\": %s", filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
- if (lReadNow == 0)
- break;
- lpSink->Write(buffer, 1, lReadNow);
- *lpiSize += lReadNow;
- }
- }
- exit:
- if (gzfp) {
- gzclose(gzfp);
- fd = -1;
- }
- if (fd != -1)
- close(fd);
- return er;
- }
- static bool EvaluateCompressibleness(const uint8_t *const lpData, const size_t iSize) {
-
-
-
-
-
-
-
-
-
- if (iSize <= 4096)
- return false;
-
- if (lpData[0] == 'P' && lpData[1] == 'K' && lpData[2] == 0x03 && lpData[3] == 0x04)
- return false;
-
- if (lpData[0] == 0xff && lpData[1] == 0xd8)
- return false;
-
- if (lpData[0] == 'G' && lpData[1] == 'I' && lpData[2] == 'F' && lpData[3] == '8')
- return false;
-
- if (lpData[0] == 0x89 && lpData[1] == 0x50 && lpData[2] == 0x4e && lpData[3] == 0x47 && lpData[4] == 0x0d && lpData[5] == 0x0a && lpData[6] == 0x1a && lpData[7] == 0x0a)
- return false;
-
- if (lpData[0] == 0x52 && lpData[1] == 0x61 && lpData[2] == 0x72 && lpData[3] == 0x21 && lpData[4] == 0x1A && lpData[5] == 0x07)
- return false;
-
- if (lpData[0] == 0x1f && lpData[1] == 0x8b)
- return false;
-
- if (lpData[0] == 0xfd && lpData[1] == '7' && lpData[2] == 'z' && lpData[3] == 'X' && lpData[4] == 'Z' && lpData[5] == 0x00)
- return false;
-
- if (lpData[0] == 0x42 && lpData[1] == 0x5A && lpData[2] == 0x68)
- return false;
-
- if ((lpData[0] == 0x49 && lpData[1] == 0x44 && lpData[2] == 0x33) || (lpData[0] == 0xff && lpData[1] == 0xfb))
- return false;
-
- return true;
- }
- ECRESULT ECFileAttachment::SaveAttachmentInstance(ULONG ulInstanceId,
- ULONG ulPropId, size_t iSize, unsigned char *lpData)
- {
- bool compressible = EvaluateCompressibleness(lpData, iSize);
- bool compressAttachment = compressible ? m_bFileCompression && iSize : false;
- ECRESULT er = erSuccess;
- string filename = CreateAttachmentFilename(ulInstanceId, compressAttachment);
- gzFile gzfp = NULL;
- int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR | S_IRGRP);
- if (fd < 0) {
- ec_log_err("Unable to open attachment \"%s\" for writing: %s", filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
-
- if (compressAttachment) {
- gzfp = gzdopen(fd, std::string("wb" + m_CompressionLevel).c_str());
- if (!gzfp) {
- ec_log_err("Unable to gzopen attachment \"%s\" for writing: %s", filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
- ssize_t iWritten = gzwrite_retry(gzfp, lpData, iSize);
- if (iWritten != static_cast<ssize_t>(iSize)) {
- ec_log_err("Unable to gzwrite %lu bytes to attachment \"%s\", returned %lu.",
- static_cast<unsigned long>(iSize), filename.c_str(), static_cast<unsigned long>(iWritten));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
- }
- else {
- give_filesize_hint(fd, iSize);
- ssize_t iWritten = write_retry(fd, lpData, iSize);
- if (iWritten != static_cast<ssize_t>(iSize)) {
- ec_log_err("Unable to write %lu bytes to attachment \"%s\": %s. Returned %lu.",
- static_cast<unsigned long>(iSize), filename.c_str(), strerror(errno), static_cast<unsigned long>(iWritten));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
- }
-
- if(m_bTransaction)
- m_setNewAttachment.insert(ulInstanceId);
- exit:
- if (gzfp != NULL) {
- int ret = gzclose(gzfp);
- if (ret != Z_OK) {
- ec_log_err("gzclose on attachment \"%s\" failed: %s",
- filename.c_str(), (ret == Z_ERRNO) ? strerror(errno) : gzerror(gzfp, NULL));
- er = KCERR_DATABASE_ERROR;
- }
- fd = -1;
- }
- if (fd != -1)
- close(fd);
- return er;
- }
- ECRESULT ECFileAttachment::SaveAttachmentInstance(ULONG ulInstanceId,
- ULONG ulPropId, size_t iSize, ECSerializer *lpSource)
- {
- ECRESULT er = erSuccess;
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression);
- unsigned char szBuffer[CHUNK_SIZE];
- size_t iSizeLeft = iSize;
- int fd = open(filename.c_str(), O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
- if (fd == -1) {
- ec_log_err("Unable to open attachment \"%s\" for writing: %s.",
- filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
-
- if (m_bFileCompression) {
- gzFile gzfp = gzdopen(fd, std::string("wb" + m_CompressionLevel).c_str());
- if (!gzfp) {
- ec_log_err("Unable to gzdopen attachment \"%s\" for writing: %s",
- filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- goto exit;
- }
-
- if (m_bTransaction)
- m_setNewAttachment.insert(ulInstanceId);
- while (iSizeLeft > 0) {
- size_t iChunkSize = iSizeLeft < CHUNK_SIZE ? iSizeLeft : CHUNK_SIZE;
- er = lpSource->Read(szBuffer, 1, iChunkSize);
- if (er != erSuccess) {
- ec_log_err("Problem retrieving attachment from ECSource: %s (0x%x)", GetMAPIErrorMessage(kcerr_to_mapierr(er, ~0U )), er);
- er = KCERR_DATABASE_ERROR;
- break;
- }
- ssize_t iWritten = gzwrite_retry(gzfp, szBuffer, iChunkSize);
- if (iWritten != static_cast<ssize_t>(iChunkSize)) {
- ec_log_err("Unable to gzwrite %lu bytes to attachment \"%s\", returned %lu",
- static_cast<unsigned long>(iChunkSize), filename.c_str(), static_cast<unsigned long>(iWritten));
- er = KCERR_DATABASE_ERROR;
- break;
- }
- iSizeLeft -= iChunkSize;
- }
- if (er == erSuccess) {
- if (gzflush(gzfp, Z_FINISH)) {
- int saved_errno = errno;
- int zerror;
- const char *zstrerr = gzerror(gzfp, &zerror);
- ec_log_err("gzflush failed: %s (%d)", zstrerr, zerror);
- if (zerror == Z_ERRNO)
- ec_log_err("gzflush failed: stdio says: %s", strerror(saved_errno));
- er = KCERR_DATABASE_ERROR;
- }
- if (force_changes_to_disk && !force_buffers_to_disk(fd)) {
- ec_log_warn("Problem syncing file \"%s\": %s", filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- }
- }
- int ret = gzclose(gzfp);
- if (ret != Z_OK) {
- ec_log_err("Problem closing file \"%s\": %s",
- filename.c_str(), (ret == Z_ERRNO) ? strerror(errno) : gzerror(gzfp, NULL));
- er = KCERR_DATABASE_ERROR;
- }
-
- fd = -1;
- }
- else {
- give_filesize_hint(fd, iSize);
-
- if (m_bTransaction)
- m_setNewAttachment.insert(ulInstanceId);
- while (iSizeLeft > 0) {
- size_t iChunkSize = iSizeLeft < CHUNK_SIZE ? iSizeLeft : CHUNK_SIZE;
- er = lpSource->Read(szBuffer, 1, iChunkSize);
- if (er != erSuccess) {
- ec_log_err("Problem retrieving attachment from ECSource: %s (0x%x)", GetMAPIErrorMessage(kcerr_to_mapierr(er, ~0U)), er);
- er = KCERR_DATABASE_ERROR;
- break;
- }
- ssize_t iWritten = write_retry(fd, szBuffer, iChunkSize);
- if (iWritten != static_cast<ssize_t>(iChunkSize)) {
- ec_log_err("Unable to write %lu bytes to streaming attachment: %s", static_cast<unsigned long>(iChunkSize), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- break;
- }
- iSizeLeft -= iChunkSize;
- }
- if (er == erSuccess) {
- if (force_changes_to_disk && !force_buffers_to_disk(fd)) {
- ec_log_warn("Problem syncing file \"%s\": %s", filename.c_str(), strerror(errno));
- er = KCERR_DATABASE_ERROR;
- }
- }
- close(fd);
- fd = -1;
- }
- exit:
- if (er == erSuccess && m_dirFd != -1 && fsync(m_dirFd) == -1)
- ec_log_warn("Problem syncing parent directory of \"%s\": %s", filename.c_str(), strerror(errno));
- if (fd != -1)
- close(fd);
- return er;
- }
- ECRESULT ECFileAttachment::DeleteAttachmentInstances(const std::list<ULONG> &lstDeleteInstances, bool bReplace)
- {
- ECRESULT er = erSuccess;
- int errors = 0;
- for (auto del_id : lstDeleteInstances) {
- er = this->DeleteAttachmentInstance(del_id, bReplace);
- if (er != erSuccess)
- ++errors;
- }
- if (errors)
- ec_log_err("ECFileAttachment::DeleteAttachmentInstances(): %x delete fails", errors);
- return errors == 0 ? erSuccess : KCERR_DATABASE_ERROR;
- }
- ECRESULT ECFileAttachment::MarkAttachmentForDeletion(ULONG ulInstanceId)
- {
- ECRESULT er;
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression);
- if(rename(filename.c_str(), string(filename+".deleted").c_str()) == 0)
- return erSuccess;
- if (errno == ENOENT) {
-
- filename = CreateAttachmentFilename(ulInstanceId, !m_bFileCompression);
- if(rename(filename.c_str(), string(filename+".deleted").c_str()) == 0)
- return erSuccess;
- }
-
- if (errno == EACCES || errno == EPERM)
- er = KCERR_NO_ACCESS;
- else if (errno == ENOENT)
- er = KCERR_NOT_FOUND;
- else {
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECFileAttachment::MarkAttachmentForDeletion(): cannot mark %u", ulInstanceId);
- }
- return er;
- }
- ECRESULT ECFileAttachment::RestoreMarkedAttachment(ULONG ulInstanceId)
- {
- ECRESULT er;
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression);
- if(rename(string(filename+".deleted").c_str(), filename.c_str()) == 0)
- return erSuccess;
- if (errno == ENOENT) {
-
- filename = CreateAttachmentFilename(ulInstanceId, !m_bFileCompression);
- if(rename(string(filename+".deleted").c_str(), filename.c_str()) == 0)
- return erSuccess;
- }
-
- if (errno == EACCES || errno == EPERM)
- er = KCERR_NO_ACCESS;
- else if (errno == ENOENT)
- er = KCERR_NOT_FOUND;
- else {
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECFileAttachment::RestoreMarkedAttachment(): cannot mark %u", ulInstanceId);
- }
- return er;
- }
- ECRESULT ECFileAttachment::DeleteMarkedAttachment(ULONG ulInstanceId)
- {
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression) + ".deleted";
- if (unlink(filename.c_str()) == 0)
- return erSuccess;
- if (errno == ENOENT) {
- filename = CreateAttachmentFilename(ulInstanceId, !m_bFileCompression) + ".deleted";
- if (unlink(filename.c_str()) == 0)
- return erSuccess;
- }
- ECRESULT er = erSuccess;
- ec_log_err("%s unlink %s failed: %s\n", __PRETTY_FUNCTION__, filename.c_str(), strerror(errno));
- if (errno == EACCES || errno == EPERM)
- er = KCERR_NO_ACCESS;
- else if (errno != ENOENT) {
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECFileAttachment::DeleteMarkedAttachment() cannot delete instance %u", ulInstanceId);
- }
- return er;
- }
- ECRESULT ECFileAttachment::DeleteAttachmentInstance(ULONG ulInstanceId, bool bReplace)
- {
- ECRESULT er = erSuccess;
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression);
-
- if(m_bTransaction) {
- if (bReplace) {
- er = MarkAttachmentForDeletion(ulInstanceId);
- if (er != erSuccess && er != KCERR_NOT_FOUND) {
- assert(false);
- return er;
- } else if(er != KCERR_NOT_FOUND) {
- m_setMarkedAttachment.insert(ulInstanceId);
- }
- er = erSuccess;
- } else {
- m_setDeletedAttachment.insert(ulInstanceId);
- }
- return er;
- }
- if (unlink(filename.c_str()) != 0) {
- if (errno == ENOENT){
- filename = CreateAttachmentFilename(ulInstanceId, !m_bFileCompression);
- if (unlink(filename.c_str()) == 0)
- return erSuccess;
- }
- if (errno == EACCES || errno == EPERM)
- er = KCERR_NO_ACCESS;
- else if (errno != ENOENT) {
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECFileAttachment::DeleteAttachmentInstance() id %u fail", ulInstanceId);
- }
- }
- return er;
- }
- std::string ECFileAttachment::CreateAttachmentFilename(ULONG ulInstanceId, bool bCompressed)
- {
- string filename;
- unsigned int l1, l2;
- l1 = ulInstanceId % ATTACH_PATHDEPTH_LEVEL1;
- l2 = (ulInstanceId / ATTACH_PATHDEPTH_LEVEL1) % ATTACH_PATHDEPTH_LEVEL2;
- filename = m_basepath + PATH_SEPARATOR + stringify(l1) + PATH_SEPARATOR + stringify(l2) + PATH_SEPARATOR + stringify(ulInstanceId);
- if (bCompressed)
- filename += ".gz";
- return filename;
- }
- ECRESULT ECFileAttachment::GetSizeInstance(ULONG ulInstanceId, size_t *lpulSize, bool *lpbCompressed)
- {
- ECRESULT er = erSuccess;
- string filename = CreateAttachmentFilename(ulInstanceId, m_bFileCompression);
- bool bCompressed = m_bFileCompression;
- struct stat st;
-
- int fd = open(filename.c_str(), O_RDONLY);
- if (fd == -1) {
- filename = CreateAttachmentFilename(ulInstanceId, !m_bFileCompression);
- bCompressed = !m_bFileCompression;
- fd = open(filename.c_str(), O_RDONLY);
- }
- if (fd == -1) {
- ec_log_err("ECFileAttachment::GetSizeInstance(): file \"%s\" cannot be accessed: %s", filename.c_str(), strerror(errno));
- er = KCERR_NOT_FOUND;
- goto exit;
- }
-
- if (fstat(fd, &st) == -1) {
- ec_log_err("ECFileAttachment::GetSizeInstance(): file \"%s\" fstat failed: %s", filename.c_str(), strerror(errno));
-
- goto exit;
- }
- if (!bCompressed) {
- *lpulSize = st.st_size;
- } else if (st.st_size >= 4) {
-
-
-
- if (lseek(fd, -4, SEEK_END) == -1) {
- ec_log_err("ECFileAttachment::GetSizeInstance(): file \"%s\" fseek (compressed file) failed: %s", filename.c_str(), strerror(errno));
-
- goto exit;
- }
-
- uint32_t atsize;
- if (read_retry(fd, &atsize, 4) != 4) {
- ec_log_err("ECFileAttachment::GetSizeInstance(): file \"%s\" fread failed: %s", filename.c_str(), strerror(errno));
-
- goto exit;
- }
- if (st.st_size >= 40 && atsize == 0) {
- ec_log_warn("ECFileAttachment: %s seems to be an unsupported multi-stream gzip file (KC-104).", filename.c_str());
-
- goto exit;
- }
- *lpulSize = atsize;
- } else {
- *lpulSize = 0;
- ec_log_debug("ECFileAttachment::GetSizeInstance(): file \"%s\" is truncated!", filename.c_str());
-
- }
- if (lpbCompressed)
- *lpbCompressed = bCompressed;
- exit:
- if (fd != -1)
- close(fd);
- return er;
- }
- ECRESULT ECFileAttachment::Begin()
- {
- if(m_bTransaction) {
-
- assert(false);
- return erSuccess;
- }
-
- m_setNewAttachment.clear();
- m_setDeletedAttachment.clear();
- m_setMarkedAttachment.clear();
- m_bTransaction = true;
- return erSuccess;
- }
- ECRESULT ECFileAttachment::Commit()
- {
- ECRESULT er = erSuccess;
- bool bError = false;
-
- if(!m_bTransaction) {
- assert(false);
- return erSuccess;
- }
-
- m_bTransaction = false;
-
- for (auto att_id : m_setDeletedAttachment)
- if (DeleteAttachmentInstance(att_id, false) != erSuccess)
- bError = true;
-
- for (auto att_id : m_setMarkedAttachment)
- if (DeleteMarkedAttachment(att_id) != erSuccess)
- bError = true;
- if (bError) {
- assert(false);
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECFileAttachment::Commit() error during commit");
- }
- m_setNewAttachment.clear();
- m_setDeletedAttachment.clear();
- m_setMarkedAttachment.clear();
- return er;
- }
- ECRESULT ECFileAttachment::Rollback()
- {
- ECRESULT er = erSuccess;
- bool bError = false;
- if(!m_bTransaction) {
- assert(false);
- return erSuccess;
- }
-
- m_bTransaction = false;
-
- m_setDeletedAttachment.clear();
-
-
- for (auto att_id : m_setNewAttachment)
- if (DeleteAttachmentInstance(att_id, false) != erSuccess)
- bError = true;
-
- for (auto att_id : m_setMarkedAttachment)
- if (RestoreMarkedAttachment(att_id) != erSuccess)
- bError = true;
- m_setNewAttachment.clear();
- m_setMarkedAttachment.clear();
-
- if (bError) {
- assert(false);
- er = KCERR_DATABASE_ERROR;
- ec_log_err("ECFileAttachment::Rollback() error");
- }
- return er;
- }
- }
|