void SG_vfile__end( SG_context* pCtx, SG_vfile** ppvf, const SG_vhash* pvh ) { SG_string* pstr = NULL; SG_vfile* pvf = NULL; SG_NULLARGCHECK_RETURN(ppvf); pvf = *ppvf; if (pvh) { SG_ARGCHECK_RETURN( !(pvf->mode & SG_FILE_RDONLY) , pvh ); SG_ERR_CHECK( SG_STRING__ALLOC(pCtx, &pstr) ); SG_ERR_CHECK( SG_vhash__to_json(pCtx, pvh,pstr) ); SG_ERR_CHECK( SG_file__seek(pCtx, pvf->pFile, 0) ); SG_ERR_CHECK( SG_file__truncate(pCtx, pvf->pFile) ); #if TRACE_VFILE SG_ERR_IGNORE( SG_console(pCtx, SG_CS_STDERR, "VFileEnd: Writing %d bytes to file.\n", SG_string__length_in_bytes(pstr)) ); #endif SG_ERR_CHECK( SG_file__write(pCtx, pvf->pFile, SG_string__length_in_bytes(pstr), (const SG_byte *)SG_string__sz(pstr), NULL) ); SG_STRING_NULLFREE(pCtx, pstr); } else { if (!(pvf->mode & SG_FILE_RDONLY)) { SG_ERR_CHECK( SG_file__seek(pCtx, pvf->pFile, 0) ); SG_ERR_CHECK( SG_file__truncate(pCtx, pvf->pFile) ); #if TRACE_VFILE SG_ERR_IGNORE( SG_console(pCtx, SG_CS_STDERR, "VFileEnd: Truncating file.\n") ); #endif } } SG_FILE_NULLCLOSE(pCtx, pvf->pFile); SG_NULLFREE(pCtx, pvf); *ppvf = NULL; return; fail: SG_STRING_NULLFREE(pCtx, pstr); }
void SG_dbrecord__save_to_repo(SG_context* pCtx, SG_dbrecord * pRecord, SG_repo * pRepo, SG_repo_tx_handle* pRepoTx, SG_uint64* iBlobFullLength) { SG_string * pString = NULL; char* pszHidComputed = NULL; SG_uint32 iLength = 0; SG_NULLARGCHECK_RETURN(pRepo); SG_NULLARGCHECK_RETURN(pRecord); // serialize the dbrecord into JSON string. SG_ERR_CHECK( SG_STRING__ALLOC(pCtx, &pString) ); SG_ERR_CHECK( SG_dbrecord__to_json(pCtx, pRecord,pString) ); #if 0 printf("%s\n", SG_string__sz(pString)); // TODO remove this #endif // remember the length of the blob iLength = SG_string__length_in_bytes(pString); *iBlobFullLength = (SG_uint64) iLength; // create a blob in the repository using the JSON string. this computes the HID and returns it. SG_repo__store_blob_from_memory(pCtx, pRepo,pRepoTx, NULL, // TODO pass the record id into here SG_FALSE, (const SG_byte *)SG_string__sz(pString), iLength, &pszHidComputed); if (!SG_context__has_err(pCtx) || SG_context__err_equals(pCtx, SG_ERR_BLOBFILEALREADYEXISTS)) { // freeeze the dbrecord memory-object and effectively make it read-only // from this point forward. we give up our ownership of the HID. SG_ASSERT( pszHidComputed ); _sg_dbrecord__freeze(pCtx, pRecord, pszHidComputed); pszHidComputed = NULL; SG_STRING_NULLFREE(pCtx, pString); return; // return _OK or __BLOBFILEALREADYEXISTS } fail: SG_STRING_NULLFREE(pCtx, pString); SG_NULLFREE(pCtx, pszHidComputed); }
void SG_password__set( SG_context* pCtx, const char *szRepoSpec, SG_string *pstrUserName, SG_string *pstrPassword) { SG_string* pstrTarget = NULL; LPWSTR pwszTarget = NULL; LPWSTR pwszPassword = NULL; SG_uint32 lenPassword = 0; LPWSTR pwszUserName = NULL; CREDENTIAL cred; SG_NULLARGCHECK_RETURN(szRepoSpec); SG_NULLARGCHECK_RETURN(pstrUserName); SG_NULLARGCHECK_RETURN(pstrPassword); if (!SG_string__length_in_bytes(pstrUserName)) SG_ERR_THROW2_RETURN(SG_ERR_INVALIDARG, (pCtx, "%s", "pstrUserName is empty")); SG_ERR_CHECK( _get_key(pCtx, szRepoSpec, SG_string__sz(pstrUserName), &pstrTarget) ); SG_ERR_CHECK( SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrTarget), &pwszTarget, NULL) ); SG_ERR_CHECK( SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrPassword), &pwszPassword, &lenPassword) ); SG_ERR_CHECK( SG_utf8__extern_to_os_buffer__wchar(pCtx, SG_string__sz(pstrUserName), &pwszUserName, NULL) ); SG_zero(cred); cred.Type = CRED_TYPE_GENERIC; cred.TargetName = pwszTarget; cred.CredentialBlob = (LPBYTE)pwszPassword; cred.CredentialBlobSize = lenPassword*sizeof(wchar_t); cred.Persist = CRED_PERSIST_LOCAL_MACHINE; // unsupported on Windows Vista Home Basic, Windows Vista Home Premium, Windows Vista Starter, and Windows XP Home Edition cred.UserName = pwszUserName; if ( !CredWriteW(&cred, 0) ) SG_ERR_THROW2( SG_ERR_GETLASTERROR(GetLastError()), (pCtx, "%s", "unable to save credentials") ); /* fall through */ fail: SG_STRING_NULLFREE(pCtx, pstrTarget); SG_NULLFREE(pCtx, pwszTarget); SG_NULLFREE(pCtx, pwszPassword); SG_NULLFREE(pCtx, pwszUserName); }
void SG_dbrecord__freeze(SG_context* pCtx, SG_dbrecord* prec, SG_repo * pRepo, const char** ppid) { SG_string* pstr = NULL; char* psz_hid = NULL; SG_NULLARGCHECK_RETURN(prec); SG_ERR_CHECK( SG_STRING__ALLOC(pCtx, &pstr) ); SG_ERR_CHECK( SG_dbrecord__to_json(pCtx, prec, pstr) ); SG_ERR_CHECK( SG_repo__alloc_compute_hash__from_bytes(pCtx,pRepo, SG_string__length_in_bytes(pstr), (SG_byte *)SG_string__sz(pstr), &psz_hid) ); SG_STRING_NULLFREE(pCtx, pstr); _sg_dbrecord__freeze(pCtx, prec, psz_hid); *ppid = psz_hid; return; fail: SG_STRING_NULLFREE(pCtx, pstr); }
static void _format_comment(SG_context* pCtx, SG_bool onlyIncludeFirstLine, const char* szLinePrefix, const char* szComment, char** ppszReturn) { SG_bool bFoundLineBreak = SG_FALSE; SG_string* pstr = NULL; SG_uint32 lenPrefix = SG_STRLEN(szLinePrefix); SG_uint32 offset; { const char* pos; for (pos = szComment; *pos; pos++) { if (*pos == SG_CR || *pos == SG_LF) { bFoundLineBreak = SG_TRUE; break; } } if (!bFoundLineBreak) return; if(onlyIncludeFirstLine) { SG_ERR_CHECK( SG_STRING__ALLOC__BUF_LEN(pCtx, &pstr, (const SG_byte*)szComment, (SG_uint32)(pos-szComment)) ); SG_ERR_CHECK( SG_string__sizzle(pCtx, &pstr, (SG_byte**)ppszReturn, NULL) ); return; } offset = (SG_uint32)(pos-szComment); } SG_ERR_CHECK( SG_STRING__ALLOC__SZ(pCtx, &pstr, szComment) ); while (offset < SG_string__length_in_bytes(pstr)) { SG_byte current; SG_ERR_CHECK( SG_string__get_byte_l(pCtx, pstr, offset, ¤t) ); if (current == SG_CR) { SG_byte next; bFoundLineBreak = SG_TRUE; SG_ERR_CHECK( SG_string__get_byte_l(pCtx, pstr, offset+1, &next) ); if (next != SG_LF) { // Mac format, lines end with \r only. Consoles will not advance a line. SG_ERR_CHECK( SG_string__insert__sz(pCtx, pstr, offset+1, "\n") ); } offset++; } else if (current == SG_LF) { bFoundLineBreak = SG_TRUE; } offset++; if (bFoundLineBreak) { SG_ERR_CHECK( SG_string__insert__sz(pCtx, pstr, offset, szLinePrefix) ); offset += lenPrefix; bFoundLineBreak = SG_FALSE; } } SG_ERR_CHECK( SG_string__sizzle(pCtx, &pstr, (SG_byte**)ppszReturn, NULL) ); return; fail: SG_STRING_NULLFREE(pCtx, pstr); }