@@ -22,31 +22,37 @@ static fio_binding fio_bindings[] =
2222{& current .stop_lsn ,sizeof (current .stop_lsn )}
2323};
2424
25+ /* Convert FIO pseudo handle to index in file descriptor array */
2526#define fio_fileno (f ) (((size_t)f - 1) | FIO_PIPE_MARKER)
2627
28+ /* Use specified file descriptors as stding/stdout for FIO functions */
2729void fio_redirect (int in ,int out )
2830{
2931fio_stdin = in ;
3032fio_stdout = out ;
3133}
3234
35+ /* Check if FILE handle is local or remote (created by FIO) */
3336static bool fio_is_remote_file (FILE * file )
3437{
3538return (size_t )file <=FIO_FDMAX ;
3639}
3740
41+ /* Check if file descriptor is local or remote (created by FIO) */
3842static bool fio_is_remote_fd (int fd )
3943{
4044return (fd & FIO_PIPE_MARKER )!= 0 ;
4145}
4246
47+ /* Check if specified location is local for current node */
4348static bool fio_is_remote (fio_location location )
4449{
4550return location == FIO_REMOTE_HOST
46- || (location == FIO_BACKUP_HOST && remote_agent )
51+ || (location == FIO_BACKUP_HOST && remote_agent )/* agent is launched at Postgres side */
4752|| (location == FIO_DB_HOST && !remote_agent && IsSshConnection ());
4853}
4954
55+ /* Try to read specified amount of bytes unless error or EOF are encountered */
5056static ssize_t fio_read_all (int fd ,void * buf ,size_t size )
5157{
5258size_t offs = 0 ;
@@ -66,6 +72,7 @@ static ssize_t fio_read_all(int fd, void* buf, size_t size)
6672return offs ;
6773}
6874
75+ /* Try to write specified amount of bytes unless error is encountered */
6976static ssize_t fio_write_all (int fd ,void const * buf ,size_t size )
7077{
7178size_t offs = 0 ;
@@ -83,6 +90,7 @@ static ssize_t fio_write_all(int fd, void const* buf, size_t size)
8390return offs ;
8491}
8592
93+ /* Open input stream. Remote file is fetched to the in-memory buffer and then accessed through Linux fmemopen */
8694FILE * fio_open_stream (char const * path ,fio_location location )
8795{
8896FILE * f ;
@@ -116,6 +124,7 @@ FILE* fio_open_stream(char const* path, fio_location location)
116124return f ;
117125}
118126
127+ /* Close input stream */
119128int fio_close_stream (FILE * f )
120129{
121130if (fio_stdin_buffer )
@@ -126,6 +135,7 @@ int fio_close_stream(FILE* f)
126135return fclose (f );
127136}
128137
138+ /* Open directory */
129139DIR * fio_opendir (char const * path ,fio_location location )
130140{
131141DIR * dir ;
@@ -161,6 +171,7 @@ DIR* fio_opendir(char const* path, fio_location location)
161171return dir ;
162172}
163173
174+ /* Get next directory entry */
164175struct dirent * fio_readdir (DIR * dir )
165176{
166177if (fio_is_remote_file ((FILE * )dir ))
@@ -193,6 +204,7 @@ struct dirent* fio_readdir(DIR *dir)
193204}
194205}
195206
207+ /* Close directory */
196208int fio_closedir (DIR * dir )
197209{
198210if (fio_is_remote_file ((FILE * )dir ))
@@ -213,7 +225,7 @@ int fio_closedir(DIR *dir)
213225}
214226}
215227
216-
228+ /* Open file */
217229int fio_open (char const * path ,int mode ,fio_location location )
218230{
219231int fd ;
@@ -250,6 +262,7 @@ int fio_open(char const* path, int mode, fio_location location)
250262return fd ;
251263}
252264
265+ /* Open stdio file */
253266FILE * fio_fopen (char const * path ,char const * mode ,fio_location location )
254267{
255268FILE * f ;
@@ -266,6 +279,7 @@ FILE* fio_fopen(char const* path, char const* mode, fio_location location)
266279return f ;
267280}
268281
282+ /* Format output to file stream */
269283int fio_fprintf (FILE * f ,char const * format , ...)
270284{
271285int rc ;
@@ -291,6 +305,7 @@ int fio_fprintf(FILE* f, char const* format, ...)
291305return rc ;
292306}
293307
308+ /* Flush stream data (does nothing for remote file) */
294309int fio_fflush (FILE * f )
295310{
296311int rc = 0 ;
@@ -304,18 +319,21 @@ int fio_fflush(FILE* f)
304319return rc ;
305320}
306321
322+ /* Sync file to the disk (does nothing for remote file) */
307323int fio_flush (int fd )
308324{
309325return fio_is_remote_fd (fd ) ?0 :fsync (fd );
310326}
311327
328+ /* Close output stream */
312329int fio_fclose (FILE * f )
313330{
314331return fio_is_remote_file (f )
315332?fio_close (fio_fileno (f ))
316333:fclose (f );
317334}
318335
336+ /* Close file */
319337int fio_close (int fd )
320338{
321339if (fio_is_remote_fd (fd ))
@@ -340,13 +358,15 @@ int fio_close(int fd)
340358}
341359}
342360
361+ /* Truncate stdio file */
343362int fio_ftruncate (FILE * f ,off_t size )
344363{
345364return fio_is_remote_file (f )
346365?fio_truncate (fio_fileno (f ),size )
347366:ftruncate (fileno (f ),size );
348367}
349368
369+ /* Truncate file */
350370int fio_truncate (int fd ,off_t size )
351371{
352372if (fio_is_remote_fd (fd ))
@@ -371,13 +391,15 @@ int fio_truncate(int fd, off_t size)
371391}
372392}
373393
394+ /* Set position in stdio file */
374395int fio_fseek (FILE * f ,off_t offs )
375396{
376397return fio_is_remote_file (f )
377398?fio_seek (fio_fileno (f ),offs )
378399:fseek (f ,offs ,SEEK_SET );
379400}
380401
402+ /* Set position in file */
381403int fio_seek (int fd ,off_t offs )
382404{
383405if (fio_is_remote_fd (fd ))
@@ -402,13 +424,15 @@ int fio_seek(int fd, off_t offs)
402424}
403425}
404426
427+ /* Write data to stdio file */
405428size_t fio_fwrite (FILE * f ,void const * buf ,size_t size )
406429{
407430return fio_is_remote_file (f )
408431?fio_write (fio_fileno (f ),buf ,size )
409432:fwrite (buf ,1 ,size ,f );
410433}
411434
435+ /* Write data to the file */
412436ssize_t fio_write (int fd ,void const * buf ,size_t size )
413437{
414438if (fio_is_remote_fd (fd ))
@@ -433,13 +457,15 @@ ssize_t fio_write(int fd, void const* buf, size_t size)
433457}
434458}
435459
460+ /* Read data from stdio file */
436461size_t fio_fread (FILE * f ,void * buf ,size_t size )
437462{
438463return fio_is_remote_file (f )
439464?fio_read (fio_fileno (f ),buf ,size )
440465:fread (buf ,1 ,size ,f );
441466}
442467
468+ /* Read data from file */
443469ssize_t fio_read (int fd ,void * buf ,size_t size )
444470{
445471if (fio_is_remote_fd (fd ))
@@ -472,13 +498,15 @@ ssize_t fio_read(int fd, void* buf, size_t size)
472498}
473499}
474500
501+ /* Get information about stdio file */
475502int fio_ffstat (FILE * f ,struct stat * st )
476503{
477504return fio_is_remote_file (f )
478505?fio_fstat (fio_fileno (f ),st )
479506:fio_fstat (fileno (f ),st );
480507}
481508
509+ /* Get information about file descriptor */
482510int fio_fstat (int fd ,struct stat * st )
483511{
484512if (fio_is_remote_fd (fd ))
@@ -510,6 +538,7 @@ int fio_fstat(int fd, struct stat* st)
510538}
511539}
512540
541+ /* Get information about file */
513542int fio_stat (char const * path ,struct stat * st ,bool follow_symlinks ,fio_location location )
514543{
515544if (fio_is_remote (location ))
@@ -544,6 +573,7 @@ int fio_stat(char const* path, struct stat* st, bool follow_symlinks, fio_locati
544573}
545574}
546575
576+ /* Check presence of the file */
547577int fio_access (char const * path ,int mode ,fio_location location )
548578{
549579if (fio_is_remote (location ))
@@ -576,6 +606,7 @@ int fio_access(char const* path, int mode, fio_location location)
576606}
577607}
578608
609+ /* Rename file */
579610int fio_rename (char const * old_path ,char const * new_path ,fio_location location )
580611{
581612if (fio_is_remote (location ))
@@ -602,6 +633,7 @@ int fio_rename(char const* old_path, char const* new_path, fio_location location
602633}
603634}
604635
636+ /* Remove file */
605637int fio_unlink (char const * path ,fio_location location )
606638{
607639if (fio_is_remote (location ))
@@ -626,6 +658,7 @@ int fio_unlink(char const* path, fio_location location)
626658}
627659}
628660
661+ /* Create directory */
629662int fio_mkdir (char const * path ,int mode ,fio_location location )
630663{
631664if (fio_is_remote (location ))
@@ -651,6 +684,7 @@ int fio_mkdir(char const* path, int mode, fio_location location)
651684}
652685}
653686
687+ /* Checnge file mode */
654688int fio_chmod (char const * path ,int mode ,fio_location location )
655689{
656690if (fio_is_remote (location ))
@@ -677,6 +711,8 @@ int fio_chmod(char const* path, int mode, fio_location location)
677711}
678712
679713#ifdef HAVE_LIBZ
714+ /* Open compressed file. In case of remove file, it is fetched to local temporary file in read-only mode or is written
715+ * to temoporary file and rtansfered to remote host by fio_gzclose. */
680716gzFile fio_gzopen (char const * path ,char const * mode ,int * tmp_fd ,fio_location location )
681717{
682718gzFile file ;
@@ -715,6 +751,7 @@ gzFile fio_gzopen(char const* path, char const* mode, int* tmp_fd, fio_location
715751return file ;
716752}
717753
754+ /* Close compressed file. In case of writing remote file, content of temporary file is trasfered to remote host */
718755int fio_gzclose (gzFile file ,char const * path ,int tmp_fd )
719756{
720757if (tmp_fd >=0 )
@@ -749,7 +786,7 @@ int fio_gzclose(gzFile file, char const* path, int tmp_fd)
749786}
750787#endif
751788
752-
789+ /* Send file content */
753790static void fio_send_file (int out ,char const * path )
754791{
755792int fd = open (path ,O_RDONLY );
@@ -776,6 +813,7 @@ static void fio_send_file(int out, char const* path)
776813}
777814}
778815
816+ /* Send values of variables. Variables are described infio_bindings array and "var" is index in this array. */
779817void fio_transfer (fio_shared_variable var )
780818{
781819size_t var_size = fio_bindings [var ].size ;
@@ -794,6 +832,7 @@ void fio_transfer(fio_shared_variable var)
794832free (msg );
795833}
796834
835+ /* Execute commands at remote host */
797836void fio_communicate (int in ,int out )
798837{
799838int fd [FIO_FDMAX ];