//## String Curl.receiveString(); static KMETHOD Curl_receiveString(KonohaContext *kctx, KonohaStack *sfp) { kCurl* kcurl = (kCurl *)sfp[0].asObject; /* presets */ struct ReceiveBuffer rbuf = {0}; rbuf.kctx = kctx; KLIB KBuffer_Init(&(kctx->stack->cwb), &rbuf.wb); curl_easy_setopt(kcurl->curl, CURLOPT_WRITEFUNCTION, writeToBuffer); curl_easy_setopt(kcurl->curl, CURLOPT_WRITEDATA, &rbuf); /* perform */ KMakeTrace(trace, sfp); CURLcode res; if(kcurl->headers != NULL) { curl_easy_setopt(kcurl->curl, CURLOPT_HTTPHEADER, kcurl->headers); } KTraceResponseCheckPoint(trace, 0, "curl_easy_perform", res = curl_easy_perform(kcurl->curl) ); if(res != CURLE_OK) { int fault = diagnosisCurlFaultType(kctx, res, (kcurl->URLInfoNULL == NULL) ? 0 : kString_guessUserFault(kcurl->URLInfoNULL)); KTraceErrorPoint(trace, fault, "curl_easy_perform", LogURL(kcurl), LogCurlStrError(res)); } KReturnWith( KLIB new_kString(rbuf.kctx, OnStack, KLIB KBuffer_Stringfy(rbuf.kctx, &rbuf.wb, 0), KBuffer_bytesize(&rbuf.wb), 0), KLIB KBuffer_Free(&rbuf.wb) ); }
static KMETHOD System_rename(KonohaContext *kctx, KonohaStack *sfp) { char buffer[K_PATHMAX], buffer2[K_PATHMAX]; kString *path = sfp[1].asString, *path2 = sfp[2].asString; KMakeTrace(trace, sfp); const char *oldpath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace); const char *newpath = PLATAPI formatSystemPath(kctx, buffer2, sizeof(buffer2), S_text(path2), S_size(path2), trace); int ret = rename(oldpath, newpath); if(ret == -1) { int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(path)|kString_guessUserFault(path2)|SystemError, trace); KTraceErrorPoint(trace, fault, "rename", LogFileName(S_text(path)), LogFileName2(S_text(path2)), LogErrno); } else { KTraceChangeSystemPoint(trace, "rename", LogFileName(S_text(path)), LogFileName2(S_text(path2)), LogErrno); } KReturnUnboxValue(ret != -1); }
//## Stat System.lstat(String path) static KMETHOD System_lstat(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace); struct stat buf = {}; /* zero */ int ret = lstat(systemPath, &buf); if(ret == -1) { int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "lstat", LogText("path", S_text(path)), LogErrno); } KReturn(KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)&buf)); }
static KMETHOD System_unlink(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace); int ret = unlink(systemPath); if(ret == -1) { int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "unlink", LogFileName(S_text(path)), LogErrno); } else { KTraceChangeSystemPoint(trace, "unlink", LogFileName(S_text(path))); } KReturnUnboxValue(ret != -1); }
//## boolean Curl.perform(); static KMETHOD Curl_perform(KonohaContext *kctx, KonohaStack *sfp) { kCurl* kcurl = (kCurl *)sfp[0].asObject; KMakeTrace(trace, sfp); CURLcode res; if(kcurl->headers != NULL) { curl_easy_setopt(kcurl->curl, CURLOPT_HTTPHEADER, kcurl->headers); } KTraceResponseCheckPoint(trace, 0, "curl_easy_perform", res = curl_easy_perform(kcurl->curl) ); if(res != CURLE_OK){ int fault = diagnosisCurlFaultType(kctx, res, (kcurl->URLInfoNULL == NULL) ? 0 : kString_guessUserFault(kcurl->URLInfoNULL)); KTraceErrorPoint(trace, fault, "curl_easy_perform", LogURL(kcurl), LogCurlStrError(res)); } KReturnUnboxValue((res == CURLE_OK)); }
//## int System.open(String pathname, int flags) static KMETHOD System_open(KonohaContext *kctx, KonohaStack *sfp) { kString *s = sfp[1].asString; const char *pathname = S_text(s); int flags = sfp[2].intValue; int ret = open(pathname, flags); if(ret == -1) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(s)|SystemError, trace); KTraceErrorPoint(trace, fault, "open", LogText("pathname", pathname), LogUint("flags", flags) ); } KReturnUnboxValue(ret); }
static KMETHOD System_readlink(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace); char pathbuf[K_PATHMAX]; ssize_t ret = readlink(systemPath, pathbuf, K_PATHMAX); if(ret == -1) { int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "readlink", LogFileName(S_text(path)), LogErrno); KReturn(KNULL(String)); } else { const char *konohaPath = PLATAPI formatKonohaPath(kctx, buffer, sizeof(buffer), pathbuf, strlen(pathbuf), trace); KReturn(KLIB new_kString(kctx, OnStack, konohaPath, strlen(konohaPath), 0)); } }
//## DIR System.opendir(String path) static KMETHOD System_opendir(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace); DIR *d = opendir(systemPath); if(d == NULL) { int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "opendir", LogText("dirname", S_text(path)), LogErrno); KLIB KonohaRuntime_raise(kctx, EXPT_("IO"), fault, NULL, sfp); } kDir *dir = (kDir *)KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)d); KFieldSet(dir, dir->PathInfoNULL, path); if(!PLATAPI isSystemCharsetUTF8(kctx)) { dir->readerIconv = PLATAPI iconvSystemCharsetToUTF8(kctx, trace); } KReturn(dir); }