gfal_testrw.c

This exemple show how to open, write a file and read it after ex : use case with SRM plugin : ./gfal_testrw srm://grid05.lal.in2p3.fr:8446/dpm/lal.in2p3.fr/home/dteam/test_yoda

00001 
00007 #include <fcntl.h>
00008 #include <stdio.h>
00009 #include <gfal_api.h>
00010 #include <stdlib.h>
00011 #define BLKLEN 65536
00012 
00013 main(int argc, char **argv)
00014 {
00015    int fd;
00016    int i;
00017    char ibuf[BLKLEN];
00018    char obuf[BLKLEN];
00019    int rc;
00020 
00021    // gfal_set_verbose(GFAL_VERBOSE_TRACE | GFAL_VERBOSE_VERBOSE);      // switch Gfal in verbose mode
00022    if (argc != 2) {
00023            fprintf (stderr, "usage: %s filename\n", argv[0]);
00024            exit (1);
00025    }
00026 
00027    for (i = 0; i < BLKLEN; i++)
00028            obuf[i] = i;
00029 
00030    printf ("creating file %s\n", argv[1]);
00031    if ((fd = gfal_open (argv[1], O_WRONLY|O_CREAT, 0644)) < 0) {
00032                 gfal_posix_check_error();
00033                 exit (1);
00034    }
00035    
00036         printf ("open successful, fd = %d\n", fd);
00037 
00038         if ((rc = gfal_write (fd, obuf, BLKLEN)) != BLKLEN) {
00039                 gfal_posix_check_error();
00040                 (void) gfal_close (fd);
00041                 exit (1);
00042         }
00043         printf ("write successful\n");
00044 
00045         if ((rc = gfal_close (fd)) < 0) {
00046                 gfal_posix_check_error();
00047                 exit (1);
00048         }
00049         printf ("close successful\n");
00050 
00051 
00052         printf ("reading back %s\n", argv[1]);
00053         if ((fd = gfal_open (argv[1], O_RDONLY, 0)) < 0) {
00054                 gfal_posix_check_error();
00055                 exit (1);
00056         }
00057         printf ("open successful, fd = %d\n", fd);
00058 
00059         if ((rc = gfal_read (fd, ibuf, BLKLEN)) != BLKLEN) {
00060                         gfal_posix_check_error();
00061                         (void) gfal_close (fd);
00062                         exit (1);
00063            }
00064            printf ("read successful\n");
00065 
00066            if ((rc = gfal_close (fd)) < 0) {
00067                         gfal_posix_check_error();
00068                         exit (1);
00069            }
00070            printf ("close successful\n");
00071 
00072            for (i = 0; i < BLKLEN; i++) {
00073                    if (ibuf[i] != obuf[i]) {
00074                                 fprintf (stderr, "compare failed at offset %d\n", i);
00075                                 exit (1);
00076                    }
00077            }
00078            printf ("compare successful\n");
00079            exit (0);
00080 }

Generated on Mon Aug 6 19:54:27 2012 for CERN GFAL 2.0 by  doxygen 1.4.7