00001
00002
00003
00004
00005
00006
00007
00008 #include "ad_gridftp.h"
00009 #include "adioi.h"
00010
00011 static globus_mutex_t resize_lock;
00012 static globus_cond_t resize_cond;
00013 static globus_bool_t resize_done;
00014 static globus_bool_t resize_success;
00015
00016 void resize_cb(void *myargs, globus_ftp_client_handle_t *handle,
00017 globus_object_t *error)
00018 {
00019 if (error)
00020 {
00021 FPRINTF(stderr, "%s\n", globus_object_printable_to_string(error));
00022 globus_mutex_lock(&resize_lock);
00023 resize_success=GLOBUS_FALSE;
00024 globus_mutex_unlock(&resize_lock);
00025 }
00026 else
00027 {
00028 globus_mutex_lock(&resize_lock);
00029 resize_success=GLOBUS_TRUE;
00030 globus_mutex_unlock(&resize_lock);
00031 }
00032 globus_mutex_lock(&resize_lock);
00033 resize_done=GLOBUS_TRUE;
00034 globus_cond_signal(&resize_cond);
00035 globus_mutex_unlock(&resize_lock);
00036 }
00037
00038
00039 static void resize_wrdata_cb(void *myargs, globus_ftp_client_handle_t *handle, globus_object_t *error,
00040 globus_byte_t *buffer, globus_size_t length, globus_off_t offset,
00041 globus_bool_t eof)
00042 {
00043 if (error)
00044 FPRINTF(stderr, "%s\n", globus_object_printable_to_string(error));
00045 if (!eof)
00046 globus_ftp_client_register_read(handle,
00047 buffer,
00048 length,
00049 resize_wrdata_cb,
00050 myargs);
00051 return;
00052 }
00053
00054
00055 void ADIOI_GRIDFTP_Resize(ADIO_File fd, ADIO_Offset size, int *error_code)
00056 {
00057 int myrank, nprocs;
00058 char myname[]="ADIOI_GRIDFTP_Resize";
00059 globus_off_t fsize;
00060 globus_result_t result;
00061
00062 *error_code = MPI_SUCCESS;
00063
00064 MPI_Comm_size(fd->comm, &nprocs);
00065 MPI_Comm_rank(fd->comm, &myrank);
00066
00067
00068 if ( fd->access_mode&ADIO_RDONLY )
00069 {
00070 FPRINTF(stderr,"%s: attempt to resize read-only file %s!\n",
00071 myname,fd->filename);
00072 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00073 myname, __LINE__, MPI_ERR_IO,
00074 "**io", 0);
00075 return;
00076 }
00077
00078
00079
00080 globus_mutex_init(&resize_lock,GLOBUS_NULL);
00081 globus_cond_init(&resize_cond,GLOBUS_NULL);
00082 resize_done=GLOBUS_FALSE;
00083 if ( (result=globus_ftp_client_size(&(gridftp_fh[fd->fd_sys]),
00084 fd->filename,
00085 &(oattr[fd->fd_sys]),
00086 &(fsize),
00087 resize_cb,
00088 GLOBUS_NULL))!=GLOBUS_SUCCESS )
00089 {
00090 globus_err_handler("globus_ftp_client_size",myname,result);
00091 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
00092 myname, __LINE__, MPI_ERR_IO,
00093 "**io",
00094 "**io %s", globus_object_printable_to_string(globus_error_get(result)));
00095 return;
00096 }
00097 globus_mutex_lock(&resize_lock);
00098 while ( resize_done!=GLOBUS_TRUE )
00099 globus_cond_wait(&resize_lock,&resize_cond);
00100 if ( fsize < (globus_off_t)size )
00101 {
00102
00103
00104 globus_byte_t touchbuf=(globus_byte_t)'\0';
00105 resize_done=GLOBUS_FALSE;
00106 if ( (result=globus_ftp_client_partial_put(&(gridftp_fh[fd->fd_sys]),
00107 fd->filename,
00108 &(oattr[fd->fd_sys]),
00109 GLOBUS_NULL,
00110 (globus_off_t)size,
00111 (globus_off_t)size,
00112 resize_cb,
00113 GLOBUS_NULL))!=GLOBUS_SUCCESS )
00114 {
00115 globus_err_handler("globus_ftp_client_partial_put",myname,result);
00116 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00117 MPIR_ERR_RECOVERABLE, myname, __LINE__,
00118 MPI_ERR_IO, "**io", "**io %s",
00119 globus_object_printable_to_string(globus_error_get(result)));
00120 return;
00121 }
00122
00123 if ( (result=globus_ftp_client_register_write(&(gridftp_fh[fd->fd_sys]),
00124 (globus_byte_t *)&touchbuf,
00125 0,
00126 (globus_off_t)0,
00127 GLOBUS_TRUE,
00128 resize_wrdata_cb,
00129 GLOBUS_NULL))!=GLOBUS_SUCCESS )
00130 {
00131 globus_err_handler("globus_ftp_client_register_write",myname,result);
00132 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00133 MPIR_ERR_RECOVERABLE, myname, __LINE__,
00134 MPI_ERR_IO, "**io", "**io %s",
00135 globus_object_printable_to_string(globus_error_get(result)));
00136 return;
00137 }
00138 globus_mutex_lock(&resize_lock);
00139 while ( resize_done!=GLOBUS_TRUE )
00140 globus_cond_wait(&resize_cond,&resize_lock);
00141 globus_mutex_unlock(&resize_lock);
00142 }
00143 else if ( fsize > (globus_off_t)size )
00144 {
00145
00146
00147
00148 char *urlold;
00149 size_t urllen;
00150
00151 urllen=strlen(fd->filename);
00152 urlold=(char *)ADIOI_Malloc(urllen+5);
00153 ADIOI_Snprintf(urlold,urllen+5,"%s.old",fd->filename);
00154 resize_done=GLOBUS_FALSE;
00155 resize_success=GLOBUS_FALSE;
00156 if ( (result=globus_ftp_client_move(&(gridftp_fh[fd->fd_sys]),
00157 fd->filename,
00158 urlold,
00159 &(oattr[fd->fd_sys]),
00160 resize_cb,
00161 GLOBUS_NULL))!=GLOBUS_SUCCESS )
00162 {
00163 globus_err_handler("globus_ftp_client_move",myname,result);
00164 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00165 MPIR_ERR_RECOVERABLE, myname, __LINE__,
00166 MPI_ERR_IO, "**io", "**io %s",
00167 globus_object_printable_to_string(globus_error_get(result)));
00168 return;
00169 }
00170 globus_mutex_lock(&resize_lock);
00171 while ( resize_done!=GLOBUS_TRUE )
00172 globus_cond_wait(&resize_cond,&resize_lock);
00173 globus_mutex_unlock(&resize_lock);
00174 if ( resize_success!=GLOBUS_TRUE )
00175 {
00176 *error_code = MPI_ERR_IO;
00177 return;
00178 }
00179 resize_done=GLOBUS_FALSE;
00180 if ( (result=globus_ftp_client_partial_third_party_transfer(&(gridftp_fh[fd->fd_sys]),
00181 urlold,
00182 &(oattr[fd->fd_sys]),
00183 fd->filename,
00184 &(oattr[fd->fd_sys]),
00185 GLOBUS_NULL,
00186 0,
00187 (globus_off_t)size,
00188 resize_cb,
00189 GLOBUS_NULL))!=GLOBUS_SUCCESS )
00190 {
00191 globus_err_handler("globus_ftp_client_partial_third_party_transfer",myname,result);
00192 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00193 MPIR_ERR_RECOVERABLE, myname, __LINE__,
00194 MPI_ERR_IO, "**io", "**io %s",
00195 globus_object_printable_to_string(globus_error_get(result)));
00196 return;
00197 }
00198 globus_mutex_lock(&resize_lock);
00199 while ( resize_done!=GLOBUS_TRUE )
00200 globus_cond_wait(&resize_cond,&resize_lock);
00201 globus_mutex_unlock(&resize_lock);
00202 if ( resize_success!=GLOBUS_TRUE )
00203 {
00204 *error_code = MPI_ERR_IO;
00205 ADIOI_Error(fd,*error_code,myname);
00206 return;
00207 }
00208 resize_done=GLOBUS_FALSE;
00209 if ( (result=globus_ftp_client_delete(&(gridftp_fh[fd->fd_sys]),
00210 urlold,
00211 &(oattr[fd->fd_sys]),
00212 resize_cb,
00213 GLOBUS_NULL))!=GLOBUS_SUCCESS )
00214 {
00215 globus_err_handler("globus_ftp_client_delete",myname,result);
00216 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00217 MPIR_ERR_RECOVERABLE, myname, __LINE__,
00218 MPI_ERR_IO, "**io", "**io %s",
00219 globus_object_printable_to_string(globus_error_get(result)));
00220 return;
00221 }
00222 globus_mutex_lock(&resize_lock);
00223 while ( resize_done!=GLOBUS_TRUE )
00224 globus_cond_wait(&resize_cond,&resize_lock);
00225 globus_mutex_unlock(&resize_lock);
00226 if ( resize_success!=GLOBUS_TRUE )
00227 {
00228 *error_code = MPI_ERR_IO;
00229 ADIOI_Error(fd,*error_code,myname);
00230 return;
00231 }
00232 ADIOI_Free(urlold);
00233 }
00234 globus_mutex_destroy(&resize_lock);
00235 globus_cond_destroy(&resize_cond);
00236 }
00237
00238
00239
00240
00241