Exemplo n.º 1
0
tAsyncCall* System_IO_FileInternal_GetFileSystemEntries(PTR pThis_, PTR pParams, PTR pReturnValue) {
	//HEAP_PTR pathHP = ((HEAP_PTR*)pParams)[0];
	HEAP_PTR pathPatternHP = ((HEAP_PTR*)pParams)[1];
	U32 attrs = ((U32*)pParams)[2];
	U32 mask = ((U32*)pParams)[3];
	U32* pError = ((U32**)pParams)[4];
	U32 /*pathLen,*/ pathPatternLen;
	//STRING2 path = SystemString_GetString(pathHP, &pathLen);
	STRING2 pathPattern = SystemString_GetString(pathPatternHP, &pathPatternLen);
	HEAP_PTR retArray;
	U32 tempStoreSize = 32, tempStoreOfs = 0, i;
	HEAP_PTR *pTempStore = malloc(tempStoreSize * sizeof(void*));
	PTR arrayElements;
#ifdef WIN32
	unsigned short pathPatternNullTerm[256];
	HANDLE hFind;
	WIN32_FIND_DATA find;
	memcpy(pathPatternNullTerm, pathPattern, pathPatternLen << 1);
	pathPatternNullTerm[pathPatternLen] = 0;
	hFind = FindFirstFileW(pathPatternNullTerm, &find);
	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			if ((find.dwFileAttributes & mask) == attrs) {
				HEAP_PTR str;
				// Want this file, so store it in tempStore
				if (tempStoreOfs >= tempStoreSize) {
					tempStoreSize <<= 1;
					pTempStore = realloc(pTempStore, tempStoreSize * sizeof(void*));
				}
				str = SystemString_FromCharPtrUTF16(find.cFileName);
				// Need to temporarily make these undeletable, in case a GC happens before they're in the array
				Heap_MakeUndeletable(str);
				pTempStore[tempStoreOfs++] = str;
			}
		} while (FindNextFile(hFind, &find) != 0);
		FindClose(hFind);
	}
#else
	unsigned char path8[256];
	glob_t gl;
	for (i=0; i<pathPatternLen; i++) {
		path8[i] = (U8)pathPattern[i];
	}
	path8[i] = 0;
	i = glob(path8, GLOB_NOSORT, NULL, &gl);
	if (i == 0) {
		for (i=0; i<gl.gl_pathc; i++) {
			unsigned char *pResult = gl.gl_pathv[i];
			U32 fileAttrs = Attrs(pResult, pError);
			if (fileAttrs == (U32)-1) {
				break;
			}
			if ((fileAttrs & mask) == attrs) {
				HEAP_PTR str;
				// Want this file, so store it in tempStore
				if (tempStoreOfs >= tempStoreSize) {
					tempStoreSize <<= 1;
					pTempStore = realloc(pTempStore, tempStoreSize * sizeof(void*));
				}
				str = SystemString_FromCharPtrASCII(pResult);
				// Need to temporarily make these undeletable, in case a GC happens before they're in the array
				Heap_MakeUndeletable(str);
				pTempStore[tempStoreOfs++] = str;
			}
		}
		globfree(&gl);
	} else {
		*pError = errno;
	}
#endif
	// Move the temp-store values into the final returnable array
	retArray = SystemArray_NewVector(types[TYPE_SYSTEM_ARRAY_STRING], tempStoreOfs);
	arrayElements = SystemArray_GetElements(retArray);
	memcpy(arrayElements, pTempStore, tempStoreOfs * sizeof(void*));
	free(pTempStore);
	*(HEAP_PTR*)pReturnValue = retArray;
	// Make the strings deletable again
	for (i=0; i<tempStoreOfs; i++) {
		Heap_MakeDeletable(((HEAP_PTR*)arrayElements)[i]);
	}
	return NULL;
}
static U32 Send_Check(PTR pThis_, PTR pParams, PTR pReturnValue, tAsyncCall *pAsync) {
	PTR buffer;
	int r;
	tSendRecvState *pState = (tSendRecvState*)pAsync->state;

	int s = INTERNALCALL_PARAM(0, int);
	HEAP_PTR bufferArray = INTERNALCALL_PARAM(4, HEAP_PTR);
	U32 ofs = INTERNALCALL_PARAM(8, U32);
	U32 size = INTERNALCALL_PARAM(12, U32);
	U32 flags = INTERNALCALL_PARAM(16, U32);
	U32 *pError = INTERNALCALL_PARAM(20, U32*);

	buffer = SystemArray_GetElements(bufferArray) + ofs + pState->count;

	r = send(s, buffer, size, flags);
	//printf("Send_Check: r=%d\n", r);

	if (r >= 0) {
		pState->count += r;
		if (pState->count >= size) {
			// All data sent
			*(U32*)pReturnValue = pState->count;
			*pError = 0;
			return 1;
		} else {
			// Still more data to come
			return 0;
		}
	} else {
		int err = ERRNO;
printf("Send_Check: errno=%d\n", err);
#ifdef WIN32
		if (err == WSAEINPROGRESS || err == WSAEWOULDBLOCK) {
#else
		if (err == EAGAIN) {
#endif
			return 0;
		} else {
			*(U32*)pReturnValue = pState->count;
			*pError = err;
			return 1;
		}
	}
}

tAsyncCall* System_Net_Sockets_Internal_Send(PTR pThis_, PTR pParams, PTR pReturnValue) {
	U32 ok;
	tAsyncCall *pAsync = TMALLOC(tAsyncCall);
	tSendRecvState *pState = TMALLOC(tSendRecvState);
	pAsync->sleepTime = -1;
	pAsync->checkFn = Receive_Check;
	pAsync->state = (PTR)pState;
	pState->count = 0;
	ok = Send_Check(pThis_, pParams, pReturnValue, pAsync);
	if (ok) {
		free(pState);
		free(pAsync);
		return NULL;
	} else {
		return pAsync;
	}
}