1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
|
/*@-modfilesys@*/
/** \ingroup rpmio
* \file rpmio/rpmdav.c
*/
#include "system.h"
#if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
#include <pthread.h>
#endif
#include <neon/ne_alloc.h>
#include <neon/ne_auth.h>
#include <neon/ne_basic.h>
#include <neon/ne_dates.h>
#include <neon/ne_locks.h>
#define NEONBLOWSCHUNKS
#ifndef NEONBLOWSCHUNKS
/* HACK: include ne_private.h to access sess->socket for now. */
#include "../neon/src/ne_private.h"
#endif
#include <neon/ne_props.h>
#include <neon/ne_request.h>
#include <neon/ne_socket.h>
#include <neon/ne_string.h>
#include <neon/ne_utils.h>
#include <rpmio_internal.h>
#define _RPMDAV_INTERNAL
#include <rpmdav.h>
#include "argv.h"
#include "ugid.h"
#include "debug.h"
/*@access DIR @*/
/*@access FD_t @*/
/*@access urlinfo @*/
#if 0 /* HACK: reasonable value needed. */
#define TIMEOUT_SECS 60
#else
#define TIMEOUT_SECS 5
#endif
/*@unchecked@*/
static int httpTimeoutSecs = TIMEOUT_SECS;
/**
* Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
* @param p memory to free
* @retval NULL always
*/
/*@unused@*/ static inline /*@null@*/ void *
_free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p)
/*@modifies p@*/
{
if (p != NULL) free((void *)p);
return NULL;
}
/* =============================================================== */
static int davFree(urlinfo u)
/*@globals internalState @*/
/*@modifies u, internalState @*/
{
if (u != NULL && u->sess != NULL) {
u->capabilities = _free(u->capabilities);
if (u->lockstore != NULL)
ne_lockstore_destroy(u->lockstore);
u->lockstore = NULL;
ne_session_destroy(u->sess);
u->sess = NULL;
}
return 0;
}
static void davProgress(void * userdata, off_t current, off_t total)
/*@*/
{
urlinfo u = userdata;
ne_session * sess;
assert(u != NULL);
sess = u->sess;
assert(sess != NULL);
assert(u == ne_get_session_private(sess, "urlinfo"));
u->current = current;
u->total = total;
if (_dav_debug < 0)
fprintf(stderr, "*** davProgress(%p,0x%x:0x%x) sess %p u %p\n", userdata, (unsigned int)current, (unsigned int)total, sess, u);
}
static void davNotify(void * userdata,
ne_conn_status connstatus, const char * info)
/*@*/
{
urlinfo u = userdata;
ne_session * sess;
/*@observer@*/
static const char * connstates[] = {
"namelookup",
"connecting",
"connected",
"secure",
"unknown"
};
assert(u != NULL);
sess = u->sess;
assert(sess != NULL);
assert(u == ne_get_session_private(sess, "urlinfo"));
#ifdef REFERENCE
typedef enum {
ne_conn_namelookup, /* lookup up hostname (info = hostname) */
ne_conn_connecting, /* connecting to host (info = hostname) */
ne_conn_connected, /* connected to host (info = hostname) */
ne_conn_secure /* connection now secure (info = crypto level) */
} ne_conn_status;
#endif
u->connstatus = connstatus;
/*@-boundsread@*/
if (_dav_debug < 0)
fprintf(stderr, "*** davNotify(%p,%d,%p) sess %p u %p %s\n", userdata, connstatus, info, sess, u, connstates[ (connstatus < 4 ? connstatus : 4)]);
/*@=boundsread@*/
}
static void davCreateRequest(ne_request * req, void * userdata,
const char * method, const char * uri)
/*@*/
{
urlinfo u = userdata;
ne_session * sess;
void * private = NULL;
const char * id = "urlinfo";
assert(u != NULL);
assert(u->sess != NULL);
assert(req != NULL);
sess = ne_get_session(req);
assert(sess == u->sess);
assert(u == ne_get_session_private(sess, "urlinfo"));
assert(sess != NULL);
private = ne_get_session_private(sess, id);
assert(u == private);
if (_dav_debug < 0)
fprintf(stderr, "*** davCreateRequest(%p,%p,%s,%s) %s:%p\n", req, userdata, method, uri, id, private);
}
static void davPreSend(ne_request * req, void * userdata, ne_buffer * buf)
{
urlinfo u = userdata;
ne_session * sess;
const char * id = "fd";
FD_t fd = NULL;
assert(u != NULL);
assert(u->sess != NULL);
assert(req != NULL);
sess = ne_get_session(req);
assert(sess == u->sess);
assert(u == ne_get_session_private(sess, "urlinfo"));
fd = ne_get_request_private(req, id);
if (_dav_debug < 0)
fprintf(stderr, "*** davPreSend(%p,%p,%p) sess %p %s %p\n", req, userdata, buf, sess, id, fd);
if (_dav_debug)
fprintf(stderr, "-> %s\n", buf->data);
}
static int davPostSend(ne_request * req, void * userdata, const ne_status * status)
/*@*/
{
urlinfo u = userdata;
ne_session * sess;
const char * id = "fd";
FD_t fd = NULL;
assert(u != NULL);
assert(u->sess != NULL);
assert(req != NULL);
sess = ne_get_session(req);
assert(sess == u->sess);
assert(u == ne_get_session_private(sess, "urlinfo"));
fd = ne_get_request_private(req, id);
/*@-evalorder@*/
if (_dav_debug < 0)
fprintf(stderr, "*** davPostSend(%p,%p,%p) sess %p %s %p %s\n", req, userdata, status, sess, id, fd, ne_get_error(sess));
/*@=evalorder@*/
return NE_OK;
}
static void davDestroyRequest(ne_request * req, void * userdata)
/*@*/
{
urlinfo u = userdata;
ne_session * sess;
const char * id = "fd";
FD_t fd = NULL;
assert(u != NULL);
assert(u->sess != NULL);
assert(req != NULL);
sess = ne_get_session(req);
assert(sess == u->sess);
assert(u == ne_get_session_private(sess, "urlinfo"));
fd = ne_get_request_private(req, id);
if (_dav_debug < 0)
fprintf(stderr, "*** davDestroyRequest(%p,%p) sess %p %s %p\n", req, userdata, sess, id, fd);
}
static void davDestroySession(void * userdata)
/*@*/
{
urlinfo u = userdata;
ne_session * sess;
void * private = NULL;
const char * id = "urlinfo";
assert(u != NULL);
assert(u->sess != NULL);
sess = u->sess;
assert(u == ne_get_session_private(sess, "urlinfo"));
assert(sess != NULL);
private = ne_get_session_private(sess, id);
assert(u == private);
if (_dav_debug < 0)
fprintf(stderr, "*** davDestroySession(%p) sess %p %s %p\n", userdata, sess, id, private);
}
static int
davVerifyCert(void *userdata, int failures, const ne_ssl_certificate *cert)
/*@*/
{
const char *hostname = userdata;
if (_dav_debug < 0)
fprintf(stderr, "*** davVerifyCert(%p,%d,%p) %s\n", userdata, failures, cert, hostname);
return 0; /* HACK: trust all server certificates. */
}
static int davConnect(urlinfo u)
/*@globals internalState @*/
/*@modifies u, internalState @*/
{
const char * path = NULL;
int rc;
/* HACK: where should server capabilities be read? */
(void) urlPath(u->url, &path);
/* HACK: perhaps capture Allow: tag, look for PUT permitted. */
rc = ne_options(u->sess, path, u->capabilities);
switch (rc) {
case NE_OK:
break;
case NE_ERROR:
/* HACK: "301 Moved Permanently" on empty subdir. */
if (!strncmp("301 ", ne_get_error(u->sess), sizeof("301 ")-1))
break;
/*@fallthrough@*/
case NE_CONNECT:
case NE_LOOKUP:
default:
if (_dav_debug)
fprintf(stderr, "*** Connect to %s:%d failed(%d):\n\t%s\n",
u->host, u->port, rc, ne_get_error(u->sess));
break;
}
/* HACK: sensitive to error returns? */
u->httpVersion = (ne_version_pre_http11(u->sess) ? 0 : 1);
/* HACK: stupid error impedence matching. */
if (rc)
rc = FTPERR_FAILED_CONNECT;
return rc;
}
static int davInit(const char * url, urlinfo * uret)
/*@globals internalState @*/
/*@modifies *uret, internalState @*/
{
urlinfo u = NULL;
int rc = 0;
/*@-globs@*/ /* FIX: h_errno annoyance. */
if (urlSplit(url, &u))
return -1; /* XXX error returns needed. */
/*@=globs@*/
if ((u->urltype == URL_IS_HTTP || u->urltype == URL_IS_HTTPS)
&& u->url != NULL && u->sess == NULL)
{
ne_server_capabilities * capabilities;
/* HACK: oneshots should be done Somewhere Else Instead. */
/*@-noeffect@*/
rc = ((_dav_debug < 0) ? NE_DBG_HTTP : 0);
ne_debug_init(stderr, rc); /* XXX oneshot? */
/*@=noeffect@*/
rc = ne_sock_init(); /* XXX oneshot? */
u->capabilities = capabilities = xcalloc(1, sizeof(*capabilities));
u->sess = ne_session_create(u->scheme, u->host, u->port);
u->lockstore = ne_lockstore_create(); /* XXX oneshot? */
ne_lockstore_register(u->lockstore, u->sess);
if (u->proxyh != NULL)
ne_session_proxy(u->sess, u->proxyh, u->proxyp);
#if 0
{ const ne_inet_addr ** addrs;
unsigned int n;
ne_set_addrlist(u->sess, addrs, n);
}
#endif
ne_set_progress(u->sess, davProgress, u);
ne_set_status(u->sess, davNotify, u);
ne_set_persist(u->sess, 1);
ne_set_read_timeout(u->sess, httpTimeoutSecs);
ne_set_useragent(u->sess, PACKAGE "/" PACKAGE_VERSION);
/* XXX check that neon is ssl enabled. */
if (!strcasecmp(u->scheme, "https"))
ne_ssl_set_verify(u->sess, davVerifyCert, (char *)u->host);
ne_set_session_private(u->sess, "urlinfo", u);
ne_hook_destroy_session(u->sess, davDestroySession, u);
ne_hook_create_request(u->sess, davCreateRequest, u);
ne_hook_pre_send(u->sess, davPreSend, u);
ne_hook_post_send(u->sess, davPostSend, u);
ne_hook_destroy_request(u->sess, davDestroyRequest, u);
/* HACK: where should server capabilities be read? */
rc = davConnect(u);
if (rc)
goto exit;
}
exit:
/*@-boundswrite@*/
if (rc == 0 && uret != NULL)
*uret = urlLink(u, __FUNCTION__);
/*@=boundswrite@*/
u = urlFree(u, "urlSplit (davInit)");
return rc;
}
/* =============================================================== */
enum fetch_rtype_e {
resr_normal = 0,
resr_collection,
resr_reference,
resr_error
};
struct fetch_resource_s {
struct fetch_resource_s *next;
char *uri;
/*@unused@*/
char *displayname;
enum fetch_rtype_e type;
size_t size;
time_t modtime;
int is_executable;
int is_vcr; /* Is version resource. 0: no vcr, 1 checkin 2 checkout */
char *error_reason; /* error string returned for this resource */
int error_status; /* error status returned for this resource */
};
/*@null@*/
static void *fetch_destroy_item(/*@only@*/ struct fetch_resource_s *res)
/*@modifies res @*/
{
NE_FREE(res->uri);
NE_FREE(res->error_reason);
res = _free(res);
return NULL;
}
/*@null@*/
static void *fetch_destroy_list(/*@only@*/ struct fetch_resource_s *res)
/*@modifies res @*/
{
struct fetch_resource_s *next;
/*@-branchstate@*/
for (; res != NULL; res = next) {
next = res->next;
res = fetch_destroy_item(res);
}
/*@=branchstate@*/
return NULL;
}
static void *fetch_create_item(/*@unused@*/ void *userdata, /*@unused@*/ const char *uri)
/*@*/
{
struct fetch_resource_s * res = ne_calloc(sizeof(*res));
return res;
}
/* =============================================================== */
struct fetch_context_s {
/*@relnull@*/
struct fetch_resource_s **resrock;
const char *uri;
unsigned int include_target; /* Include resource at href */
/*@refcounted@*/
urlinfo u;
int ac;
int nalloced;
ARGV_t av;
mode_t * modes;
size_t * sizes;
time_t * mtimes;
};
/*@null@*/
static void *fetch_destroy_context(/*@only@*/ /*@null@*/ struct fetch_context_s *ctx)
/*@globals internalState @*/
/*@modifies ctx, internalState @*/
{
if (ctx == NULL)
return NULL;
if (ctx->av != NULL)
ctx->av = argvFree(ctx->av);
ctx->modes = _free(ctx->modes);
ctx->sizes = _free(ctx->sizes);
ctx->mtimes = _free(ctx->mtimes);
ctx->u = urlFree(ctx->u, __FUNCTION__);
ctx->uri = _free(ctx->uri);
/*@-boundswrite@*/
memset(ctx, 0, sizeof(*ctx));
/*@=boundswrite@*/
ctx = _free(ctx);
return NULL;
}
/*@null@*/
static void *fetch_create_context(const char *uri)
/*@globals internalState @*/
/*@modifies internalState @*/
{
struct fetch_context_s * ctx;
urlinfo u;
/*@-globs@*/ /* FIX: h_errno annoyance. */
if (urlSplit(uri, &u))
return NULL;
/*@=globs@*/
ctx = ne_calloc(sizeof(*ctx));
ctx->uri = xstrdup(uri);
ctx->u = urlLink(u, __FUNCTION__);
return ctx;
}
/*@unchecked@*/ /*@observer@*/
static const ne_propname fetch_props[] = {
{ "DAV:", "getcontentlength" },
{ "DAV:", "getlastmodified" },
{ "http://apache.org/dav/props/", "executable" },
{ "DAV:", "resourcetype" },
{ "DAV:", "checked-in" },
{ "DAV:", "checked-out" },
{ NULL, NULL }
};
#define ELM_resourcetype (NE_PROPS_STATE_TOP + 1)
#define ELM_collection (NE_PROPS_STATE_TOP + 2)
/*@unchecked@*/ /*@observer@*/
static const struct ne_xml_idmap fetch_idmap[] = {
{ "DAV:", "resourcetype", ELM_resourcetype },
{ "DAV:", "collection", ELM_collection }
};
static int fetch_startelm(void *userdata, int parent,
const char *nspace, const char *name,
/*@unused@*/ const char **atts)
/*@*/
{
ne_propfind_handler *pfh = userdata;
struct fetch_resource_s *r = ne_propfind_current_private(pfh);
int state = ne_xml_mapid(fetch_idmap, NE_XML_MAPLEN(fetch_idmap),
nspace, name);
if (r == NULL ||
!((parent == NE_207_STATE_PROP && state == ELM_resourcetype) ||
(parent == ELM_resourcetype && state == ELM_collection)))
return NE_XML_DECLINE;
if (state == ELM_collection) {
r->type = resr_collection;
}
return state;
}
static int fetch_compare(const struct fetch_resource_s *r1,
const struct fetch_resource_s *r2)
/*@*/
{
/* Sort errors first, then collections, then alphabetically */
if (r1->type == resr_error) {
return -1;
} else if (r2->type == resr_error) {
return 1;
} else if (r1->type == resr_collection) {
if (r2->type != resr_collection) {
return -1;
} else {
return strcmp(r1->uri, r2->uri);
}
} else {
if (r2->type != resr_collection) {
return strcmp(r1->uri, r2->uri);
} else {
return 1;
}
}
}
static void fetch_results(void *userdata, const char *uri,
const ne_prop_result_set *set)
/*@*/
{
struct fetch_context_s *ctx = userdata;
struct fetch_resource_s *current, *previous, *newres;
const char *clength, *modtime, *isexec;
const char *checkin, *checkout;
const ne_status *status = NULL;
const char * path = NULL;
(void) urlPath(uri, &path);
if (path == NULL)
return;
newres = ne_propset_private(set);
if (_dav_debug < 0)
fprintf(stderr, "==> %s in uri %s\n", path, ctx->uri);
if (ne_path_compare(ctx->uri, path) == 0 && !ctx->include_target) {
/* This is the target URI */
if (_dav_debug < 0)
fprintf(stderr, "==> %s skipping target resource.\n", path);
/* Free the private structure. */
free(newres);
return;
}
newres->uri = ne_strdup(path);
/*@-boundsread@*/
clength = ne_propset_value(set, &fetch_props[0]);
modtime = ne_propset_value(set, &fetch_props[1]);
isexec = ne_propset_value(set, &fetch_props[2]);
checkin = ne_propset_value(set, &fetch_props[4]);
checkout = ne_propset_value(set, &fetch_props[5]);
/*@=boundsread@*/
if (clength == NULL)
status = ne_propset_status(set, &fetch_props[0]);
if (modtime == NULL)
status = ne_propset_status(set, &fetch_props[1]);
if (newres->type == resr_normal && status != NULL) {
/* It's an error! */
newres->error_status = status->code;
/* Special hack for Apache 1.3/mod_dav */
if (strcmp(status->reason_phrase, "status text goes here") == 0) {
const char *desc;
if (status->code == 401) {
desc = _("Authorization Required");
} else if (status->klass == 3) {
desc = _("Redirect");
} else if (status->klass == 5) {
desc = _("Server Error");
} else {
desc = _("Unknown Error");
}
newres->error_reason = ne_strdup(desc);
} else {
newres->error_reason = ne_strdup(status->reason_phrase);
}
newres->type = resr_error;
}
if (isexec && strcasecmp(isexec, "T") == 0) {
newres->is_executable = 1;
} else {
newres->is_executable = 0;
}
if (modtime)
newres->modtime = ne_httpdate_parse(modtime);
if (clength)
newres->size = atoi(clength);
/* is vcr */
if (checkin) {
newres->is_vcr = 1;
} else if (checkout) {
newres->is_vcr = 2;
} else {
newres->is_vcr = 0;
}
for (current = *ctx->resrock, previous = NULL; current != NULL;
previous = current, current = current->next)
{
if (fetch_compare(current, newres) >= 0) {
break;
}
}
if (previous) {
previous->next = newres;
} else {
/*@-boundswrite@*/
*ctx->resrock = newres;
/*@=boundswrite@*/
}
newres->next = current;
}
static int davFetch(const urlinfo u, struct fetch_context_s * ctx)
/*@globals internalState @*/
/*@modifies ctx, internalState @*/
{
const char * path = NULL;
int depth = 1; /* XXX passed arg? */
unsigned int include_target = 0; /* XXX passed arg? */
struct fetch_resource_s * resitem = NULL;
struct fetch_resource_s ** resrock = &resitem; /* XXX passed arg? */
ne_propfind_handler *pfh;
struct fetch_resource_s *current, *next;
mode_t st_mode;
int rc = 0;
int xx;
(void) urlPath(u->url, &path);
pfh = ne_propfind_create(u->sess, ctx->uri, depth);
/* HACK: need to set u->httpHasRange here. */
ctx->resrock = resrock;
ctx->include_target = include_target;
ne_xml_push_handler(ne_propfind_get_parser(pfh),
fetch_startelm, NULL, NULL, pfh);
ne_propfind_set_private(pfh, fetch_create_item, NULL);
rc = ne_propfind_named(pfh, fetch_props, fetch_results, ctx);
ne_propfind_destroy(pfh);
for (current = resitem; current != NULL; current = next) {
const char *s, *se;
char * val;
next = current->next;
/* Collections have trailing '/' that needs trim. */
/* The top level collection is returned as well. */
se = current->uri + strlen(current->uri);
if (se[-1] == '/') {
if (strlen(current->uri) <= strlen(path)) {
current = fetch_destroy_item(current);
continue;
}
se--;
}
s = se;
while (s > current->uri && s[-1] != '/')
s--;
val = ne_strndup(s, (se - s));
/*@-nullpass@*/
val = ne_path_unescape(val);
/*@=nullpass@*/
xx = argvAdd(&ctx->av, val);
if (_dav_debug < 0)
fprintf(stderr, "*** argvAdd(%p,\"%s\")\n", &ctx->av, val);
NE_FREE(val);
while (ctx->ac >= ctx->nalloced) {
if (ctx->nalloced <= 0)
ctx->nalloced = 1;
ctx->nalloced *= 2;
ctx->modes = xrealloc(ctx->modes,
(sizeof(*ctx->modes) * ctx->nalloced));
ctx->sizes = xrealloc(ctx->sizes,
(sizeof(*ctx->sizes) * ctx->nalloced));
ctx->mtimes = xrealloc(ctx->mtimes,
(sizeof(*ctx->mtimes) * ctx->nalloced));
}
switch (current->type) {
case resr_normal:
st_mode = S_IFREG;
/*@switchbreak@*/ break;
case resr_collection:
st_mode = S_IFDIR;
/*@switchbreak@*/ break;
case resr_reference:
case resr_error:
default:
st_mode = 0;
/*@switchbreak@*/ break;
}
/*@-boundswrite@*/
ctx->modes[ctx->ac] = st_mode;
ctx->sizes[ctx->ac] = current->size;
ctx->mtimes[ctx->ac] = current->modtime;
/*@=boundswrite@*/
ctx->ac++;
current = fetch_destroy_item(current);
}
ctx->resrock = NULL; /* HACK: avoid leaving stack reference. */
return rc;
}
static int davNLST(struct fetch_context_s * ctx)
/*@globals internalState @*/
/*@modifies ctx, internalState @*/
{
urlinfo u = NULL;
int rc;
int xx;
rc = davInit(ctx->uri, &u);
if (rc || u == NULL)
goto exit;
rc = davFetch(u, ctx);
switch (rc) {
case NE_OK:
break;
case NE_ERROR:
/* HACK: "301 Moved Permanently" on empty subdir. */
if (!strncmp("301 ", ne_get_error(u->sess), sizeof("301 ")-1))
break;
/*@fallthrough@*/
default:
if (_dav_debug)
fprintf(stderr, "*** Fetch from %s:%d failed:\n\t%s\n",
u->host, u->port, ne_get_error(u->sess));
break;
}
exit:
if (rc)
xx = davFree(u);
return rc;
}
/* =============================================================== */
/*@observer@*/
static const char * my_retstrerror(int ret)
/*@*/
{
const char * str = "NE_UNKNOWN";
switch (ret) {
case NE_OK: str = "NE_OK: Succeeded."; break;
case NE_ERROR: str = "NE_ERROR: Generic error."; break;
case NE_LOOKUP: str = "NE_LOOKUP: Hostname lookup failed."; break;
case NE_AUTH: str = "NE_AUTH: Server authentication failed."; break;
case NE_PROXYAUTH: str = "NE_PROXYAUTH: Proxy authentication failed.";break;
case NE_CONNECT: str = "NE_CONNECT: Could not connect to server.";break;
case NE_TIMEOUT: str = "NE_TIMEOUT: Connection timed out."; break;
case NE_FAILED: str = "NE_FAILED: The precondition failed."; break;
case NE_RETRY: str = "NE_RETRY: Retry request."; break;
case NE_REDIRECT: str = "NE_REDIRECT: Redirect received."; break;
default: str = "NE_UNKNOWN"; break;
}
return str;
}
static int my_result(const char * msg, int ret, /*@null@*/ FILE * fp)
/*@modifies *fp @*/
{
/* HACK: don't print unless debugging. */
if (_dav_debug >= 0)
return ret;
if (fp == NULL)
fp = stderr;
if (msg != NULL)
fprintf(fp, "*** %s: ", msg);
#ifdef HACK
fprintf(fp, "%s: %s\n", my_retstrerror(ret), ne_get_error(sess));
#else
fprintf(fp, "%s\n", my_retstrerror(ret));
#endif
return ret;
}
#ifndef DYING
static void hexdump(const unsigned char * buf, ssize_t len)
/*@*/
{
int i;
if (len <= 0)
return;
for (i = 0; i < len; i++) {
if (i != 0 && (i%16) == 0)
fprintf(stderr, "\n");
fprintf(stderr, " %02X", buf[i]);
}
fprintf(stderr, "\n");
}
#endif
static void davAcceptRanges(void * userdata, const char * value)
{
urlinfo u = userdata;
if (_dav_debug < 0)
fprintf(stderr, "*** u %p Accept-Ranges: %s\n", u, value);
if (!strcmp(value, "bytes"))
u->httpHasRange = 1;
if (!strcmp(value, "none"))
u->httpHasRange = 0;
}
static void davAllHeaders(void * userdata, const char * value)
{
FD_t ctrl = userdata;
if (_dav_debug)
fprintf(stderr, "<- %s\n", value);
}
static void davContentLength(void * userdata, const char * value)
{
FD_t ctrl = userdata;
if (_dav_debug < 0)
fprintf(stderr, "*** fd %p Content-Length: %s\n", ctrl, value);
/*@-unrecog@*/
ctrl->contentLength = strtoll(value, NULL, 10);
/*@=unrecog@*/
}
static void davConnection(void * userdata, const char * value)
{
FD_t ctrl = userdata;
if (_dav_debug < 0)
fprintf(stderr, "*** fd %p Connection: %s\n", ctrl, value);
if (!strcasecmp(value, "close"))
ctrl->persist = 0;
else if (!strcasecmp(value, "Keep-Alive"))
ctrl->persist = 1;
}
/*@-mustmod@*/ /* HACK: stash error in *str. */
int davResp(urlinfo u, FD_t ctrl, /*@unused@*/ char *const * str)
{
int rc = 0;
rc = ne_begin_request(ctrl->req);
rc = my_result("ne_begin_req(ctrl->req)", rc, NULL);
if (_dav_debug < 0)
fprintf(stderr, "*** davResp(%p,%p,%p) sess %p req %p rc %d\n", u, ctrl, str, u->sess, ctrl->req, rc);
/* HACK: stupid error impedence matching. */
/* HACK: NE_TIMEOUT et al here does not unravel refcnt correctly. */
switch (rc) {
case NE_OK: rc = 0; break;
case NE_ERROR: rc = FTPERR_SERVER_IO_ERROR; break;
case NE_LOOKUP: rc = FTPERR_BAD_HOSTNAME; break;
case NE_AUTH: rc = FTPERR_FAILED_CONNECT; break;
case NE_PROXYAUTH: rc = FTPERR_FAILED_CONNECT; break;
case NE_CONNECT: rc = FTPERR_FAILED_CONNECT; break;
case NE_TIMEOUT: rc = FTPERR_SERVER_TIMEOUT; break;
case NE_FAILED: rc = FTPERR_SERVER_IO_ERROR; break;
case NE_RETRY: /* HACK: davReq handles. */ break;
case NE_REDIRECT: rc = FTPERR_BAD_SERVER_RESPONSE;break;
default: rc = FTPERR_UNKNOWN; break;
}
/*@-observertrans@*/
if (rc)
fdSetSyserrno(ctrl, errno, ftpStrerror(rc));
/*@=observertrans@*/
return rc;
}
/*@=mustmod@*/
int davReq(FD_t ctrl, const char * httpCmd, const char * httpArg)
{
urlinfo u;
int rc = 0;
assert(ctrl != NULL);
u = ctrl->url;
URLSANE(u);
if (_dav_debug < 0)
fprintf(stderr, "*** davReq(%p,%s,\"%s\") entry sess %p req %p\n", ctrl, httpCmd, (httpArg ? httpArg : ""), u->sess, ctrl->req);
ctrl->persist = (u->httpVersion > 0 ? 1 : 0);
ctrl = fdLink(ctrl, "open ctrl (davReq)");
assert(u->sess != NULL);
assert(ctrl->req == NULL);
/*@-nullpass@*/
ctrl->req = ne_request_create(u->sess, httpCmd, httpArg);
/*@=nullpass@*/
assert(ctrl->req != NULL);
ne_set_request_private(ctrl->req, "fd", ctrl);
ne_add_response_header_catcher(ctrl->req, davAllHeaders, ctrl);
ne_add_response_header_handler(ctrl->req, "Content-Length",
davContentLength, ctrl);
ne_add_response_header_handler(ctrl->req, "Connection",
davConnection, ctrl);
if (!strcmp(httpCmd, "PUT")) {
ctrl->wr_chunked = 1;
ne_add_request_header(ctrl->req, "Transfer-Encoding", "chunked");
ne_set_request_chunked(ctrl->req, 1);
/* HACK: no retries if/when chunking. */
rc = davResp(u, ctrl, NULL);
} else {
/* HACK: possible Last-Modified: Tue, 02 Nov 2004 14:29:36 GMT */
/* HACK: possible ETag: "inode-size-mtime" */
ne_add_response_header_handler(ctrl->req, "Accept-Ranges",
davAcceptRanges, u);
/* HACK: possible Transfer-Encoding: on GET. */
/* HACK: other errors may need retry too. */
/* HACK: neon retries once, gud enuf. */
do {
rc = davResp(u, ctrl, NULL);
} while (rc == NE_RETRY);
}
if (rc)
goto errxit;
if (_dav_debug < 0)
fprintf(stderr, "*** davReq(%p,%s,\"%s\") exit sess %p req %p rc %d\n", ctrl, httpCmd, (httpArg ? httpArg : ""), u->sess, ctrl->req, rc);
ctrl = fdLink(ctrl, "open data (davReq)");
return 0;
errxit:
/*@-observertrans@*/
fdSetSyserrno(ctrl, errno, ftpStrerror(rc));
/*@=observertrans@*/
return rc;
}
FD_t davOpen(const char * url, /*@unused@*/ int flags,
/*@unused@*/ mode_t mode, /*@out@*/ urlinfo * uret)
{
const char * path = NULL;
urltype urlType = urlPath(url, &path);
urlinfo u = NULL;
FD_t fd = NULL;
int rc;
#if 0 /* XXX makeTempFile() heartburn */
assert(!(flags & O_RDWR));
#endif
if (_dav_debug < 0)
fprintf(stderr, "*** davOpen(%s,0x%x,0%o,%p)\n", url, flags, mode, uret);
rc = davInit(url, &u);
if (rc || u == NULL || u->sess == NULL)
goto exit;
if (u->ctrl == NULL)
u->ctrl = fdNew("persist ctrl (davOpen)");
if (u->ctrl->nrefs > 2 && u->data == NULL)
u->data = fdNew("persist data (davOpen)");
if (u->ctrl->url == NULL)
fd = fdLink(u->ctrl, "grab ctrl (davOpen persist ctrl)");
else if (u->data->url == NULL)
fd = fdLink(u->data, "grab ctrl (davOpen persist data)");
else
fd = fdNew("grab ctrl (davOpen)");
if (fd) {
fdSetIo(fd, ufdio);
fd->ftpFileDoneNeeded = 0;
fd->rd_timeoutsecs = httpTimeoutSecs;
fd->contentLength = fd->bytesRemain = -1;
fd->url = urlLink(u, "url (davOpen)");
fd = fdLink(fd, "grab data (davOpen)");
assert(urlType == URL_IS_HTTPS || urlType == URL_IS_HTTP);
fd->urlType = urlType; /* URL_IS_HTTPS */
}
exit:
/*@-boundswrite@*/
if (uret)
*uret = u;
/*@=boundswrite@*/
/*@-refcounttrans@*/
return fd;
/*@=refcounttrans@*/
}
ssize_t davRead(void * cookie, /*@out@*/ char * buf, size_t count)
{
FD_t fd = cookie;
ssize_t rc;
#if 0
assert(count >= 128); /* HACK: see ne_request.h comment */
#endif
rc = ne_read_response_block(fd->req, buf, count);
if (_dav_debug < 0) {
fprintf(stderr, "*** davRead(%p,%p,0x%x) rc 0x%x\n", cookie, buf, count, (unsigned)rc);
#ifdef DYING
hexdump(buf, rc);
#endif
}
return rc;
}
ssize_t davWrite(void * cookie, const char * buf, size_t count)
{
FD_t fd = cookie;
ssize_t rc;
int xx;
#ifndef NEONBLOWSCHUNKS
ne_session * sess;
assert(fd->req != NULL);
sess = ne_get_session(fd->req);
assert(sess != NULL);
/* HACK: include ne_private.h to access sess->socket for now. */
xx = ne_sock_fullwrite(sess->socket, buf, count);
#else
assert(fd->req != NULL);
xx = ne_send_request_chunk(fd->req, buf, count);
#endif
/* HACK: stupid error impedence matching. */
rc = (xx == 0 ? count : -1);
if (_dav_debug < 0)
fprintf(stderr, "*** davWrite(%p,%p,0x%x) rc 0x%x\n", cookie, buf, count, rc);
if (count > 0)
hexdump(buf, count);
return rc;
}
int davSeek(void * cookie, /*@unused@*/ _libio_pos_t pos, int whence)
{
if (_dav_debug < 0)
fprintf(stderr, "*** davSeek(%p,pos,%d)\n", cookie, whence);
return -1;
}
/*@-mustmod@*/ /* HACK: fd->req is modified. */
int davClose(void * cookie)
{
/*@-onlytrans@*/
FD_t fd = cookie;
/*@=onlytrans@*/
int rc;
assert(fd->req != NULL);
rc = ne_end_request(fd->req);
rc = my_result("ne_end_request(req)", rc, NULL);
ne_request_destroy(fd->req);
fd->req = NULL;
if (_dav_debug < 0)
fprintf(stderr, "*** davClose(%p) rc %d\n", fd, rc);
return rc;
}
/*@=mustmod@*/
/* =============================================================== */
int davMkdir(const char * path, mode_t mode)
{
urlinfo u = NULL;
const char * src = NULL;
int rc;
rc = davInit(path, &u);
if (rc)
goto exit;
(void) urlPath(path, &src);
rc = ne_mkcol(u->sess, path);
if (rc) rc = -1; /* XXX HACK: errno impedance match */
/* XXX HACK: verify getrestype(remote) == resr_collection */
exit:
if (_dav_debug)
fprintf(stderr, "*** davMkdir(%s,0%o) rc %d\n", path, mode, rc);
return rc;
}
int davRmdir(const char * path)
{
urlinfo u = NULL;
const char * src = NULL;
int rc;
rc = davInit(path, &u);
if (rc)
goto exit;
(void) urlPath(path, &src);
/* XXX HACK: only getrestype(remote) == resr_collection */
rc = ne_delete(u->sess, path);
if (rc) rc = -1; /* XXX HACK: errno impedance match */
exit:
if (_dav_debug)
fprintf(stderr, "*** davRmdir(%s) rc %d\n", path, rc);
return rc;
}
int davRename(const char * oldpath, const char * newpath)
{
urlinfo u = NULL;
const char * src = NULL;
const char * dst = NULL;
int overwrite = 1; /* HACK: set this correctly. */
int rc;
rc = davInit(oldpath, &u);
if (rc)
goto exit;
(void) urlPath(oldpath, &src);
(void) urlPath(newpath, &dst);
/* XXX HACK: only getrestype(remote) != resr_collection */
rc = ne_move(u->sess, overwrite, src, dst);
if (rc) rc = -1; /* XXX HACK: errno impedance match */
exit:
if (_dav_debug)
fprintf(stderr, "*** davRename(%s,%s) rc %d\n", oldpath, newpath, rc);
return rc;
}
int davUnlink(const char * path)
{
urlinfo u = NULL;
const char * src = NULL;
int rc;
rc = davInit(path, &u);
if (rc)
goto exit;
(void) urlPath(path, &src);
/* XXX HACK: only getrestype(remote) != resr_collection */
rc = ne_delete(u->sess, src);
exit:
if (rc) rc = -1; /* XXX HACK: errno impedance match */
if (_dav_debug)
fprintf(stderr, "*** davUnlink(%s) rc %d\n", path, rc);
return rc;
}
#ifdef NOTYET
static int davChdir(const char * path)
/*@globals h_errno, fileSystem, internalState @*/
/*@modifies fileSystem, internalState @*/
{
return davCommand("CWD", path, NULL);
}
#endif /* NOTYET */
/* =============================================================== */
static const char * statstr(const struct stat * st,
/*@returned@*/ /*@out@*/ char * buf)
/*@modifies *buf @*/
{
sprintf(buf,
"*** dev %x ino %x mode %0o nlink %d uid %d gid %d rdev %x size %x\n",
(unsigned)st->st_dev,
(unsigned)st->st_ino,
st->st_mode,
(unsigned)st->st_nlink,
st->st_uid,
st->st_gid,
(unsigned)st->st_rdev,
(unsigned)st->st_size);
return buf;
}
/*@unchecked@*/
static int dav_st_ino = 0xdead0000;
/*@-boundswrite@*/
int davStat(const char * path, /*@out@*/ struct stat *st)
/*@globals dav_st_ino, fileSystem, internalState @*/
/*@modifies *st, dav_st_ino, fileSystem, internalState @*/
{
struct fetch_context_s * ctx = NULL;
char buf[1024];
int rc = -1;
/* HACK: neon really wants collections with trailing '/' */
ctx = fetch_create_context(path);
if (ctx == NULL) {
/* HACK: errno = ??? */
goto exit;
}
rc = davNLST(ctx);
if (rc) {
/* HACK: errno = ??? */
goto exit;
}
memset(st, 0, sizeof(*st));
st->st_mode = ctx->modes[0];
st->st_size = ctx->sizes[0];
st->st_mtime = ctx->mtimes[0];
if (S_ISDIR(st->st_mode)) {
st->st_nlink = 2;
st->st_mode |= 0755;
} else
if (S_ISREG(st->st_mode)) {
st->st_nlink = 1;
st->st_mode |= 0644;
}
/* XXX fts(3) needs/uses st_ino, make something up for now. */
if (st->st_ino == 0)
st->st_ino = dav_st_ino++;
if (_dav_debug < 0)
fprintf(stderr, "*** davStat(%s) rc %d\n%s", path, rc, statstr(st, buf));
exit:
ctx = fetch_destroy_context(ctx);
return rc;
}
/*@=boundswrite@*/
/*@-boundswrite@*/
int davLstat(const char * path, /*@out@*/ struct stat *st)
/*@globals dav_st_ino, fileSystem, internalState @*/
/*@modifies *st, dav_st_ino, fileSystem, internalState @*/
{
struct fetch_context_s * ctx = NULL;
char buf[1024];
int rc = -1;
/* HACK: neon really wants collections with trailing '/' */
ctx = fetch_create_context(path);
if (ctx == NULL) {
/* HACK: errno = ??? */
goto exit;
}
rc = davNLST(ctx);
if (rc) {
/* HACK: errno = ??? */
goto exit;
}
memset(st, 0, sizeof(*st));
st->st_mode = ctx->modes[0];
st->st_size = ctx->sizes[0];
st->st_mtime = ctx->mtimes[0];
if (S_ISDIR(st->st_mode)) {
st->st_nlink = 2;
st->st_mode |= 0755;
} else
if (S_ISREG(st->st_mode)) {
st->st_nlink = 1;
st->st_mode |= 0644;
}
/* XXX fts(3) needs/uses st_ino, make something up for now. */
if (st->st_ino == 0)
st->st_ino = dav_st_ino++;
if (_dav_debug < 0)
fprintf(stderr, "*** davLstat(%s) rc %d\n%s\n", path, rc, statstr(st, buf));
exit:
ctx = fetch_destroy_context(ctx);
return rc;
}
/*@=boundswrite@*/
#ifdef NOTYET
static int davReadlink(const char * path, /*@out@*/ char * buf, size_t bufsiz)
/*@globals h_errno, fileSystem, internalState @*/
/*@modifies *buf, fileSystem, internalState @*/
{
int rc;
rc = davNLST(path, DO_FTP_READLINK, NULL, buf, bufsiz);
if (_dav_debug < 0)
fprintf(stderr, "*** davReadlink(%s) rc %d\n", path, rc);
return rc;
}
#endif /* NOTYET */
/* =============================================================== */
/*@unchecked@*/
int avmagicdir = 0x3607113;
int avClosedir(/*@only@*/ DIR * dir)
{
AVDIR avdir = (AVDIR)dir;
if (_av_debug)
fprintf(stderr, "*** avClosedir(%p)\n", avdir);
#if defined(HAVE_PTHREAD_H)
/*@-moduncon -noeffectuncon @*/
(void) pthread_mutex_destroy(&avdir->lock);
/*@=moduncon =noeffectuncon @*/
#endif
avdir = _free(avdir);
return 0;
}
struct dirent * avReaddir(DIR * dir)
{
AVDIR avdir = (AVDIR)dir;
struct dirent * dp;
const char ** av;
unsigned char * dt;
int ac;
int i;
if (avdir == NULL || !ISAVMAGIC(avdir) || avdir->data == NULL) {
/* XXX TODO: EBADF errno. */
return NULL;
}
dp = (struct dirent *) avdir->data;
av = (const char **) (dp + 1);
ac = avdir->size;
dt = (char *) (av + (ac + 1));
i = avdir->offset + 1;
/*@-boundsread@*/
if (i < 0 || i >= ac || av[i] == NULL)
return NULL;
/*@=boundsread@*/
avdir->offset = i;
/* XXX glob(3) uses REAL_DIR_ENTRY(dp) test on d_ino */
/*@-type@*/
dp->d_ino = i + 1; /* W2DO? */
dp->d_reclen = 0; /* W2DO? */
#if !defined(hpux) && !defined(sun)
dp->d_off = 0; /* W2DO? */
/*@-boundsread@*/
dp->d_type = dt[i];
/*@=boundsread@*/
#endif
/*@=type@*/
strncpy(dp->d_name, av[i], sizeof(dp->d_name));
if (_av_debug)
fprintf(stderr, "*** avReaddir(%p) %p \"%s\"\n", (void *)avdir, dp, dp->d_name);
return dp;
}
/*@-boundswrite@*/
DIR * avOpendir(const char * path)
{
AVDIR avdir;
struct dirent * dp;
size_t nb = 0;
const char ** av;
unsigned char * dt;
char * t;
int ac;
if (_av_debug)
fprintf(stderr, "*** avOpendir(%s)\n", path);
nb = sizeof(".") + sizeof("..");
ac = 2;
nb += sizeof(*avdir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
avdir = xcalloc(1, nb);
/*@-abstract@*/
dp = (struct dirent *) (avdir + 1);
av = (const char **) (dp + 1);
dt = (char *) (av + (ac + 1));
t = (char *) (dt + ac + 1);
/*@=abstract@*/
avdir->fd = avmagicdir;
/*@-usereleased@*/
avdir->data = (char *) dp;
/*@=usereleased@*/
avdir->allocation = nb;
avdir->size = ac;
avdir->offset = -1;
avdir->filepos = 0;
#if defined(HAVE_PTHREAD_H)
/*@-moduncon -noeffectuncon -nullpass @*/
(void) pthread_mutex_init(&avdir->lock, NULL);
/*@=moduncon =noeffectuncon =nullpass @*/
#endif
ac = 0;
/*@-dependenttrans -unrecog@*/
dt[ac] = DT_DIR; av[ac++] = t; t = stpcpy(t, "."); t++;
dt[ac] = DT_DIR; av[ac++] = t; t = stpcpy(t, ".."); t++;
/*@=dependenttrans =unrecog@*/
av[ac] = NULL;
/*@-kepttrans@*/
return (DIR *) avdir;
/*@=kepttrans@*/
}
/*@=boundswrite@*/
/* =============================================================== */
/*@unchecked@*/
int davmagicdir = 0x8440291;
int davClosedir(/*@only@*/ DIR * dir)
{
DAVDIR avdir = (DAVDIR)dir;
if (_dav_debug < 0)
fprintf(stderr, "*** davClosedir(%p)\n", avdir);
#if defined(HAVE_PTHREAD_H)
/*@-moduncon -noeffectuncon @*/
(void) pthread_mutex_destroy(&avdir->lock);
/*@=moduncon =noeffectuncon @*/
#endif
avdir = _free(avdir);
return 0;
}
struct dirent * davReaddir(DIR * dir)
{
DAVDIR avdir = (DAVDIR)dir;
struct dirent * dp;
const char ** av;
unsigned char * dt;
int ac;
int i;
if (avdir == NULL || !ISDAVMAGIC(avdir) || avdir->data == NULL) {
/* XXX TODO: EBADF errno. */
return NULL;
}
dp = (struct dirent *) avdir->data;
av = (const char **) (dp + 1);
ac = avdir->size;
dt = (char *) (av + (ac + 1));
i = avdir->offset + 1;
/*@-boundsread@*/
if (i < 0 || i >= ac || av[i] == NULL)
return NULL;
/*@=boundsread@*/
avdir->offset = i;
/* XXX glob(3) uses REAL_DIR_ENTRY(dp) test on d_ino */
/*@-type@*/
dp->d_ino = i + 1; /* W2DO? */
dp->d_reclen = 0; /* W2DO? */
#if !defined(hpux) && !defined(sun)
dp->d_off = 0; /* W2DO? */
/*@-boundsread@*/
dp->d_type = dt[i];
/*@=boundsread@*/
#endif
/*@=type@*/
strncpy(dp->d_name, av[i], sizeof(dp->d_name));
if (_dav_debug < 0)
fprintf(stderr, "*** davReaddir(%p) %p \"%s\"\n", (void *)avdir, dp, dp->d_name);
return dp;
}
/*@-boundswrite@*/
DIR * davOpendir(const char * path)
{
struct fetch_context_s * ctx;
DAVDIR avdir;
struct dirent * dp;
size_t nb;
const char ** av, ** nav;
unsigned char * dt;
char * t;
int ac, nac;
int rc;
if (_dav_debug < 0)
fprintf(stderr, "*** davOpendir(%s)\n", path);
/* Load DAV collection into argv. */
ctx = fetch_create_context(path);
if (ctx == NULL) {
/* HACK: errno = ??? */
return NULL;
}
rc = davNLST(ctx);
if (rc) {
/* HACK: errno = ??? */
return NULL;
}
nb = 0;
ac = 0;
av = ctx->av;
if (av != NULL)
while (av[ac] != NULL)
nb += strlen(av[ac++]) + 1;
ac += 2; /* for "." and ".." */
nb += sizeof(".") + sizeof("..");
nb += sizeof(*avdir) + sizeof(*dp) + ((ac + 1) * sizeof(*av)) + (ac + 1);
avdir = xcalloc(1, nb);
/*@-abstract@*/
dp = (struct dirent *) (avdir + 1);
nav = (const char **) (dp + 1);
dt = (char *) (nav + (ac + 1));
t = (char *) (dt + ac + 1);
/*@=abstract@*/
avdir->fd = davmagicdir;
/*@-usereleased@*/
avdir->data = (char *) dp;
/*@=usereleased@*/
avdir->allocation = nb;
avdir->size = ac;
avdir->offset = -1;
avdir->filepos = 0;
#if defined(HAVE_PTHREAD_H)
/*@-moduncon -noeffectuncon -nullpass @*/
(void) pthread_mutex_init(&avdir->lock, NULL);
/*@=moduncon =noeffectuncon =nullpass @*/
#endif
nac = 0;
/*@-dependenttrans -unrecog@*/
dt[nac] = DT_DIR; nav[nac++] = t; t = stpcpy(t, "."); t++;
dt[nac] = DT_DIR; nav[nac++] = t; t = stpcpy(t, ".."); t++;
/*@=dependenttrans =unrecog@*/
/* Copy DAV items into DIR elments. */
ac = 0;
if (av != NULL)
while (av[ac] != NULL) {
nav[nac] = t;
dt[nac] = (S_ISDIR(ctx->modes[ac]) ? DT_DIR : DT_REG);
t = stpcpy(t, av[ac]);
ac++;
t++;
nac++;
}
nav[nac] = NULL;
ctx = fetch_destroy_context(ctx);
/*@-kepttrans@*/
return (DIR *) avdir;
/*@=kepttrans@*/
}
/*@=modfilesys@*/
|