~ubuntu-branches/ubuntu/saucy/globus-gass-copy/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/globus-gass-copy-pathmax.patch/globus_gass_copy_glob.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattias Ellert
  • Date: 2010-07-19 11:25:06 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100719112506-smob42wt962q4aqg
Tags: 5.7-1
Update to Globus Toolkit 5.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 */
16
16
 
17
17
#include "globus_gass_copy.h"
 
18
#include "openssl/md5.h"
18
19
 
19
20
#ifndef TARGET_ARCH_WIN32
20
21
#include <fnmatch.h>
135
136
    int                                 url_len;
136
137
    int                                 path_len;
137
138
    globus_bool_t                       glob = GLOBUS_TRUE;
138
 
    globus_gass_copy_glob_stat_t        info_stat;  
 
139
    globus_gass_copy_glob_stat_t *      stat_info;  
139
140
    char *                              path;
140
141
 
 
142
 
 
143
    stat_info = (globus_gass_copy_glob_stat_t *)
 
144
         globus_calloc(1, sizeof(globus_gass_copy_glob_stat_t));
 
145
         
141
146
    info = (globus_l_gass_copy_glob_info_t *)
142
147
        globus_malloc(sizeof(globus_l_gass_copy_glob_info_t));
143
148
    
193
198
        }
194
199
        else
