00001
00002
00003
00004
00005
00006
00007 #include "ad_ntfs.h"
00008
00009 void ADIOI_NTFS_WriteContig(ADIO_File fd, void *buf, int count,
00010 MPI_Datatype datatype, int file_ptr_type,
00011 ADIO_Offset offset, ADIO_Status *status,
00012 int *error_code)
00013 {
00014 static char myname[] = "ADIOI_NTFS_WriteContig";
00015 LONG dwTemp;
00016 DWORD dwNumWritten = 0;
00017 int err=-1, datatype_size, len;
00018 OVERLAPPED *pOvl;
00019
00020
00021
00022 if(file_ptr_type == ADIO_INDIVIDUAL){
00023 offset = fd->fp_ind;
00024 }
00025
00026 MPI_Type_size(datatype, &datatype_size);
00027 len = datatype_size * count;
00028
00029 pOvl = (OVERLAPPED *) ADIOI_Calloc(sizeof(OVERLAPPED), 1);
00030 if (pOvl == NULL)
00031 {
00032 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00033 myname, __LINE__, MPI_ERR_IO,
00034 "**nomem", "**nomem %s", "OVERLAPPED");
00035 return;
00036 }
00037 pOvl->hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
00038 if (pOvl->hEvent == NULL)
00039 {
00040 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00041 err = GetLastError();
00042 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00043 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00044 myname, __LINE__, MPI_ERR_IO,
00045 "**io", "**io %s", errMsg);
00046 ADIOI_Free(pOvl);
00047 return;
00048 }
00049 pOvl->Offset = DWORDLOW(offset);
00050 pOvl->OffsetHigh = DWORDHIGH(offset);
00051
00052 if (file_ptr_type == ADIO_EXPLICIT_OFFSET)
00053 {
00054 if (fd->fp_sys_posn != offset)
00055 {
00056 dwTemp = DWORDHIGH(offset);
00057 if (SetFilePointer(fd->fd_sys, DWORDLOW(offset), &dwTemp, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
00058 {
00059 err = GetLastError();
00060 if (err != NO_ERROR)
00061 {
00062 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00063 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00064 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00065 myname, __LINE__, MPI_ERR_IO,
00066 "**io", "**io %s", errMsg);
00067 CloseHandle(pOvl->hEvent);
00068 ADIOI_Free(pOvl);
00069 return;
00070 }
00071 }
00072 }
00073
00074 err = WriteFile(fd->fd_sys, buf, len, &dwNumWritten, pOvl);
00075
00076 if (err == FALSE)
00077 {
00078 err = GetLastError();
00079 if (err != ERROR_IO_PENDING)
00080 {
00081 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00082 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00083 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00084 myname, __LINE__, MPI_ERR_IO,
00085 "**io",
00086 "**io %s", errMsg);
00087 CloseHandle(pOvl->hEvent);
00088 ADIOI_Free(pOvl);
00089 return;
00090 }
00091 }
00092
00093 err = GetOverlappedResult(fd->fd_sys, pOvl, &dwNumWritten, TRUE);
00094
00095 if (err == FALSE)
00096 {
00097 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00098 err = GetLastError();
00099 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00100 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00101 MPIR_ERR_RECOVERABLE, myname,
00102 __LINE__, MPI_ERR_IO, "**io",
00103 "**io %s", errMsg);
00104 CloseHandle(pOvl->hEvent);
00105 ADIOI_Free(pOvl);
00106 return;
00107 }
00108
00109 if (!CloseHandle(pOvl->hEvent))
00110 {
00111 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00112 err = GetLastError();
00113 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00114 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00115 myname, __LINE__, MPI_ERR_IO,
00116 "**io", "**io %s", errMsg);
00117 CloseHandle(pOvl->hEvent);
00118 ADIOI_Free(pOvl);
00119 return;
00120 }
00121 ADIOI_Free(pOvl);
00122
00123 fd->fp_sys_posn = offset + dwNumWritten;
00124
00125 }
00126 else
00127 {
00128
00129 if (fd->fp_sys_posn != fd->fp_ind)
00130 {
00131 dwTemp = DWORDHIGH(fd->fp_ind);
00132 if (SetFilePointer(fd->fd_sys, DWORDLOW(fd->fp_ind), &dwTemp, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
00133 {
00134 err = GetLastError();
00135 if (err != NO_ERROR)
00136 {
00137 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00138 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00139 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00140 myname, __LINE__, MPI_ERR_IO,
00141 "**io", "**io %s", errMsg);
00142 CloseHandle(pOvl->hEvent);
00143 ADIOI_Free(pOvl);
00144 return;
00145 }
00146 }
00147 }
00148
00149 err = WriteFile(fd->fd_sys, buf, len, &dwNumWritten, pOvl);
00150
00151 if (err == FALSE)
00152 {
00153 err = GetLastError();
00154 if (err != ERROR_IO_PENDING)
00155 {
00156 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00157 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00158 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00159 myname, __LINE__, MPI_ERR_IO,
00160 "**io",
00161 "**io %s", errMsg);
00162 CloseHandle(pOvl->hEvent);
00163 ADIOI_Free(pOvl);
00164 return;
00165 }
00166 }
00167
00168 err = GetOverlappedResult(fd->fd_sys, pOvl, &dwNumWritten, TRUE);
00169
00170 if (err == FALSE)
00171 {
00172 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00173 err = GetLastError();
00174 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00175 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00176 MPIR_ERR_RECOVERABLE, myname,
00177 __LINE__, MPI_ERR_IO, "**io",
00178 "**io %s", errMsg);
00179 CloseHandle(pOvl->hEvent);
00180 ADIOI_Free(pOvl);
00181 return;
00182 }
00183
00184 if (!CloseHandle(pOvl->hEvent))
00185 {
00186 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00187 err = GetLastError();
00188 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00189 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00190 myname, __LINE__, MPI_ERR_IO,
00191 "**io", "**io %s", errMsg);
00192 ADIOI_Free(pOvl);
00193 return;
00194 }
00195 ADIOI_Free(pOvl);
00196
00197 fd->fp_ind = fd->fp_ind + dwNumWritten;
00198 fd->fp_sys_posn = fd->fp_ind;
00199 }
00200
00201 #ifdef HAVE_STATUS_SET_BYTES
00202 if (err != FALSE)
00203 {
00204 MPIR_Status_set_bytes(status, datatype, dwNumWritten);
00205 }
00206 #endif
00207
00208
00209 if (err == FALSE)
00210 {
00211 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00212 err = GetLastError();
00213 ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00214 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00215 myname, __LINE__, MPI_ERR_IO,
00216 "**io",
00217 "**io %s", errMsg);
00218 return;
00219 }
00220
00221 *error_code = MPI_SUCCESS;
00222 }