void _dispatch__todo(SG_context * pCtx, _request_headers * pRequestHeaders, SG_repo **ppRepo, const char ** ppUriSubstrings, SG_uint32 uriSubstringsCount, _response_handle ** ppResponseHandle) { SG_ASSERT(pCtx!=NULL); SG_NULLARGCHECK_RETURN(pRequestHeaders); SG_NULLARGCHECK_RETURN(ppUriSubstrings); SG_NULLARGCHECK_RETURN(ppResponseHandle); SG_ASSERT(*ppResponseHandle==NULL); SG_UNUSED(ppRepo); if(uriSubstringsCount==0) { if(eq(pRequestHeaders->pRequestMethod,"GET")) { SG_ERR_CHECK_RETURN( _create_response_handle_for_template(pCtx, pRequestHeaders, SG_HTTP_STATUS_OK, "todo.xhtml", _todo_replacer, ppResponseHandle) ); } else SG_ERR_THROW_RETURN(SG_ERR_URI_HTTP_405_METHOD_NOT_ALLOWED); } else SG_ERR_THROW_RETURN(SG_ERR_URI_HTTP_404_NOT_FOUND); }
void sg_sync_client__http__push_clone__abort( SG_context* pCtx, SG_sync_client * pSyncClient, SG_sync_client_push_handle** ppPush) { SG_ERR_THROW_RETURN( SG_ERR_NOTIMPLEMENTED ); }
void SG_curl__perform(SG_context* pCtx, SG_curl* pCurl) { CURLcode rc = CURLE_OK; _sg_curl* pMe = (_sg_curl*)pCurl; SG_NULLARGCHECK_RETURN(pCurl); rc = curl_easy_perform(pMe->pCurl); // Check for errors in the request and response callbacks. SG_ERR_CHECK_RETURN_CURRENT; // No callback errors. Make sure curl result code is also kosher. if (rc) { switch (rc) { case CURLE_SEND_ERROR: case CURLE_RECV_ERROR: SG_ERR_THROW2_RETURN(SG_ERR_LIBCURL(rc), (pCtx, "%s", "please try again.")); break; #if defined(WINDOWS) case CURLE_SSL_CACERT: SG_ERR_THROW2_RETURN(SG_ERR_LIBCURL(rc), (pCtx, "Verify that the server/files setting is correct, or use the command 'vv config set network/verify_ssl_certs false' to disable this warning.")); break; #endif default: SG_ERR_THROW_RETURN(SG_ERR_LIBCURL(rc)); } } }
static void _sg_dbrecord__validate_vhash(SG_context* pCtx, const SG_dbrecord* prec) { SG_uint32 count; SG_uint32 i; SG_ERR_CHECK( SG_vhash__count(pCtx, prec->pvh, &count) ); for (i=0; i<count; i++) { const char* pszKey = NULL; const SG_variant* pv = NULL; SG_ERR_CHECK( SG_vhash__get_nth_pair(pCtx, prec->pvh, i, &pszKey, &pv) ); if (!pszKey || !pv || pv->type != SG_VARIANT_TYPE_SZ) { SG_ERR_THROW_RETURN(SG_ERR_DBRECORD_VALIDATION_FAILED); } } return; fail: return; }
void SG_dagfrag__eat_other_frag(SG_context * pCtx, SG_dagfrag* pConsumerFrag, SG_dagfrag** ppFragToBeEaten) { SG_rbtree_iterator* pit = NULL; SG_bool b = SG_FALSE; _my_data * pMyData = NULL; const char* psz_id = NULL; SG_dagfrag* pFragToBeEaten = NULL; SG_NULLARGCHECK_RETURN(pConsumerFrag); SG_NULLARGCHECK_RETURN(ppFragToBeEaten); SG_NULLARGCHECK_RETURN(*ppFragToBeEaten); pFragToBeEaten = *ppFragToBeEaten; #if DEBUG && TRACE_DAGFRAG SG_ERR_CHECK( SG_dagfrag_debug__dump__console(pCtx, pFragToBeEaten, "frag to be eaten", 0, SG_CS_STDOUT) ); SG_ERR_CHECK( SG_rbtree_debug__dump_keys_to_console(pCtx, pFragToBeEaten->m_pRB_Cache) ); SG_ERR_CHECK( SG_console__flush(pCtx, SG_CS_STDERR) ); SG_ERR_CHECK( SG_dagfrag_debug__dump__console(pCtx, pConsumerFrag, "new frag before meal", 0, SG_CS_STDOUT) ); SG_ERR_CHECK( SG_rbtree_debug__dump_keys_to_console(pCtx, pConsumerFrag->m_pRB_Cache) ); SG_ERR_CHECK( SG_console__flush(pCtx, SG_CS_STDERR) ); #endif if ( (pConsumerFrag->m_iDagNum != pFragToBeEaten->m_iDagNum) || (0 != strcmp(pConsumerFrag->m_sz_repo_id, pFragToBeEaten->m_sz_repo_id)) || (0 != strcmp(pConsumerFrag->m_sz_admin_id, pFragToBeEaten->m_sz_admin_id)) ) { SG_ERR_THROW_RETURN( SG_ERR_REPO_MISMATCH ); } SG_ERR_CHECK( SG_rbtree__iterator__first(pCtx, &pit,pFragToBeEaten->m_pRB_Cache,&b,&psz_id,(void **)&pMyData) ); while (b) { if (pMyData->m_pDagnode) { SG_ERR_CHECK( SG_dagfrag__add_dagnode(pCtx, pConsumerFrag, &pMyData->m_pDagnode) ); } SG_ERR_CHECK( SG_rbtree__iterator__next(pCtx, pit,&b,&psz_id,(void **)&pMyData) ); } SG_RBTREE_ITERATOR_NULLFREE(pCtx, pit); SG_DAGFRAG_NULLFREE(pCtx, pFragToBeEaten); *ppFragToBeEaten = NULL; #if DEBUG && TRACE_DAGFRAG SG_ERR_CHECK( SG_dagfrag_debug__dump__console(pCtx, pConsumerFrag, "new frag after meal", 0, SG_CS_STDOUT) ); SG_ERR_CHECK( SG_rbtree_debug__dump_keys_to_console(pCtx, pConsumerFrag->m_pRB_Cache) ); SG_ERR_CHECK( SG_console__flush(pCtx, SG_CS_STDERR) ); #endif return; fail: SG_RBTREE_ITERATOR_NULLFREE(pCtx, pit); }
void SG_curl__set__read_string(SG_context* pCtx, SG_curl* pCurl, SG_string* pString) { SG_UNUSED(pCtx); SG_UNUSED(pCurl); SG_UNUSED(pString); SG_ERR_THROW_RETURN(SG_ERR_NOTIMPLEMENTED); }
void SG_dbrecord__add_pair(SG_context* pCtx, SG_dbrecord* prec, const char* putf8Name, const char* putf8Value) { if (prec->pid) { SG_ERR_THROW_RETURN(SG_ERR_INVALID_WHILE_FROZEN); } SG_ERR_CHECK_RETURN( SG_vhash__add__string__sz(pCtx, prec->pvh, putf8Name, putf8Value) ); }
void SG_curl__global_init(SG_context* pCtx) { CURLcode rc; rc = curl_global_init(CURL_GLOBAL_ALL); if (rc) SG_ERR_THROW_RETURN(SG_ERR_LIBCURL(rc)); }
void sg_sync_client__http__push_clone__begin( SG_context* pCtx, SG_sync_client * pSyncClient, const SG_vhash* pvhExistingRepoInfo, SG_sync_client_push_handle** ppPush ) { SG_ERR_THROW_RETURN( SG_ERR_NOTIMPLEMENTED ); }
void sg_lib__fetch_utf8_global_data(SG_context * pCtx, sg_lib_utf8__global_data ** ppUtf8GlobalData) { SG_NULLARGCHECK_RETURN(ppUtf8GlobalData); if (!gbLibDataInitialized || !gLibData.pUtf8GlobalData) SG_ERR_THROW_RETURN(SG_ERR_UNINITIALIZED); *ppUtf8GlobalData = gLibData.pUtf8GlobalData; }
void SG_context__msg__add_listener(SG_context* pCtx, SG_context__msg_callback* cb) { SG_NULLARGCHECK_RETURN(pCtx); SG_NULLARGCHECK_RETURN(cb); if (pCtx->numMsgListeners >= SG_CONTEXT_MAX_MSG_LISTENERS) SG_ERR_THROW_RETURN(SG_ERR_LIMIT_EXCEEDED); pCtx->msg_callbacks[pCtx->numMsgListeners++] = cb; }
void sg_client__c__list_repo_instances(SG_context* pCtx, SG_client * pClient, SG_vhash** ppResult) { SG_NULLARGCHECK_RETURN(pClient); SG_NULLARGCHECK_RETURN(ppResult); /* TODO call SG_server__list_repo_instances */ SG_ERR_THROW_RETURN(SG_ERR_NOTIMPLEMENTED); }
void SG_password__get( SG_context *pCtx, const char *szRepoSpec, const char *szUsername, SG_string **ppstrPassword) { SG_UNUSED(szRepoSpec); SG_UNUSED(szUsername); SG_UNUSED(ppstrPassword); SG_ERR_THROW_RETURN(SG_ERR_NOTIMPLEMENTED); }
void sg_sync_client__http__push_clone__upload_and_commit( SG_context* pCtx, SG_sync_client* pSyncClient, SG_sync_client_push_handle** ppPush, SG_bool bProgressIfPossible, SG_pathname** ppPathCloneFragball, SG_vhash** ppvhResult ) { SG_ERR_THROW_RETURN( SG_ERR_NOTIMPLEMENTED ); }
/** * Create a label for one side of a UNIFIED DIFF. This will appear on one of * the header lines after the "+++ " or "--- ". (We DO NOT create the "+++ " * portion.) * * Gnu-diff (when --label is not used) creates something like: * +++ %s\t%s with the file's relative pathname and date-time-modified. * * --- d1/date2 1970-01-01 00:00:00.000000000 +0000 * +++ d2/date2 2009-04-30 13:10:57.000000000 +0000 * * BZR's diff command creates something like: * * --- d2/date2 2009-04-30 12:38:41 +0000 * +++ d2/date2 2009-04-30 13:10:57 +0000 * * GIT's diff command creates something like: * * --- a/eeeeee.txt * +++ b/eeeeee.txt * * So, we can pretty much do whatever we want here. * * I'm going to try the following and see if we like it: * [] for a historical version, print: * +++ %s\t%s with the repo-path and the HID. * * we don't have a valid date-stamp to print. (our caller might have * the date-stamp on the changeset, but that doesn't have anything to * to with the date on an indivdual file (the file might not have even * been modified in that changeset). * * the HID may be useful later. * * [] for a working-directory version, print: * +++ %s\t%s with the repo-path and the live date-time-stamp. * * since the file is in the working-directory, the date stamp has * some validity (it doesn't mean that they changed the file or reflect * the last change (it could be date of the last get-latest for all we * know)). * * [] just for sanity, we allow a repo-path only version: * +++ %s * * In all cases we print the complete repo-path "@/a/b/c/foo.c". * The repo-path is as computed in SG_treediff2 and reflects all pathname * renames/moves. * * TODO do we care about feeding this output to PATCH and how it digests * pathnames and with the various -p0 -p1 ... arguments? * * We return a string that you must free. */ void SG_diff_utils__make_label(SG_context * pCtx, const char * szRepoPath, const char * szHid, const char * szDate, SG_string ** ppStringLabel) { SG_string * pString = NULL; SG_NONEMPTYCHECK_RETURN(szRepoPath); if (szHid && szDate) // allow zero or one, but not both SG_ERR_THROW_RETURN( SG_ERR_INVALIDARG ); SG_NULLARGCHECK(ppStringLabel); SG_ERR_CHECK_RETURN( SG_STRING__ALLOC(pCtx,&pString) ); ////////////////////////////////////////////////////////////////// // WARNING: All of the input arguments are in our internal NFC UTF-8 format // WARNING: (as is everything). SG_exec__() will convert whatever we construct // WARNING: here to an os-buffer (utf-8, locale, or wchar_t) before giving it // WARNING: to execvp() or CreateProcessW(). // WARNING: // WARNING: I *think* this is what we want. // WARNING: // WARNING: We can't be sure how the child process will handle the label text // WARNING: when it prints it to its STDOUT. This may cause some confusion. // WARNING: For example, NFC/NFD issues on MAC or some '?' characters getting // WARNING: printed by cygwin's version of gnu-diff and etc. // WARNING: // WARNING: I don't know if there are actually any issues here or not or if // WARNING: so whether they will cause a problem. ////////////////////////////////////////////////////////////////// if (szHid) SG_ERR_CHECK( SG_string__sprintf(pCtx, pString,"%s\t%s", szRepoPath,szHid) ); else if (szDate) SG_ERR_CHECK( SG_string__sprintf(pCtx, pString,"%s\t%s", szRepoPath,szDate) ); else SG_ERR_CHECK( SG_string__set__sz(pCtx,pString,szRepoPath) ); *ppStringLabel = pString; return; fail: SG_STRING_NULLFREE(pCtx, pString); }
void sg_client__c__push_remove(SG_context* pCtx, SG_client * pClient, SG_client_push_handle* pPush, SG_vhash* pvh_stuff_to_be_removed_from_staging_area, SG_vhash** ppResult) { SG_NULLARGCHECK_RETURN(pClient); SG_NULLARGCHECK_RETURN(pPush); SG_NULLARGCHECK_RETURN(pvh_stuff_to_be_removed_from_staging_area); SG_NULLARGCHECK_RETURN(ppResult); SG_ERR_THROW_RETURN(SG_ERR_NOTIMPLEMENTED); }
void SG_curl__getinfo__int32(SG_context* pCtx, SG_curl* pCurl, CURLINFO info, SG_int32* piVal) { _sg_curl* p = (_sg_curl*)pCurl; CURLcode rc = CURLE_OK; SG_int64 val; SG_NULLARGCHECK_RETURN(pCurl); rc = curl_easy_getinfo(p->pCurl, info, &val); if (rc) SG_ERR_THROW_RETURN(SG_ERR_LIBCURL(rc)); *piVal = (SG_int32)val; }
void SG_group__list_members( SG_context* pCtx, SG_repo* pRepo, const char* psz_group_name, SG_varray** ppva ) { SG_UNUSED(pCtx); SG_UNUSED(pRepo); SG_UNUSED(psz_group_name); SG_UNUSED(ppva); SG_ERR_THROW_RETURN( SG_ERR_NOTIMPLEMENTED ); }
void SG_group__remove_subgroups( SG_context* pCtx, SG_repo* pRepo, const char* psz_group_name, const char** paszMemberNames, SG_uint32 count_names ) { SG_UNUSED(pCtx); SG_UNUSED(pRepo); SG_UNUSED(psz_group_name); SG_UNUSED(paszMemberNames); SG_UNUSED(count_names); SG_ERR_THROW_RETURN( SG_ERR_NOTIMPLEMENTED ); }
static void _lookupMonth(SG_context* pCtx, const char *monthstr, int *month) { int i; SG_NULLARGCHECK_RETURN(monthstr); for ( i = 0; i < 12; ++i ) { if (strcmp(monthstr, months[i]) == 0) { *month = i; return; } } SG_ERR_THROW_RETURN(SG_ERR_ARGUMENT_OUT_OF_RANGE); }
void SG_stringarray__get_nth( SG_context* pCtx, const SG_stringarray* psa, SG_uint32 n, const char** ppsz ) { SG_NULLARGCHECK_RETURN( psa ); SG_NULLARGCHECK_RETURN( ppsz ); if (n >= psa->count) { SG_ERR_THROW_RETURN( SG_ERR_ARGUMENT_OUT_OF_RANGE ); } *ppsz = psa->aStrings[n]; }
void SG_time__get_milliseconds_since_1970_utc(SG_context* pCtx, SG_int64* pResult) { struct timeval tv; SG_int64 result; int rc; rc = gettimeofday(&tv, NULL); if (rc) { *pResult = 0; SG_ERR_THROW_RETURN( SG_ERR_ERRNO(errno) ); } result = (SG_int64) tv.tv_sec; result *= MILLISECONDS_PER_SECOND; result += (tv.tv_usec / 1000); *pResult = result; }
void SG_time__parseRFC850(SG_context* pCtx,const char* pszInputString, SG_int64 * pTime, SG_int64 * pReturnedLocalTime) { int scanOK = 0; char trash[4]; int day = 0; int month = 0; int year = 0; int hour = 0; int minute = 0; int second = 0; char monthstr[4]; char tz[4]; struct tm t; SG_UNUSED(pCtx); SG_UNUSED(pszInputString); #if defined(WINDOWS) scanOK = sscanf_s(pszInputString, "%3s, %d %3s %d %d:%d:%d %3s", trash, sizeof(trash), &day, monthstr, sizeof(monthstr), &year, &hour, &minute, &second, tz, sizeof(tz)); #else scanOK = sscanf(pszInputString, "%3s, %d %3s %d %d:%d:%d %3s", trash, &day, monthstr, &year, &hour, &minute, &second, tz); #endif if ((scanOK != 8) || (strcmp("GMT", tz) != 0)) { SG_ERR_THROW_RETURN(SG_ERR_ARGUMENT_OUT_OF_RANGE); } SG_ERR_CHECK_RETURN( _lookupMonth(pCtx, monthstr, &month) ); t.tm_min = minute; t.tm_hour = hour; t.tm_sec = second; t.tm_year = year - 1900; t.tm_mon = month; t.tm_mday = day; *pTime = (SG_int64)utc_mktime(&t) * 1000; *pReturnedLocalTime = (SG_int64)mktime(&t) * 1000; }
void SG_jscore__new_runtime( SG_context * pCtx, JSContextCallback cb, JSFunctionSpec *shell_functions, SG_bool bSkipModules, JSRuntime **ppRt ) { char * szSsjsMutable = NULL; if(gpJSCoreGlobalState != NULL) SG_ERR_THROW_RETURN(SG_ERR_ALREADY_INITIALIZED); SG_ERR_CHECK( SG_alloc1(pCtx, gpJSCoreGlobalState) ); // Store this for later. gpJSCoreGlobalState->cb = cb; gpJSCoreGlobalState->shell_functions = shell_functions; gpJSCoreGlobalState->bSkipModules = bSkipModules; if (! bSkipModules) SG_ERR_CHECK( _sg_jscore_getpaths(pCtx) ); SG_ERR_CHECK( SG_mutex__init(pCtx, &gpJSCoreGlobalState->mutexJsNamed) ); SG_ERR_CHECK( SG_RBTREE__ALLOC(pCtx, &gpJSCoreGlobalState->prbJSMutexes) ); // Start up SpiderMonkey. JS_SetCStringsAreUTF8(); gpJSCoreGlobalState->rt = JS_NewRuntime(64L * 1024L * 1024L); // TODO decide the right size here if(gpJSCoreGlobalState->rt==NULL) SG_ERR_THROW2(SG_ERR_JS, (pCtx, "Failed to allocate JS Runtime")); if (ppRt) *ppRt = gpJSCoreGlobalState->rt; return; fail: SG_NULLFREE(pCtx, szSsjsMutable); SG_PATHNAME_NULLFREE(pCtx, gpJSCoreGlobalState->pPathToDispatchDotJS); SG_PATHNAME_NULLFREE(pCtx, gpJSCoreGlobalState->pPathToModules); }
void SG_curl__alloc(SG_context* pCtx, SG_curl** ppCurl) { _sg_curl* p = NULL; SG_ERR_CHECK( SG_alloc1(pCtx, p) ); p->pCurl = curl_easy_init(); if (!p->pCurl) SG_ERR_THROW_RETURN(SG_ERR_LIBCURL(CURLE_FAILED_INIT)); SG_ERR_CHECK( _set_curl_options(pCtx, p->pCurl) ); p->pCtx = pCtx; *ppCurl = (SG_curl*)p; return; fail: SG_ERR_IGNORE( SG_curl__free(pCtx, (SG_curl*)p) ); }
/** * Since we now have __T_ file-type bits in a SG_wc_status_flags * which are strictly based on the file-type and nothing else, we * can isolate this code so that SG_vv2__status_() can use it. * */ void SG_wc__status__tne_type_to_flags(SG_context * pCtx, SG_treenode_entry_type tneType, SG_wc_status_flags * pStatusFlags) { SG_wc_status_flags statusFlags = SG_WC_STATUS_FLAGS__ZERO; SG_NULLARGCHECK_RETURN( pStatusFlags ); switch (tneType) { case SG_TREENODEENTRY_TYPE_REGULAR_FILE: statusFlags = SG_WC_STATUS_FLAGS__T__FILE; break; case SG_TREENODEENTRY_TYPE_DIRECTORY: statusFlags = SG_WC_STATUS_FLAGS__T__DIRECTORY; break; case SG_TREENODEENTRY_TYPE_SYMLINK: statusFlags = SG_WC_STATUS_FLAGS__T__SYMLINK; break; case SG_TREENODEENTRY_TYPE_SUBMODULE: SG_ERR_THROW_RETURN( SG_ERR_NOTIMPLEMENTED ); case SG_TREENODEENTRY_TYPE__DEVICE: statusFlags = SG_WC_STATUS_FLAGS__T__DEVICE; break; default: statusFlags = SG_WC_STATUS_FLAGS__T__BOGUS; break; } *pStatusFlags = statusFlags; }
void SG_getopt__long(SG_context* pCtx, SG_getopt* os, const SG_getopt_option* opts, SG_uint32* optch, const char** optarg, SG_bool* bNoMoreOptions) { const char *p; SG_int32 i; /* Let the calling program reset option processing. */ if (os->reset) { os->place = ""; os->ind = 1; os->reset = 0; } /* * We can be in one of two states: in the middle of processing a * run of short options, or about to process a new argument. * Since the second case can lead to the first one, handle that * one first. */ p = os->place; if (*p == '\0') { /* If we are interleaving, skip non-option arguments. */ if (os->interleave) { while (os->ind < os->count_args && *os->paszArgs[os->ind] != '-') os->ind++; os->skip_end = os->ind; } if (os->ind >= os->count_args || *os->paszArgs[os->ind] != '-') { os->ind = os->skip_start; //return APR_EOF; *bNoMoreOptions = SG_TRUE; SG_ERR_THROW_RETURN( SG_ERR_GETOPT_NO_MORE_OPTIONS ); } p = os->paszArgs[os->ind++] + 1; if (*p == '-' && p[1] != '\0') { /* Long option */ /* Search for the long option name in the caller's table. */ SG_uint32 len = 0; p++; for (i = 0; ; i++) { if (opts[i].optch == 0) /* No match */ SG_ERR_THROW2_RETURN( SG_ERR_GETOPT_BAD_ARG, (pCtx, "Invalid option: %s", p - 2) ); //return serr(os, "invalid option", p - 2, APR_BADCH); if (opts[i].pStringName) { len = strlen(opts[i].pStringName); if (strncmp(p, opts[i].pStringName, len) == 0 && (p[len] == '\0' || p[len] == '=')) break; } } *optch = opts[i].optch; if (opts[i].has_arg) { if (p[len] == '=') /* Argument inline */ *optarg = p + len + 1; else { if (os->ind >= os->count_args) /* Argument missing */ SG_ERR_THROW2_RETURN( SG_ERR_GETOPT_BAD_ARG, (pCtx, "Missing argument: %s", p - 2) ); //return serr(os, "missing argument", p - 2, APR_BADARG); else /* Argument in next arg */ *optarg = os->paszArgs[os->ind++]; } } else { *optarg = NULL; if (p[len] == '=') SG_ERR_THROW2_RETURN( SG_ERR_GETOPT_BAD_ARG, (pCtx, "Erroneous argument: %s", p - 2) ); //return serr(os, "erroneous argument", p - 2, APR_BADARG); } _sg_permute(os); //return APR_SUCCESS; *bNoMoreOptions = SG_FALSE; return; } else { if (*p == '-') { /* Bare "--"; we're done */ _sg_permute(os); os->ind = os->skip_start; //return APR_EOF; *bNoMoreOptions = SG_TRUE; SG_ERR_THROW_RETURN(SG_ERR_GETOPT_NO_MORE_OPTIONS); } else if (*p == '\0') /* Bare "-" is illegal */ SG_ERR_THROW2_RETURN( SG_ERR_GETOPT_BAD_ARG, (pCtx, "Invalid option: %s", p) ); //return serr(os, "invalid option", p, APR_BADCH); } } /* * Now we're in a run of short options, and *p is the next one. * Look for it in the caller's table. */ for (i = 0; ; i++) { if (opts[i].optch == 0) /* No match */ SG_ERR_THROW2_RETURN( SG_ERR_GETOPT_BAD_ARG, (pCtx, "Invalid option character: %c", *p) ); //return cerr(os, "invalid option character", *p, APR_BADCH); if (*p == opts[i].optch) break; } *optch = *p++; if (opts[i].has_arg) { if (*p != '\0') /* Argument inline */ *optarg = p; else { if (os->ind >= os->count_args) /* Argument missing */ SG_ERR_THROW2_RETURN( SG_ERR_GETOPT_BAD_ARG, (pCtx, "Missing argument: %c", *optch) ); //return cerr(os, "missing argument", *optch, APR_BADARG); else /* Argument in next arg */ *optarg = os->paszArgs[os->ind++]; } os->place = ""; } else { *optarg = NULL; os->place = p; } _sg_permute(os); *bNoMoreOptions = SG_FALSE; return; }
void SG_vfile__begin( SG_context* pCtx, const SG_pathname* pPath, /**< The path of the file containing the JSON text */ SG_file_flags mode, SG_vhash** ppvh, /**< If there are no errors, the resulting vhash table will be returned here. */ SG_vfile** ppvf ) { SG_vfile* pvf = NULL; SG_vhash* pvh = NULL; SG_uint32 len32; SG_fsobj_type t; SG_byte* p = NULL; SG_bool bExists; SG_fsobj_type FsObjType; SG_fsobj_perms FsObjPerms; SG_ERR_CHECK( SG_fsobj__exists__pathname(pCtx, pPath, &bExists, &FsObjType, &FsObjPerms) ); if ( bExists && (SG_FSOBJ_TYPE__REGULAR != FsObjType) ) { SG_ERR_THROW_RETURN(SG_ERR_NOTAFILE); } if (bExists) { SG_uint64 len64; SG_ERR_CHECK( SG_fsobj__length__pathname(pCtx, pPath, &len64, &t) ); // TODO "len" is uint64 because we can have huge files, but // TODO our buffer is limited to uint32 (on 32bit systems). // TODO verify that len will fit in uint32. len32 = (SG_uint32)len64; } else { len32 = 0; } SG_ERR_CHECK_RETURN( SG_alloc1(pCtx, pvf) ); SG_ERR_CHECK( SG_file__open__pathname(pCtx, pPath, mode | SG_FILE_LOCK, SG_FSOBJ_PERMS__UNUSED, &pvf->pFile) ); pvf->mode = mode; #if TRACE_VFILE SG_ERR_IGNORE( SG_console(pCtx, SG_CS_STDERR, "VFileBegin: Reading %d bytes from %s\n", len32, SG_pathname__sz(pPath)) ); #endif if (len32 > 0) { SG_ERR_CHECK( SG_alloc(pCtx, 1,len32+1,&p) ); SG_ERR_CHECK( SG_file__read(pCtx, pvf->pFile, len32, p, NULL) ); p[len32] = 0; SG_ERR_CHECK( SG_VHASH__ALLOC__FROM_JSON(pCtx, &pvh, (const char*) p) ); SG_NULLFREE(pCtx, p); p = NULL; } else { pvh = NULL; } *ppvf = pvf; *ppvh = pvh; return; fail: SG_FILE_NULLCLOSE(pCtx, pvf->pFile); SG_NULLFREE(pCtx, p); SG_NULLFREE(pCtx, pvf); }
void SG_unzip__currentfile__read(SG_context* pCtx, SG_unzip* s, SG_byte* pBuf, SG_uint32 iLenBuf, SG_uint32* piBytesRead) { int zerr=Z_OK; SG_uint32 iRead = 0; file_in_zip_read_info_s* pfile_in_zip_read_info; SG_NULLARGCHECK_RETURN( s ); pfile_in_zip_read_info = s->pfile_in_zip_read; SG_NULLARGCHECK_RETURN( pfile_in_zip_read_info ); if (!pfile_in_zip_read_info->read_buffer) { SG_ERR_THROW_RETURN( SG_ERR_UNSPECIFIED ); } if (!iLenBuf) { return; } pfile_in_zip_read_info->stream.next_out = pBuf; pfile_in_zip_read_info->stream.avail_out = iLenBuf; if (iLenBuf > pfile_in_zip_read_info->rest_read_uncompressed) { pfile_in_zip_read_info->stream.avail_out = (SG_uint32)pfile_in_zip_read_info->rest_read_uncompressed; } while (pfile_in_zip_read_info->stream.avail_out>0) { if ((pfile_in_zip_read_info->stream.avail_in==0) && (pfile_in_zip_read_info->rest_read_compressed>0)) { SG_uint32 uReadThis = UNZ_BUFSIZE; if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) { uReadThis = (SG_uint32)pfile_in_zip_read_info->rest_read_compressed; } if (uReadThis == 0) { //TODO - maybe we should change this SG_ERR_THROW( SG_ERR_EOF ); } SG_ERR_CHECK( SG_file__seek(pCtx, s->pFile, pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile) ); SG_ERR_CHECK( SG_file__read(pCtx, s->pFile, uReadThis, (SG_byte*) pfile_in_zip_read_info->read_buffer, NULL) ); pfile_in_zip_read_info->pos_in_zipfile += uReadThis; pfile_in_zip_read_info->rest_read_compressed-=uReadThis; pfile_in_zip_read_info->stream.next_in = (Bytef*)pfile_in_zip_read_info->read_buffer; pfile_in_zip_read_info->stream.avail_in = (SG_uint32)uReadThis; } if (pfile_in_zip_read_info->compression_method==0) { SG_uint32 uDoCopy,i ; if ((pfile_in_zip_read_info->stream.avail_in == 0) && (pfile_in_zip_read_info->rest_read_compressed == 0)) { if (iRead == 0) { SG_ERR_THROW( SG_ERR_EOF ); } goto done; } if (pfile_in_zip_read_info->stream.avail_out < pfile_in_zip_read_info->stream.avail_in) { uDoCopy = pfile_in_zip_read_info->stream.avail_out ; } else { uDoCopy = pfile_in_zip_read_info->stream.avail_in ; } for (i=0;i<uDoCopy;i++) { *(pfile_in_zip_read_info->stream.next_out+i) = *(pfile_in_zip_read_info->stream.next_in+i); } pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, pfile_in_zip_read_info->stream.next_out, uDoCopy); pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; pfile_in_zip_read_info->stream.avail_in -= uDoCopy; pfile_in_zip_read_info->stream.avail_out -= uDoCopy; pfile_in_zip_read_info->stream.next_out += uDoCopy; pfile_in_zip_read_info->stream.next_in += uDoCopy; pfile_in_zip_read_info->stream.total_out += uDoCopy; iRead += uDoCopy; } else { SG_uint32 uTotalOutBefore,uTotalOutAfter; const Bytef *bufBefore; SG_uint32 uOutThis; int flush=Z_SYNC_FLUSH; uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; bufBefore = pfile_in_zip_read_info->stream.next_out; /* if ((pfile_in_zip_read_info->rest_read_uncompressed == pfile_in_zip_read_info->stream.avail_out) && (pfile_in_zip_read_info->rest_read_compressed == 0)) flush = Z_FINISH; */ zerr = inflate(&pfile_in_zip_read_info->stream,flush); if ((zerr>=0) && (pfile_in_zip_read_info->stream.msg)) { SG_ERR_THROW( SG_ERR_ZLIB(zerr) ); } uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; uOutThis = uTotalOutAfter-uTotalOutBefore; pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,bufBefore, (SG_uint32)(uOutThis)); pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis; iRead += (SG_uint32)(uTotalOutAfter - uTotalOutBefore); if (zerr == Z_STREAM_END) { // return (iRead==0) ? UNZ_EOF : iRead; // break; } } } done: *piBytesRead = iRead; fail: return; }
static void sg_read_entire_file( SG_context* pCtx, const SG_pathname* pPath, char** ppbuf, SG_uint32* plen ) { SG_uint32 len32; SG_fsobj_type t; SG_byte* p = NULL; SG_bool bExists; SG_fsobj_type FsObjType; SG_fsobj_perms FsObjPerms; SG_file* pFile = NULL; SG_ERR_CHECK( SG_fsobj__exists__pathname(pCtx, pPath, &bExists, &FsObjType, &FsObjPerms) ); if ( bExists && (SG_FSOBJ_TYPE__REGULAR != FsObjType) ) { SG_ERR_IGNORE( SG_log__report_error(pCtx, "Unable to open file: %s.", SG_pathname__sz(pPath)) ); SG_ERR_THROW_RETURN(SG_ERR_NOTAFILE); } if (bExists) { SG_uint64 len64; SG_ERR_CHECK( SG_fsobj__length__pathname(pCtx, pPath, &len64, &t) ); // TODO "len" is uint64 because we can have huge files, but // TODO our buffer is limited to uint32 (on 32bit systems). // TODO verify that len will fit in uint32. len32 = (SG_uint32)len64; SG_ERR_CHECK( SG_file__open__pathname(pCtx, pPath, SG_FILE_RDONLY | SG_FILE_OPEN_EXISTING, SG_FSOBJ_PERMS__UNUSED, &pFile) ); } else { SG_ERR_THROW_RETURN(SG_ERR_NOTAFILE); //len32 = 0; } if (len32 > 0) { SG_ERR_CHECK( SG_alloc(pCtx, 1,len32+1,&p) ); SG_ERR_CHECK( SG_file__read(pCtx, pFile, len32, p, NULL) ); p[len32] = 0; *ppbuf = (char*) p; p = NULL; *plen = len32; } else { *ppbuf = NULL; *plen = 0; } fail: SG_FILE_NULLCLOSE(pCtx, pFile); SG_NULLFREE(pCtx, p); }