195
200
#endif
196
 
        {    
197
 
            info_stat.type = GLOBUS_GASS_COPY_GLOB_ENTRY_UNKNOWN;
198
 
            info_stat.unique_id = GLOBUS_NULL;
199
 
            info_stat.symlink_target = GLOBUS_NULL;
200
 
            info_stat.mode = -1;
201
 
            info_stat.mdtm = -1;
202
 
            info_stat.size = -1;
203
 
            
204
 
            info->entry_cb(
205
 
                info->url,
206
 
                &info_stat,
 
201
        if(handle->always_stat_on_expand)
 
202
        {
 
203
            result = globus_gass_copy_stat(
 
204
                handle, info->url, attr, stat_info);
 
205
            
 
206
            info->entry_cb(
 
207
                info->url,
 
208
                stat_info,
 
209
                info->entry_user_arg);
 
210
                
 
211
            if(stat_info->symlink_target)
 
212
            {
 
213
                globus_free(stat_info->symlink_target);
 
214
            }
 
215
            if(stat_info->unique_id)
 
216
            {
 
217
                globus_free(stat_info->unique_id);
 
218
            }
 
219
                
 
220
            glob = GLOBUS_FALSE;
 
221
        }
 
222
        else
 
223
        {
 
224
            stat_info->type = GLOBUS_GASS_COPY_GLOB_ENTRY_UNKNOWN;
 
225
            stat_info->unique_id = GLOBUS_NULL;
 
226
            stat_info->symlink_target = GLOBUS_NULL;
 
227
            stat_info->mode = -1;
 
228
            stat_info->mdtm = -1;
 
229
            stat_info->size = -1;
 
230
            
 
231
            info->entry_cb(
 
232
                info->url,
 
233
                stat_info,
207
234
                info->entry_user_arg);
208
235
                 
209
236
            glob = GLOBUS_FALSE;
249
276
 
250
277
    globus_free(info->url);
251
278
    globus_free(info);
 
279
    globus_free(stat_info);
252
280
    return result;
253
281
            
254
282
error:
255
283
    globus_free(info->url);
256
284
    globus_free(info);
 
285
    globus_free(stat_info);
257
286
    return result;
258
287
}
259
288
 
864
893
    return GLOBUS_FAILURE;
865
894
}
866
895
    
 
896
static
 
897
globus_result_t
 
898
globus_l_gass_copy_glob_parse_mlst_line(
 
899
    char *                              line,
 
900
    globus_gass_copy_glob_stat_t *      stat_info)
 
901
{
 
902
    static char *   myname = "globus_l_gass_copy_glob_parse_ftp_list";
 
903
    globus_result_t                     result;
 
904
    int                                 i;
 
905
    char *                              space;
 
906
    char *                              filename;
 
907
    char *                              startline;
 
908
    char *                              startfact;
 
909
    char *                              endfact;
 
910
    char *                              factval;
 
911
    
 
912
    char *                              unique_id = NULL;
 
913
    char *                              mode_s = NULL;
 
914
    char *                              symlink_target = NULL;
 
915
    char *                              modify_s = NULL;
 
916
    char *                              size_s = NULL;
 
917
    globus_gass_copy_glob_entry_t       type;
 
918
 
 
919
    startline = line;
 
920
    
 
921
    space = strchr(startline, ' ');
 
922
    if(space == GLOBUS_NULL)
 
923
    {
 
924
        result = globus_error_put(
 
925
            globus_error_construct_string(
 
926
                GLOBUS_GASS_COPY_MODULE,
 
927
                GLOBUS_NULL,
 
928
                "[%s]: Bad MLSD response",
 
929
                myname));
 
930
                  
 
931
        goto error_invalid_mlsd;
 
932
    }
 
933
    *space = '\0';            
 
934
    filename = space + 1;
 
935
    startfact = startline;
 
936
    
 
937
    while(startfact != space)
 
938
    {
 
939
        endfact = strchr(startfact, ';');
 
940
        if(endfact)
 
941
        {             
 
942
            *endfact = '\0';
 
943
        }
 
944
        else
 
945
        {                                     
 
946
/*
 
947
         older MLST-draft spec says ending fact can be missing
 
948
         the final semicolon... not a problem to support this,
 
949
         no need to die.  (ncftpd does this) 
 
950
            
 
951
            result = globus_error_put(
 
952
                globus_error_construct_string(
 
953
                    GLOBUS_GASS_COPY_MODULE,
 
954
                    GLOBUS_NULL,
 
955
                    "[%s]: Bad MLSD response",
 
956
                    myname));
 
957
                  
 
958
            goto error_invalid_mlsd;
 
959
*/
 
960
 
 
961
            endfact = space - 1;   
 
962
        }
 
963
        
 
964
        factval = strchr(startfact, '=');
 
965
        if(!factval)
 
966
        {             
 
967
            result = globus_error_put(
 
968
                globus_error_construct_string(
 
969
                    GLOBUS_GASS_COPY_MODULE,
 
970
                    GLOBUS_NULL,
 
971
                    "[%s]: Bad MLSD response",
 
972
                    myname));
 
973
                  
 
974
            goto error_invalid_mlsd;
 
975
        }
 
976
        *(factval++) = '\0';
 
977
 
 
978
        for(i = 0; startfact[i] != '\0'; i++)
 
979
        {
 
980
            startfact[i] = tolower(startfact[i]);
 
981
        }
 
982
    
 
983
        if(strcmp(startfact, "type") == 0)
 
984
        {
 
985
            if(strcasecmp(factval, "dir") == 0)
 
986
            {
 
987
                type = GLOBUS_GASS_COPY_GLOB_ENTRY_DIR;
 
988
            }
 
989
            else if(strcasecmp(factval, "file") == 0)
 
990
            {
 
991
                type = GLOBUS_GASS_COPY_GLOB_ENTRY_FILE;
 
992
            }
 
993
            else
 
994
            {
 
995
                type = GLOBUS_GASS_COPY_GLOB_ENTRY_OTHER;
 
996
            } 
 
997
        }                 
 
998
        if(strcmp(startfact, "unique") == 0)
 
999
        {
 
1000
            unique_id = factval;                        
 
1001
        }                 
 
1002
        if(strcmp(startfact, "unix.mode") == 0)
 
1003
        {
 
1004
            mode_s = factval;                        
 
1005
        }
 
1006
        if(strcmp(startfact, "modify") == 0)
 
1007
        {
 
1008
            modify_s = factval;                        
 
1009
        }                 
 
1010
        if(strcmp(startfact, "size") == 0)
 
1011
        {
 
1012
            size_s = factval;                        
 
1013
        }                 
 
1014
        if(strcmp(startfact, "unix.slink") == 0)
 
1015
        {
 
1016
            symlink_target = factval;                        
 
1017
        }                 
 
1018
        
 
1019
        startfact = endfact + 1;                                 
 
1020
    } 
 
1021
 
 
1022
    
 
1023
    stat_info->type = type;
 
1024
    stat_info->unique_id = globus_libc_strdup(unique_id);
 
1025
    stat_info->symlink_target = globus_libc_strdup(symlink_target);
 
1026
    stat_info->mode = -1;
 
1027
    stat_info->size = -1;
 
1028
    stat_info->mdtm = -1;
 
1029
        
 
1030
    if(mode_s)
 
1031
    {
 
1032
        stat_info->mode = strtoul(mode_s, NULL, 0);
 
1033
    }
 
1034
    
 
1035
    if(size_s)
 
1036
    {
 
1037
        globus_off_t            size;
 
1038
        int                     rc;
 
1039
        
 
1040
        rc = sscanf(size_s, "%"GLOBUS_OFF_T_FORMAT, &size);
 
1041
        if(rc == 1)
 
1042
        {
 
1043
            stat_info->size = size;
 
1044
        }
 
1045
    }
 
1046
    
 
1047
    if(modify_s)
 
1048
    {
 
1049
        int                     mdtm;
 
1050
        
 
1051
        if(globus_l_gass_copy_mdtm_to_timet(modify_s, &mdtm) == 
 
1052
            GLOBUS_SUCCESS)
 
1053
        {
 
1054
            stat_info->mdtm = mdtm;
 
1055
        }
 
1056
    }
 
1057
 
 
1058
    return GLOBUS_SUCCESS;
 
1059
 
 
1060
error_invalid_mlsd:
 
1061
 
 
1062
    return result;
 
1063
}
867
1064
 
868
1065
static
869
1066
globus_result_t
1275
1472
 
1276
1473
 
1277
1474
 
 
1475
static
 
1476
globus_result_t
 
1477
globus_l_gass_copy_stat_ftp(
 
1478
    globus_gass_copy_handle_t *         handle,
 
1479
    char *                              url,
 
1480
    globus_gass_copy_attr_t *           attr,
 
1481
    globus_gass_copy_glob_stat_t *      stat_info)
 
1482
{
 
1483
    globus_result_t                     result;
 
1484
    globus_l_gass_copy_glob_info_t      info;
 
1485
    globus_byte_t *                     mlst_buf;
 
1486
    globus_size_t                       mlst_len;
 
1487
    
 
1488
    info.callbacks_left = 1;
 
1489
    info.err = GLOBUS_NULL;
 
1490
    globus_cond_init(&info.cond, GLOBUS_NULL);
 
1491
    globus_mutex_init(&info.mutex, GLOBUS_NULL);
 
1492
        
 
1493
    result = globus_ftp_client_mlst(
 
1494
        &handle->ftp_handle,
 
1495
        url,
 
1496
        attr->ftp_attr,
 
1497
        &mlst_buf,
 
1498
        &mlst_len,
 
1499
        globus_l_gass_copy_ftp_client_op_done_callback,
 
1500
        &info);
 
1501
    if(result != GLOBUS_SUCCESS)
 
1502
    {
 
1503
        goto error_mlst;
 
1504
    }
 
1505
 
 
1506
    globus_mutex_lock(&info.mutex);
 
1507
    while(info.callbacks_left > 0)
 
1508
    {
 
1509
        globus_cond_wait(&info.cond, &info.mutex);
 
1510
    }
 
1511
    globus_mutex_unlock(&info.mutex);
 
1512
 
 
1513
    if(info.err)
 
1514
    {
 
1515
        result = globus_error_put(info.err);
 
1516
        info.err = GLOBUS_NULL;
 
1517
    }
 
1518
    
 
1519
    if(result != GLOBUS_SUCCESS)
 
1520
    {
 
1521
        goto error_mlst;
 
1522
    }
 
1523
    
 
1524
    result = globus_l_gass_copy_glob_parse_mlst_line(
 
1525
        (char *) mlst_buf, stat_info);
 
1526
    
 
1527
    if(result != GLOBUS_SUCCESS)
 
1528
    {
 
1529
        goto error_parse;
 
1530
    }
 
1531
 
 
1532
    globus_free(mlst_buf);
 
1533
    globus_cond_destroy(&info.cond);
 
1534
    globus_mutex_destroy(&info.mutex);
 
1535
 
 
1536
    return GLOBUS_SUCCESS;
 
1537
 
 
1538
error_parse:
 
1539
    globus_free(mlst_buf);
 
1540
 
 
1541
error_mlst:
 
1542
    globus_cond_destroy(&info.cond);
 
1543
    globus_mutex_destroy(&info.mutex);
 
1544
 
 
1545
    return result;
 
1546
}
 
1547
 
 
1548
static
 
1549
globus_result_t
 
1550
globus_l_gass_copy_stat_file(
 
1551
    char *                              url,
 
1552
    globus_gass_copy_glob_stat_t *      stat_info)
 
1553
{
 
1554
    static char *   myname = "globus_l_gass_copy_stat_file";    
 
1555
    int                                 rc;
 
1556
    globus_result_t                     result;
 
1557
    struct stat                         stat_buf;
 
1558
    globus_url_t                        parsed_url;
 
1559
    char                                unique_id[256];
 
1560
    globus_gass_copy_glob_entry_t       type;
 
1561
    char                                symlink_target[MAXPATHLEN*2];
 
1562
    
 
1563
    rc = globus_url_parse(url, &parsed_url);
 
1564
    
 
1565
    if(rc != 0)
 
1566
    {
 
1567
        result = globus_error_put(
 
1568
            globus_error_construct_string(
 
1569
                GLOBUS_GASS_COPY_MODULE,
 
1570
                GLOBUS_NULL,
 
1571
                "[%s]: error parsing url: "
 
1572
                "globus_url_parse returned %d",
 
1573
                myname,
 
1574
                rc));
 
1575
        goto error_url;
 
1576
    }
 
1577
    
 
1578
    if(parsed_url.url_path == GLOBUS_NULL)
 
1579
    {
 
1580
        result = globus_error_put(
 
1581
            globus_error_construct_string(
 
1582
                GLOBUS_GASS_COPY_MODULE,
 
1583
                GLOBUS_NULL,
 
1584
                "[%s]: error parsing url: "
 
1585
                "url has no path",
 
1586
                myname));
 
1587
        goto error_null_path;
 
1588
    }
 
1589
    
 
1590
    if(lstat(parsed_url.url_path, &stat_buf) != 0)
 
1591
    {
 
1592
        result = globus_error_put(
 
1593
            globus_error_construct_string(
 
1594
                GLOBUS_GASS_COPY_MODULE,
 
1595
                GLOBUS_NULL,
 
1596
                "[%s]: invalid entry in dir: %s",
 
1597
                myname,
 
1598
                parsed_url.url_path));
 
1599
        goto error_stat;
 
1600
    }
 
1601
 
 
1602
    *symlink_target = '\0';
 
1603
    if(S_ISLNK(stat_buf.st_mode))
 
1604
    {
 
1605
        if(stat(parsed_url.url_path, &stat_buf) != 0)
 
1606
        {
 
1607
            result = globus_error_put(
 
1608
                globus_error_construct_string(
 
1609
                    GLOBUS_GASS_COPY_MODULE,
 
1610
                    GLOBUS_NULL,
 
1611
                    "[%s]: invalid symlink entry in dir: %s",
 
1612
                    myname,
 
1613
                    parsed_url.url_path)); 
 
1614
            goto error_stat;
 
1615
        }
 
1616
        if(realpath(parsed_url.url_path, symlink_target) == NULL)
 
1617
        {
 
1618
            result = globus_error_put(
 
1619
                globus_error_construct_string(
 
1620
                    GLOBUS_GASS_COPY_MODULE,
 
1621
                    GLOBUS_NULL,
 
1622
                    "[%s]: unable to find path of symlink in dir: %s",
 
1623
                    myname,
 
1624
                    parsed_url.url_path));
 
1625
            goto error_stat;
 
1626
        }
 
1627
    }    
 
1628
 
 
1629
    if(S_ISDIR(stat_buf.st_mode))
 
1630
    {
 
1631
        type = GLOBUS_GASS_COPY_GLOB_ENTRY_DIR;
 
1632
    }
 
1633
    else
 
1634
    {                 
 
1635
        type = GLOBUS_GASS_COPY_GLOB_ENTRY_FILE;
 
1636
    } 
 
1637
 
 
1638
    *unique_id = '\0';        
 
1639
    sprintf(
 
1640
        unique_id,
 
1641
        "%lx-%lx;",
 
1642
        (unsigned long) stat_buf.st_dev, 
 
1643
        (unsigned long) stat_buf.st_ino);
 
1644
 
 
1645
    stat_info->type = type;
 
1646
    stat_info->unique_id = globus_libc_strdup(unique_id);
 
1647
    stat_info->symlink_target = *symlink_target ? 
 
1648
        globus_libc_strdup(symlink_target) : NULL;
 
1649
    stat_info->mode = stat_buf.st_mode & 07777;
 
1650
    stat_info->mdtm = (int) stat_buf.st_mtime;
 
1651
    stat_info->size = stat_buf.st_size;
 
1652
           
 
1653
    globus_url_destroy(&parsed_url); 
 
1654
    return GLOBUS_SUCCESS;
 
1655
 
 
1656
error_stat:    
 
1657
error_null_path:
 
1658
    globus_url_destroy(&parsed_url);
 
1659
    
 
1660
error_url:
 
1661
 
 
1662
    return result;
 
1663
    
 
1664
}
 
1665
 
 
1666
globus_result_t
 
1667
globus_gass_copy_stat(
 
1668
    globus_gass_copy_handle_t *         handle,
 
1669
    char *                              url,
 
1670
    globus_gass_copy_attr_t *           attr,
 
1671
    globus_gass_copy_glob_stat_t *      stat_info)
 
1672
{   
 
1673
    static char *   myname = "globus_gass_copy_stat";    
 
1674
 
 
1675
    globus_result_t                     result;
 
1676
    globus_gass_copy_url_mode_t         url_mode;
 
1677
    
 
1678
    result = globus_gass_copy_get_url_mode(url, &url_mode);
 
1679
    if(result != GLOBUS_SUCCESS)
 
1680
    {
 
1681
        goto error_exit;
 
1682
    }
 
1683
    
 
1684
    if(url_mode == GLOBUS_GASS_COPY_URL_MODE_FTP)
 
1685
    {
 
1686
        result = globus_l_gass_copy_stat_ftp(handle, url, attr, stat_info);
 
1687
 
 
1688
        if(result != GLOBUS_SUCCESS)
 
1689
        {
 
1690
            goto error_exit;
 
1691
        }
 
1692
    }
 
1693
    else if(url_mode == GLOBUS_GASS_COPY_URL_MODE_IO)
 
1694
    {
 
1695
        result = globus_l_gass_copy_stat_file(url, stat_info);
 
1696
        if(result != GLOBUS_SUCCESS)
 
1697
        {
 
1698
            goto error_exit;
 
1699
        }
 
1700
    }
 
1701
    else
 
1702
    {
 
1703
        result = globus_error_put(
 
1704
            globus_error_construct_string(
 
1705
                GLOBUS_GASS_COPY_MODULE,
 
1706
                GLOBUS_NULL,
 
1707
                "[%s]: unsupported URL scheme: %s",
 
1708
                myname,
 
1709
                url));
 
1710
        goto error_exit;
 
1711
    }
 
1712
    
 
1713
    return GLOBUS_SUCCESS;
 
1714
    
 
1715
error_exit:
 
1716
 
 
1717
    return result;
 
1718
    
 
1719
}
 
1720
 
 
1721
 
1278
1722
globus_result_t
1279
1723
globus_gass_copy_mkdir(
1280
1724
    globus_gass_copy_handle_t *         handle,
1461
1905
    
1462
1906
}
1463
1907
 
 
1908
 
 
1909
 
 
1910
static
 
1911
globus_result_t
 
1912
globus_l_gass_copy_cksm_ftp(
 
1913
    globus_gass_copy_handle_t *         handle,
 
1914
    char *                              url,
 
1915
    globus_gass_copy_attr_t *           attr,
 
1916
    char *                              cksm,
 
1917
    globus_off_t                        offset,
 
1918
    globus_off_t                        length,
 
1919
    const char *                        algorithm)
 
1920
{
 
1921
    globus_result_t                     result;
 
1922
    globus_l_gass_copy_glob_info_t      info;
 
1923
 
 
1924
    info.callbacks_left = 1;
 
1925
    info.err = GLOBUS_NULL;
 
1926
 
 
1927
    globus_mutex_init(&info.mutex, GLOBUS_NULL);
 
1928
    globus_cond_init(&info.cond, GLOBUS_NULL);
 
1929
     
 
1930
 
 
1931
    result = globus_ftp_client_cksm(
 
1932
                 &handle->ftp_handle,
 
1933
                 url,
 
1934
                 attr->ftp_attr,
 
1935
                 cksm,
 
1936
                 offset,
 
1937
                 length,
 
1938
                 algorithm,
 
1939
                 globus_l_gass_copy_ftp_client_op_done_callback,
 
1940
                 &info);
 
1941
    if(result != GLOBUS_SUCCESS)
 
1942
    {
 
1943
        goto error;
 
1944
    }
 
1945
                 
 
1946
    globus_mutex_lock(&info.mutex);
 
1947
    while(info.callbacks_left > 0)
 
1948
    {
 
1949
        globus_cond_wait(&info.cond, &info.mutex);
 
1950
    }
 
1951
    globus_mutex_unlock(&info.mutex);
 
1952
 
 
1953
 
 
1954
    if(info.err)
 
1955
    {
 
1956
        result = globus_error_put(info.err);
 
1957
        info.err = GLOBUS_NULL;
 
1958
    }
 
1959
  
 
1960
    globus_cond_destroy(&info.cond);
 
1961
    globus_mutex_destroy(&info.mutex);
 
1962
    return GLOBUS_SUCCESS;
 
1963
error:
 
1964
    globus_cond_destroy(&info.cond);
 
1965
    globus_mutex_destroy(&info.mutex);
 
1966
 
 
1967
    return result;
 
1968
}
 
1969
 
 
1970
#define GASS_COPY_CKSM_BUFSIZE 1024*1024
 
1971
 
 
1972
static
 
1973
globus_result_t
 
1974
globus_l_gass_copy_cksm_file(
 
1975
    char *                              url,
 
1976
    char *                              cksm,
 
1977
    globus_off_t                        offset,
 
1978
    globus_off_t                        length,
 
1979
    const char *                        algorithm)
 
1980
{
 
1981
    char *      myname = "globus_l_gass_copy_cksm_file";
 
1982
 
 
1983
    globus_url_t                        parsed_url;
 
1984
    globus_result_t                     result;
 
1985
    int                                 rc;
 
1986
 
 
1987
    MD5_CTX                             mdctx;
 
1988
    char *                              md5ptr;
 
1989
    unsigned char                       md[MD5_DIGEST_LENGTH];
 
1990
    char                                md5sum[MD5_DIGEST_LENGTH * 2 + 1];
 
1991
    char                                buf[GASS_COPY_CKSM_BUFSIZE];
 
1992
 
 
1993
    int                                 i;
 
1994
    int                                 fd;
 
1995
    int                                 n;
 
1996
    globus_off_t                        count;
 
1997
    globus_off_t                        read_left;
 
1998
 
 
1999
    rc = globus_url_parse_loose(url, &parsed_url);
 
2000
    if(rc != 0)
 
2001
    {
 
2002
        result = globus_error_put(
 
2003
            globus_error_construct_string(
 
2004
                GLOBUS_GASS_COPY_MODULE,
 
2005
                GLOBUS_NULL,
 
2006
                "[%s]: error parsing url: "
 
2007
                "globus_url_parse returned %d",
 
2008
                myname,
 
2009
                url));
 
2010
        goto error_url;
 
2011
    }
 
2012
 
 
2013
    if(parsed_url.url_path == GLOBUS_NULL)
 
2014
    {
 
2015
        result = globus_error_put(
 
2016
            globus_error_construct_string(
 
2017
                GLOBUS_GASS_COPY_MODULE,
 
2018
                GLOBUS_NULL,
 
2019
                "[%s]: error parsing url: "
 
2020
                "url has no path",
 
2021
                myname));
 
2022
        goto error_fd;
 
2023
    }    
 
2024
       
 
2025
    if(length >= 0)
 
2026
    {
 
2027
        read_left = length;
 
2028
        count = (read_left > GASS_COPY_CKSM_BUFSIZE) ? 
 
2029
            GASS_COPY_CKSM_BUFSIZE : read_left;
 
2030
    }
 
2031
    else
 
2032
    {
 
2033
        count = GASS_COPY_CKSM_BUFSIZE;
 
2034
    }
 
2035
    
 
2036
    fd = open(parsed_url.url_path, O_RDONLY);        
 
2037
    if(fd < 0)
 
2038
    {
 
2039
        goto error_fd;
 
2040
    }
 
2041
 
 
2042
    if (lseek(fd, offset, SEEK_SET) == -1)
 
2043
    {
 
2044
        goto error_seek;
 
2045
    }
 
2046
 
 
2047
    MD5_Init(&mdctx);        
 
2048
 
 
2049
    while((n = read(fd, buf, count)) > 0)
 
2050
    {
 
2051
        if(length >= 0)
 
2052
        {
 
2053
            read_left -= n;
 
2054
            count = (read_left > GASS_COPY_CKSM_BUFSIZE) ? GASS_COPY_CKSM_BUFSIZE : read_left;
 
2055
        }
 
2056
 
 
2057
        MD5_Update(&mdctx, buf, n);
 
2058
    }
 
2059
 
 
2060
    MD5_Final(md, &mdctx);
 
2061
    
 
2062
    close(fd);
 
2063
        
 
2064
    md5ptr = md5sum;
 
2065
    for(i = 0; i < MD5_DIGEST_LENGTH; i++)
 
2066
    {
 
2067
       sprintf(md5ptr, "%02x", md[i]);
 
2068
       md5ptr++;
 
2069
       md5ptr++;
 
2070
    }
 
2071
    md5ptr = '\0';
 
2072
    
 
2073
    strncpy(cksm, md5sum, sizeof(md5sum));
 
2074
    
 
2075
    globus_url_destroy(&parsed_url);
 
2076
 
 
2077
    return GLOBUS_SUCCESS;
 
2078
 
 
2079
error_seek:
 
2080
    close(fd);
 
2081
error_fd:
 
2082
    globus_url_destroy(&parsed_url);
 
2083
 
 
2084
error_url:
 
2085
 
 
2086
    return result;
 
2087
}
 
2088
 
 
2089
/* Determine the type of the url and call checksum functions above accordingly
 
2090
 * */
 
2091
 
 
2092
globus_result_t
 
2093
globus_gass_copy_cksm(
 
2094
    globus_gass_copy_handle_t *         handle,
 
2095
    char *                              url,
 
2096
    globus_gass_copy_attr_t *           attr,
 
2097
    globus_off_t                        offset,
 
2098
    globus_off_t                        length,
 
2099
    const char *                        algorithm,
 
2100
    char *                              cksm)
 
2101
{
 
2102
    static char *   myname = "globus_gass_copy_cksm";    
 
2103
 
 
2104
    globus_result_t                     result;
 
2105
    globus_gass_copy_url_mode_t         url_mode;
 
2106
    
 
2107
    result = globus_gass_copy_get_url_mode(url, &url_mode);
 
2108
 
 
2109
    if(result != GLOBUS_SUCCESS)
 
2110
    {
 
2111
        goto error_exit;
 
2112
    }        
 
2113
 
 
2114
    if(url_mode == GLOBUS_GASS_COPY_URL_MODE_FTP)
 
2115
    {
 
2116
        result = globus_l_gass_copy_cksm_ftp(
 
2117
                     handle,
 
2118
                     url,
 
2119
                     attr,
 
2120
                     cksm,
 
2121
                     offset,
 
2122
                     length,
 
2123
                     algorithm);
 
2124
    
 
2125
        if(result != GLOBUS_SUCCESS)
 
2126
        {
 
2127
            goto error_ftp_cksm;
 
2128
        }
 
2129
    }
 
2130
    else if(url_mode == GLOBUS_GASS_COPY_URL_MODE_IO)
 
2131
    {
 
2132
        result = globus_l_gass_copy_cksm_file(
 
2133
                     url,
 
2134
                     cksm, 
 
2135
                     offset,
 
2136
                     length,
 
2137
                     algorithm);
 
2138
    if(result != GLOBUS_SUCCESS)
 
2139
    {
 
2140
            goto error_file_cksm;
 
2141
        }
 
2142
    }
 
2143
    else
 
2144
    {
 
2145
        result = globus_error_put(
 
2146
            globus_error_construct_string(
 
2147
                GLOBUS_GASS_COPY_MODULE,
 
2148
                GLOBUS_NULL,
 
2149
                "[%s]: unsupported URL scheme: %s",
 
2150
                myname,
 
2151
                url));
 
2152
        goto error_exit;
 
2153
    }
 
2154
 
 
2155
    return GLOBUS_SUCCESS;
 
2156
 
 
2157
error_ftp_cksm:
 
2158
error_file_cksm:
 
2159
error_exit:
 
2160
        
 
2161
    return result;
 
2162
        
 
2163
}
 
2164