Ejemplo n.º 1
0
//## 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);
}
Ejemplo n.º 2
0
static int TRACE_fputc(KonohaContext *kctx, kFile *file, int ch, KTraceInfo *trace)
{
	if(fputc(ch, file->fp) == EOF) {
		KTraceErrorPoint(trace, SystemFault, "fputc", LogFile(file), LogErrno);
		KLIB KRuntime_raise(kctx, KException_("IO"), SystemFault, NULL, trace->baseStack);
	}
	else if(!kFile_is(ChangeLessStream, file)) {
		KTraceChangeSystemPoint(trace, "fputc", LogFile(file), LogWrittenByte(1));
	}
	return ch;
}
Ejemplo n.º 3
0
static size_t TRACE_fwrite(KonohaContext *kctx, kFile *file, const char *buf, size_t bufsiz, KTraceInfo *trace)
{
	size_t size = fwrite(buf, 1, bufsiz, file->fp);
	if(ferror(file->fp) != 0){
		KTraceErrorPoint(trace, SystemFault, "fwrite", LogFile(file), LogErrno);
		KLIB KRuntime_raise(kctx, KException_("IO"), SystemFault, NULL, trace->baseStack);
	}
	if(size > 0 && !kFile_is(ChangeLessStream, file)) {
		KTraceChangeSystemPoint(trace, "fwrite", LogFile(file), LogWrittenByte(size));
	}
	return size;
}
Ejemplo n.º 4
0
//## boolean System.rmdir(String path)
static KMETHOD System_rmdir(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	int ret = rmdir(systemPath);
	if(ret == -1) {
		int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace);
		KTraceErrorPoint(trace, fault, "rmdir", LogFileName(kString_text(path)), LogErrno);
	}
	else {
		KTraceChangeSystemPoint(trace, "rmdir", LogFileName(kString_text(path)));
	}
	KReturnUnboxValue(ret != -1);
}
Ejemplo n.º 5
0
//## 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 I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	mode_t mode = (mode_t)sfp[2].intValue;
	int ret = mkdir(systemPath, mode);
	if(ret == -1) {
		KTraceErrorPoint(trace, SystemFault, "mkdir", LogFileName(kString_text(path)), LogMode(mode), LogErrno);
	}
	else {
		KTraceChangeSystemPoint(trace, "mkdir", LogFileName(kString_text(path)), LogMode(mode));
	}
	KReturnUnboxValue(ret != -1);
}
Ejemplo n.º 6
0
static KMETHOD System_symlink(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 I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	const char *newpath = PLATAPI I18NModule.formatSystemPath(kctx, buffer2, sizeof(buffer2), kString_text(path2), kString_size(path2), trace);
	int ret = symlink(oldpath, newpath);
	if(ret == -1) {
		int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|kString_GuessUserFault(path2)|SystemError, trace);
		KTraceErrorPoint(trace, fault, "symlink", LogFileName(kString_text(path)), LogFileName2(kString_text(path2)), LogErrno);
	}
	else {
		KTraceChangeSystemPoint(trace, "symlink", LogFileName(kString_text(path)), LogFileName2(kString_text(path2)), LogErrno);
	}
	KReturnUnboxValue(ret != -1);
}
Ejemplo n.º 7
0
//## boolean System.truncate(String path, int length)
static KMETHOD System_truncate(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);
	off_t length = (off_t)sfp[2].intValue;
	int ret = truncate(systemPath, length);
	if(ret == -1) {
		int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(path)|SystemError, trace);
		KTraceErrorPoint(trace, fault, "truncate", LogFileName(S_text(path)), LogUint("length", length), LogErrno);
	}
	else {
		KTraceChangeSystemPoint(trace, "truncate", LogFileName(S_text(path)), LogUint("length", length));
	}
	KReturnUnboxValue(ret != -1);
}