Author: mturk
Date: Sat Jul 9 06:13:46 2011
New Revision: 1144601
URL: http://svn.apache.org/viewvc?rev=1144601&view=rev
Log:
Add common read file data function
Modified:
commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c
commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h?rev=1144601&r1=1144600&r2=1144601&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h Sat Jul
9 06:13:46 2011
@@ -1608,8 +1608,10 @@ ACR_INLINE(DWORD) AcrNetOsError()
#if defined(DEBUG) || defined(_DEBUG)
#define ACR_THROW(CL, ER) AcrDebugThrowException(env,
__FILE_FUNC_LINE__, (CL), (ER))
+#define ACR_THROW_IFENV(CL, ER) if (env != 0) AcrDebugThrowException(env,
__FILE_FUNC_LINE__, (CL), (ER)); else (void)0
#else
#define ACR_THROW(CL, ER) AcrThrowException(env, (CL), (ER))
+#define ACR_THROW_IFENV(CL, ER) if (env != 0) AcrThrowException(env, (CL),
(ER)); else (void)0
#endif
#define ACR_THROW_MSG(CL, MS) AcrThrow(env, (CL), MS)
#define ACR_THROW_SYS_ERRNO() AcrThrowByError(env, ACR_EX_ESYS,
ACR_GET_OS_ERROR(), 0)
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h?rev=1144601&r1=1144600&r2=1144601&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h Sat Jul
9 06:13:46 2011
@@ -272,6 +272,7 @@ int AcrSocketPair(SOCKET *rd, SOCKET
int AcrDrainSocket(SOCKET sd);
int AcrNonblock(SOCKET sd, int on);
int AcrWaitIO(SOCKET sd, int timeout, int events);
+char *AcrReadFileDataA(const char *name, char *sbuf, int *len);
size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz);
size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t siz);
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c?rev=1144601&r1=1144600&r2=1144601&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c Sat Jul
9 06:13:46 2011
@@ -44,7 +44,6 @@
typedef struct wls_fd_t {
acr_fd_t fd;
HANDLE fh;
-
} wls_fd_t;
ACR_NET_EXPORT(jint, LocalDescriptor, close0)(JNI_STDARGS, jlong fp)
@@ -144,32 +143,22 @@ ACR_NET_EXPORT(jint, LocalEndpoint, conn
jbyteArray cb, jint timeout)
{
int rc;
- WCHAR sname[NI_MAXHOST];
- char ssbuf[64];
+ char ssbuf[32];
HANDLE sfh = 0;
- DWORD pid, nrd, port;
+ DWORD pid, port;
struct sockaddr_in sa;
int sas = ISIZEOF(sa);
+ int bsz = ISIZEOF(ssbuf);
acr_sockaddr_t *ca = SOCKADDR_CAST(cb);
wls_fd_t *wd = J2P(fp, wls_fd_t *);
- if (MultiByteToWideChar(CP_UTF8, 0, ca->hostname, -1, sname, NI_MAXHOST)
== 0) {
+ if (AcrReadFileDataA(ca->hostname, ssbuf, &bsz) == 0) {
rc = GetLastError();
SOCKADDR_RELEASE(cb, ca);
return rc;
}
SOCKADDR_RELEASE(cb, ca);
- sfh = CreateFileW(sname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
0, 0);
- if (IS_INVALID_HANDLE(sfh)) {
- rc = GetLastError();
- return rc;
- }
- if (!ReadFile(sfh, ssbuf, 63, &nrd, 0)) {
- rc = GetLastError();
- goto finally;
- }
- ssbuf[nrd] = 0;
- if (sscanf(ssbuf, "%u %d", &pid, &port) != 2) {
+ if (sscanf(ssbuf, "%u %u", &pid, &port) != 2) {
/* Bad format ?
*/
rc = ACR_ENOTSOCK;
@@ -222,13 +211,15 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint
SOCKADDR_RELEASE(ba, aa);
return rc;
}
- sfh = CreateFileW(sname, GENERIC_WRITE, FILE_SHARE_READ,
+ sfh = CreateFileW(sname, GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
GetSaWithNullDacl(env, JNI_FALSE), CREATE_NEW,
FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_SYSTEM |
FILE_FLAG_DELETE_ON_CLOSE,
0);
if (IS_INVALID_HANDLE(sfh)) {
rc = GetLastError();
- goto failed;
+ SOCKADDR_RELEASE(ba, aa);
+ return rc;
}
sa.sin_port = 0;
sa.sin_family = AF_INET;
@@ -242,9 +233,8 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint
rc = ACR_GET_NETOS_ERROR();
goto failed;
}
- swr = _snprintf(ssbuf, sizeof(ssbuf), "%u %d\n",
- GetCurrentProcessId(),
- ntohs(sa.sin_port));
+ swr = _snprintf(ssbuf, sizeof(ssbuf), "%u %u\n",
+ GetCurrentProcessId(), (DWORD)ntohs(sa.sin_port));
if (!WriteFile(sfh, ssbuf, swr, &nwr, 0)) {
rc = GetLastError();
goto failed;
@@ -263,3 +253,17 @@ failed:
SAFE_CLOSE_HANDLE(sfh);
return rc;
}
+
+ACR_NET_EXPORT(void, LocalServerEndpoint, unlink0)(JNI_STDARGS, jbyteArray ba)
+{
+ acr_sockaddr_t *sa = SOCKADDR_CAST(ba);
+ if (sa->hostname[0] != 0) {
+ WCHAR sname[NI_MAXHOST];
+ if (MultiByteToWideChar(CP_UTF8, 0, sa->hostname, -1, sname,
NI_MAXHOST) != 0) {
+ /* Unlink the local socket file.
+ */
+ DeleteFileW(sname);
+ }
+ }
+ SOCKADDR_RELEASE(ba, sa);
+}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c?rev=1144601&r1=1144600&r2=1144601&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c Sat Jul
9 06:13:46 2011
@@ -63,7 +63,7 @@ PSECURITY_ATTRIBUTES GetSaWithNullDacl(J
if (!(_null_SA[si] = calloc(1, sizeof(SECURITY_ATTRIBUTES)))) {
rc = ACR_ENOMEM;
- ACR_THROW(ACR_EX_ENOMEM, 0);
+ ACR_THROW_IFENV(ACR_EX_ENOMEM, 0);
goto cleanup;
}
_null_SA[si]->nLength = TSIZEOF(DWORD, SECURITY_ATTRIBUTES);
@@ -71,7 +71,7 @@ PSECURITY_ATTRIBUTES GetSaWithNullDacl(J
pSD = calloc(1, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (pSD == 0) {
rc = ACR_ENOMEM;
- ACR_THROW(ACR_EX_ENOMEM, 0);
+ ACR_THROW_IFENV(ACR_EX_ENOMEM, 0);
goto cleanup;
}
_null_SA[si]->lpSecurityDescriptor = pSD;
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c?rev=1144601&r1=1144600&r2=1144601&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c Sat Jul 9
06:13:46 2011
@@ -523,3 +523,66 @@ AcrWaitIO(SOCKET sd, int timeout, int ev
return ACR_TIMEUP;
return 0;
}
+
+char *
+AcrReadFileDataA(const char *name, char *sbuf, int *len)
+{
+ DWORD rc = 0;
+ int flen = 0;
+ WCHAR wname[ACR_PATH_MAX];
+ HANDLE fh = 0;
+ char *buff = 0;
+ DWORD bpos = 0;
+ DWORD blen = 1024;
+
+ if (name == 0 || *name == 0) {
+ ACR_SET_OS_ERROR(ACR_EINVAL);
+ return 0;
+ }
+ if (len == 0)
+ len = &flen;
+ if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, ACR_PATH_MAX) == 0)
+ return 0;
+ fh = CreateFileW(wname, GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ GetSaWithNullDacl(0, JNI_FALSE), OPEN_EXISTING, 0, 0);
+ if (IS_INVALID_HANDLE(fh))
+ return 0;
+ if (sbuf != 0 && *len > 0) {
+ if (!ReadFile(fh, sbuf, *(len - 1), &bpos, 0)) {
+ rc = GetLastError();
+ goto failed;
+ }
+ sbuf[bpos] = '\0';
+ buff = sbuf;
+ }
+ else {
+ for (;;) {
+ DWORD nrd;
+ char *bpp = realloc(buff, blen);
+ if (bpp == 0) {
+ rc = ACR_ENOMEM;
+ goto failed;
+ }
+ buff = bpp;
+ if (!ReadFile(fh, buff + bpos, 1024, &nrd, 0)) {
+ rc = GetLastError();
+ goto failed;
+ }
+ bpos += nrd;
+ if (nrd < 1023) {
+ buff[bpos] = '\0';
+ break;
+ }
+ blen += 1024;
+ }
+ }
+ *len = bpos;
+ return buff;
+
+failed:
+ AcrFree(buff);
+ SAFE_CLOSE_HANDLE(fh);
+ ACR_SET_OS_ERROR(rc);
+ return 0;
+}
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1144601&r1=1144600&r2=1144601&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Sat Jul 9
06:13:46 2011
@@ -852,14 +852,18 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
* current directory gets changed.
*/
DWORD nch = GetFullPathNameW(J2S(hostname), NI_MAXHOST, buf, 0);
- if (nch == 0 || nch > NI_MAXHOST) {
- /* Error or lack of space */
- rc = nch == 0 ? GetLastError() : ACR_EOVERFLOW;
+ if (nch == 0) {
+ /* Conversion error */
+ rc = GetLastError();
+ }
+ else if (nch > NI_MAXHOST) {
+ /* Lack of space */
+ rc = ACR_EOVERFLOW;
}
else {
if (WideCharToMultiByte(CP_UTF8, 0, buf, -1, sa.hostname,
NI_MAXHOST, 0, 0) == 0) {
- /* COnversion failed. */
+ /* Conversion failed. */
rc = GetLastError();
}
else {