//## FILE FILE.new(String path, String mode); static KMETHOD File_new(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = I18NAPI formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace); const char *mode = kString_text(sfp[2].asString); FILE *fp = fopen(systemPath, mode); kFile *file = (kFile *) sfp[0].asObject; if(fp == NULL) { int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "fopen", LogText("filename", kString_text(path)), LogText("mode", mode), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), fault, NULL, sfp); } if(mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') { KTraceChangeSystemPoint(trace, "fopen", LogFileName(kString_text(path)), LogText("mode", mode)); } file->fp = fp; KFieldInit(file, file->PathInfoNULL, path); if(!I18NAPI isSystemCharsetUTF8(kctx)) { if(mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') { file->writerIconv = I18NAPI iconvUTF8ToSystemCharset(kctx, trace); } else { file->readerIconv = I18NAPI iconvSystemCharsetToUTF8(kctx, trace); } } KReturn(file); }
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); }
//## boolean chdir(String path) static KMETHOD System_chdir(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 = chdir(systemPath); if(ret == -1) { KTraceErrorPoint(trace, SystemFault, "chdir", LogFileName(S_text(path)), LogErrno); } KReturnUnboxValue(ret != -1); }
//## boolean System.isDir(String path) static KMETHOD System_isDir(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; if(stat(systemPath, &buf) == 0) { KReturnUnboxValue(S_ISDIR(buf.st_mode)); } KReturnUnboxValue(false); }
static KMETHOD System_lchown(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); uid_t owner = (uid_t)sfp[2].intValue; gid_t group = (gid_t)sfp[3].intValue; int ret = lchown(systemPath, owner, group); if(ret == -1) { KTraceErrorPoint(trace, SystemFault, "lchown", LogFileName(S_text(path)), LogOwner(owner), LogGroup(group), 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)); }
//## String System.realpath(String path) static KMETHOD System_realpath(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX], filepath[K_PATHMAX] = {0}; kString *path = sfp[1].asString; const char *systemPath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace); char *cwd = realpath(systemPath, filepath); if(cwd != NULL) { const char *konohaPath = PLATAPI formatKonohaPath(kctx, buffer, sizeof(buffer), cwd, strlen(cwd), trace); KReturn(KLIB new_kString(kctx, OnStack, konohaPath, strlen(konohaPath), 0)); } else { KTraceErrorPoint(trace, SystemFault, "realpath", LogFileName(S_text(path)), LogErrno); KReturn(KNULL(String)); } }
//## boolean System.mkdir(String path, int mode) static KMETHOD System_mkdir(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); mode_t mode = (mode_t)sfp[2].intValue; int ret = mkdir(systemPath, mode); if(ret == -1) { KTraceErrorPoint(trace, SystemFault, "mkdir", LogFileName(S_text(path)), LogMode(mode), LogErrno); } else { KTraceChangeSystemPoint(trace, "mkdir", LogFileName(S_text(path)), LogMode(mode)); } KReturnUnboxValue(ret != -1); }
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); }
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); }