summaryrefslogtreecommitdiff
path: root/gio/gfileinfo.c
blob: d2bab923a1710762a44596ab7e040bc708b82090 (plain)
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
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

/**
 * GFileInfo:
 *
 * Stores information about a file system object referenced by a [iface@Gio.File].
 *
 * Functionality for manipulating basic metadata for files. `GFileInfo`
 * implements methods for getting information that all files should
 * contain, and allows for manipulation of extended attributes.
 *
 * See [file-attributes.html](file attributes) for more information on how GIO
 * handles file attributes.
 *
 * To obtain a `GFileInfo` for a [iface@Gio.File], use
 * [method@Gio.File.query_info] (or its async variant). To obtain a `GFileInfo`
 * for a file input or output stream, use [method@Gio.FileInputStream.query_info]
 * or [method@Gio.FileOutputStream.query_info] (or their async variants).
 *
 * To change the actual attributes of a file, you should then set the
 * attribute in the `GFileInfo` and call [method@Gio.File.set_attributes_from_info]
 * or [method@Gio.File.set_attributes_async] on a `GFile`.
 *
 * However, not all attributes can be changed in the file. For instance,
 * the actual size of a file cannot be changed via [method@Gio.FileInfo.set_size].
 * You may call [method@Gio.File.query_settable_attributes] and
 * [method@Gio.File.query_writable_namespaces] to discover the settable attributes
 * of a particular file at runtime.
 *
 * The direct accessors, such as [method@Gio.FileInfo.get_name], are slightly more
 * optimized than the generic attribute accessors, such as
 * [method@Gio.FileInfo.get_attribute_byte_string].This optimization will matter
 * only if calling the API in a tight loop.
 *
 * It is an error to call these accessors without specifying their required file
 * attributes when creating the `GFileInfo`. Use
 * [method@Gio.FileInfo.has_attribute] or [method@Gio.FileInfo.list_attributes]
 * to check what attributes are specified for a `GFileInfo`.
 *
 * [struct@Gio.FileAttributeMatcher] allows for searching through a `GFileInfo`
 * for attributes.
 **/

#include "config.h"

#include <string.h>

#include "gfileinfo.h"
#include "gfileinfo-priv.h"
#include "gfileattribute-priv.h"
#include "gicon.h"
#include "glibintl.h"


/* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
#define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)

typedef struct  {
  guint32 attribute;
  GFileAttributeValue value;
} GFileAttribute;

struct _GFileInfo
{
  GObject parent_instance;

  GArray *attributes;
  GFileAttributeMatcher *mask;
};

struct _GFileInfoClass
{
  GObjectClass parent_class;
};


G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT)

typedef struct {
  guint32 id;
  guint32 attribute_id_counter;
} NSInfo;

G_LOCK_DEFINE_STATIC (attribute_hash);
static int namespace_id_counter = 0;
static GHashTable *ns_hash = NULL;
static GHashTable *attribute_hash = NULL;
static char ***global_attributes = NULL;

/* Attribute ids are 32bit, we split it up like this:
 * |------------|--------------------|
 *   12 bit          20 bit
 *   namespace      attribute id
 *
 * This way the attributes gets sorted in namespace order
 */

#define NS_POS 20
#define NS_MASK ((guint32)((1<<12) - 1))
#define ID_POS 0
#define ID_MASK ((guint32)((1<<20) - 1))

#define GET_NS(_attr_id) \
    (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
#define GET_ID(_attr_id) \
    (((guint32)(_attr_id) >> ID_POS) & ID_MASK)

#define MAKE_ATTR_ID(_ns, _id)				\
    ( ((((guint32) _ns) & NS_MASK) << NS_POS) |		\
      ((((guint32) _id) & ID_MASK) << ID_POS) )

static NSInfo *
_lookup_namespace (const char *namespace)
{
  NSInfo *ns_info;

  ns_info = g_hash_table_lookup (ns_hash, namespace);
  if (ns_info == NULL)
    {
      ns_info = g_new0 (NSInfo, 1);
      ns_info->id = ++namespace_id_counter;
      g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
      global_attributes = g_realloc (global_attributes, (ns_info->id + 1) * sizeof (char **));
      global_attributes[ns_info->id] = g_new (char *, 1);
      global_attributes[ns_info->id][0] = g_strconcat (namespace, "::*", NULL);
    }
  return ns_info;
}

static guint32
_lookup_attribute (const char *attribute)
{
  guint32 attr_id, id;
  char *ns;
  const char *colon;
  NSInfo *ns_info;

  attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));

  if (attr_id != 0)
    return attr_id;

  colon = strstr (attribute, "::");
  if (colon)
    ns = g_strndup (attribute, colon - attribute);
  else
    ns = g_strdup ("");

  ns_info = _lookup_namespace (ns);
  g_free (ns);

  id = ++ns_info->attribute_id_counter;
  global_attributes[ns_info->id] = g_realloc (global_attributes[ns_info->id], (id + 1) * sizeof (char *));
  global_attributes[ns_info->id][id] = g_strdup (attribute);

  attr_id = MAKE_ATTR_ID (ns_info->id, id);

  g_hash_table_insert (attribute_hash, global_attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));

  return attr_id;
}

static void
ensure_attribute_hash (void)
{
  if (attribute_hash != NULL)
    return;

  ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
  attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);

#define REGISTER_ATTRIBUTE(name) G_STMT_START{\
  guint _u G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */; \
  _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
  /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
  g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
}G_STMT_END

  REGISTER_ATTRIBUTE (STANDARD_TYPE);
  REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
  REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
  REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
  REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
  REGISTER_ATTRIBUTE (STANDARD_NAME);
  REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
  REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
  REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
  REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
  REGISTER_ATTRIBUTE (STANDARD_ICON);
  REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
  REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
  REGISTER_ATTRIBUTE (STANDARD_SIZE);
  REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
  REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
  REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
  REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
  REGISTER_ATTRIBUTE (STANDARD_SYMBOLIC_ICON);
  REGISTER_ATTRIBUTE (STANDARD_IS_VOLATILE);
  REGISTER_ATTRIBUTE (ETAG_VALUE);
  REGISTER_ATTRIBUTE (ID_FILE);
  REGISTER_ATTRIBUTE (ID_FILESYSTEM);
  REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
  REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
  REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
  REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
  REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
  REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
  REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
  REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
  REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
  REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
  REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
  REGISTER_ATTRIBUTE (TIME_MODIFIED);
  REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
  REGISTER_ATTRIBUTE (TIME_ACCESS);
  REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
  REGISTER_ATTRIBUTE (TIME_CHANGED);
  REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
  REGISTER_ATTRIBUTE (TIME_CREATED);
  REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
  REGISTER_ATTRIBUTE (TIME_MODIFIED_NSEC);
  REGISTER_ATTRIBUTE (TIME_ACCESS_NSEC);
  REGISTER_ATTRIBUTE (TIME_CREATED_NSEC);
  REGISTER_ATTRIBUTE (TIME_CHANGED_NSEC);
  REGISTER_ATTRIBUTE (UNIX_DEVICE);
  REGISTER_ATTRIBUTE (UNIX_INODE);
  REGISTER_ATTRIBUTE (UNIX_MODE);
  REGISTER_ATTRIBUTE (UNIX_NLINK);
  REGISTER_ATTRIBUTE (UNIX_UID);
  REGISTER_ATTRIBUTE (UNIX_GID);
  REGISTER_ATTRIBUTE (UNIX_RDEV);
  REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
  REGISTER_ATTRIBUTE (UNIX_BLOCKS);
  REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
  REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
  REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
  REGISTER_ATTRIBUTE (DOS_IS_MOUNTPOINT);
  REGISTER_ATTRIBUTE (DOS_REPARSE_POINT_TAG);
  REGISTER_ATTRIBUTE (OWNER_USER);
  REGISTER_ATTRIBUTE (OWNER_USER_REAL);
  REGISTER_ATTRIBUTE (OWNER_GROUP);
  REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
  REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
  REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID);
  REGISTER_ATTRIBUTE (THUMBNAIL_PATH_NORMAL);
  REGISTER_ATTRIBUTE (THUMBNAILING_FAILED_NORMAL);
  REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID_NORMAL);
  REGISTER_ATTRIBUTE (THUMBNAIL_PATH_LARGE);
  REGISTER_ATTRIBUTE (THUMBNAILING_FAILED_LARGE);
  REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID_LARGE);
  REGISTER_ATTRIBUTE (THUMBNAIL_PATH_XLARGE);
  REGISTER_ATTRIBUTE (THUMBNAILING_FAILED_XLARGE);
  REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID_XLARGE);
  REGISTER_ATTRIBUTE (THUMBNAIL_PATH_XXLARGE);
  REGISTER_ATTRIBUTE (THUMBNAILING_FAILED_XXLARGE);
  REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID_XXLARGE);
  REGISTER_ATTRIBUTE (PREVIEW_ICON);
  REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
  REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
  REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
  REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
  REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
  REGISTER_ATTRIBUTE (GVFS_BACKEND);
  REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
  REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
  REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
  REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);

#undef REGISTER_ATTRIBUTE
}

static guint32
lookup_namespace (const char *namespace)
{
  NSInfo *ns_info;
  guint32 id;

  G_LOCK (attribute_hash);

  ensure_attribute_hash ();

  ns_info = _lookup_namespace (namespace);
  id = 0;
  if (ns_info)
    id = ns_info->id;

  G_UNLOCK (attribute_hash);

  return id;
}

static char *
get_attribute_for_id (int attribute)
{
  char *s;
  G_LOCK (attribute_hash);
  s = global_attributes[GET_NS (attribute)][GET_ID (attribute)];
  G_UNLOCK (attribute_hash);
  return s;
}

static guint32
lookup_attribute (const char *attribute)
{
  guint32 attr_id;

  G_LOCK (attribute_hash);
  ensure_attribute_hash ();

  attr_id = _lookup_attribute (attribute);

  G_UNLOCK (attribute_hash);

  return attr_id;
}

static void
g_file_info_finalize (GObject *object)
{
  GFileInfo *info;
  guint i;
  GFileAttribute *attrs;

  info = G_FILE_INFO (object);

  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    _g_file_attribute_value_clear (&attrs[i].value);
  g_array_free (info->attributes, TRUE);

  if (info->mask != NO_ATTRIBUTE_MASK)
    g_file_attribute_matcher_unref (info->mask);

  G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
}

static void
g_file_info_class_init (GFileInfoClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_file_info_finalize;
}

static void
g_file_info_init (GFileInfo *info)
{
  info->mask = NO_ATTRIBUTE_MASK;
  info->attributes = g_array_new (FALSE, FALSE,
				  sizeof (GFileAttribute));
}

/**
 * g_file_info_new:
 *
 * Creates a new file info structure.
 *
 * Returns: a #GFileInfo.
 **/
GFileInfo *
g_file_info_new (void)
{
  return g_object_new (G_TYPE_FILE_INFO, NULL);
}

/**
 * g_file_info_copy_into:
 * @src_info: source to copy attributes from.
 * @dest_info: destination to copy attributes to.
 *
 * First clears all of the [GFileAttribute][gio-GFileAttribute] of @dest_info,
 * and then copies all of the file attributes from @src_info to @dest_info.
 **/
void
g_file_info_copy_into (GFileInfo *src_info,
                       GFileInfo *dest_info)
{
  GFileAttribute *source, *dest;
  guint i;

  g_return_if_fail (G_IS_FILE_INFO (src_info));
  g_return_if_fail (G_IS_FILE_INFO (dest_info));

  dest = (GFileAttribute *)dest_info->attributes->data;
  for (i = 0; i < dest_info->attributes->len; i++)
    _g_file_attribute_value_clear (&dest[i].value);

  g_array_set_size (dest_info->attributes,
		    src_info->attributes->len);

  source = (GFileAttribute *)src_info->attributes->data;
  dest = (GFileAttribute *)dest_info->attributes->data;

  for (i = 0; i < src_info->attributes->len; i++)
    {
      dest[i].attribute = source[i].attribute;
      dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
      _g_file_attribute_value_set (&dest[i].value, &source[i].value);
    }

  if (dest_info->mask != NO_ATTRIBUTE_MASK)
    g_file_attribute_matcher_unref (dest_info->mask);

  if (src_info->mask == NO_ATTRIBUTE_MASK)
    dest_info->mask = NO_ATTRIBUTE_MASK;
  else
    dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
}

/**
 * g_file_info_dup:
 * @other: a #GFileInfo.
 *
 * Duplicates a file info structure.
 *
 * Returns: (transfer full): a duplicate #GFileInfo of @other.
 **/
GFileInfo *
g_file_info_dup (GFileInfo *other)
{
  GFileInfo *new;

  g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);

  new = g_file_info_new ();
  g_file_info_copy_into (other, new);
  return new;
}

/**
 * g_file_info_set_attribute_mask:
 * @info: a #GFileInfo.
 * @mask: a #GFileAttributeMatcher.
 *
 * Sets @mask on @info to match specific attribute types.
 **/
void
g_file_info_set_attribute_mask (GFileInfo             *info,
				GFileAttributeMatcher *mask)
{
  GFileAttribute *attr;
  guint i;

  g_return_if_fail (G_IS_FILE_INFO (info));

  if (mask != info->mask)
    {
      if (info->mask != NO_ATTRIBUTE_MASK)
	g_file_attribute_matcher_unref (info->mask);
      info->mask = g_file_attribute_matcher_ref (mask);

      /* Remove non-matching attributes */
      for (i = 0; i < info->attributes->len; i++)
	{
	  attr = &g_array_index (info->attributes, GFileAttribute, i);
	  if (!_g_file_attribute_matcher_matches_id (mask,
						    attr->attribute))
	    {
	      _g_file_attribute_value_clear (&attr->value);
	      g_array_remove_index (info->attributes, i);
	      i--;
	    }
	}
    }
}

/**
 * g_file_info_unset_attribute_mask:
 * @info: #GFileInfo.
 *
 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
 * is set.
 **/
void
g_file_info_unset_attribute_mask (GFileInfo *info)
{
  g_return_if_fail (G_IS_FILE_INFO (info));

  if (info->mask != NO_ATTRIBUTE_MASK)
    g_file_attribute_matcher_unref (info->mask);
  info->mask = NO_ATTRIBUTE_MASK;
}

/**
 * g_file_info_clear_status:
 * @info: a #GFileInfo.
 *
 * Clears the status information from @info.
 **/
void
g_file_info_clear_status (GFileInfo  *info)
{
  GFileAttribute *attrs;
  guint i;

  g_return_if_fail (G_IS_FILE_INFO (info));

  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
}

static int
g_file_info_find_place (GFileInfo  *info,
			guint32     attribute)
{
  int min, max, med;
  GFileAttribute *attrs;
  /* Binary search for the place where attribute would be, if it's
     in the array */

  min = 0;
  max = info->attributes->len;

  attrs = (GFileAttribute *)info->attributes->data;

  while (min < max)
    {
      med = min + (max - min) / 2;
      if (attrs[med].attribute == attribute)
	{
	  min = med;
	  break;
	}
      else if (attrs[med].attribute < attribute)
	min = med + 1;
      else /* attrs[med].attribute > attribute */
	max = med;
    }

  return min;
}

static GFileAttributeValue *
g_file_info_find_value (GFileInfo *info,
			guint32    attr_id)
{
  GFileAttribute *attrs;
  guint i;

  i = g_file_info_find_place (info, attr_id);
  attrs = (GFileAttribute *)info->attributes->data;
  if (i < info->attributes->len &&
      attrs[i].attribute == attr_id)
    return &attrs[i].value;

  return NULL;
}

static GFileAttributeValue *
g_file_info_find_value_by_name (GFileInfo  *info,
				const char *attribute)
{
  guint32 attr_id;

  attr_id = lookup_attribute (attribute);
  return g_file_info_find_value (info, attr_id);
}

/**
 * g_file_info_has_attribute:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Checks if a file info structure has an attribute named @attribute.
 *
 * Returns: %TRUE if @info has an attribute named @attribute,
 *     %FALSE otherwise.
 **/
gboolean
g_file_info_has_attribute (GFileInfo  *info,
			   const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  value = g_file_info_find_value_by_name (info, attribute);
  return value != NULL;
}

/**
 * g_file_info_has_namespace:
 * @info: a #GFileInfo.
 * @name_space: a file attribute namespace.
 *
 * Checks if a file info structure has an attribute in the
 * specified @name_space.
 *
 * Returns: %TRUE if @info has an attribute in @name_space,
 *     %FALSE otherwise.
 *
 * Since: 2.22
 **/
gboolean
g_file_info_has_namespace (GFileInfo  *info,
			   const char *name_space)
{
  GFileAttribute *attrs;
  guint32 ns_id;
  guint i;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (name_space != NULL, FALSE);

  ns_id = lookup_namespace (name_space);

  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    {
      if (GET_NS (attrs[i].attribute) == ns_id)
	return TRUE;
    }

  return FALSE;
}

/**
 * g_file_info_list_attributes:
 * @info: a #GFileInfo.
 * @name_space: (nullable): a file attribute key's namespace, or %NULL to list
 *   all attributes.
 *
 * Lists the file info structure's attributes.
 *
 * Returns: (nullable) (array zero-terminated=1) (transfer full): a
 * null-terminated array of strings of all of the possible attribute
 * types for the given @name_space, or %NULL on error.
 **/
char **
g_file_info_list_attributes (GFileInfo  *info,
			     const char *name_space)
{
  GPtrArray *names;
  GFileAttribute *attrs;
  guint32 attribute;
  guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
  guint i;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  names = g_ptr_array_new ();
  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    {
      attribute = attrs[i].attribute;
      if (ns_id == 0 || GET_NS (attribute) == ns_id)
        g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
    }

  /* NULL terminate */
  g_ptr_array_add (names, NULL);

  return (char **)g_ptr_array_free (names, FALSE);
}

/**
 * g_file_info_get_attribute_type:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the attribute type for an attribute key.
 *
 * Returns: a #GFileAttributeType for the given @attribute, or
 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
 **/
GFileAttributeType
g_file_info_get_attribute_type (GFileInfo  *info,
				const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);

  value = g_file_info_find_value_by_name (info, attribute);
  if (value)
    return value->type;
  else
    return G_FILE_ATTRIBUTE_TYPE_INVALID;
}

static void
g_file_info_remove_value (GFileInfo *info,
			  guint32 attr_id)
{
  GFileAttribute *attrs;
  guint i;

  if (info->mask != NO_ATTRIBUTE_MASK &&
      !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
    return;

  i = g_file_info_find_place (info, attr_id);

  attrs = (GFileAttribute *)info->attributes->data;
  if (i < info->attributes->len &&
      attrs[i].attribute == attr_id)
    {
      _g_file_attribute_value_clear (&attrs[i].value);
      g_array_remove_index (info->attributes, i);
    }
}

/**
 * g_file_info_remove_attribute:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Removes all cases of @attribute from @info if it exists.
 **/
void
g_file_info_remove_attribute (GFileInfo  *info,
			      const char *attribute)
{
  guint32 attr_id;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  attr_id = lookup_attribute (attribute);

  g_file_info_remove_value (info, attr_id);
}

/**
 * g_file_info_get_attribute_data:
 * @info: a #GFileInfo
 * @attribute: a file attribute key
 * @type: (out) (optional): return location for the attribute type, or %NULL
 * @value_pp: (out) (optional) (not nullable): return location for the
 *    attribute value, or %NULL; the attribute value will not be %NULL
 * @status: (out) (optional): return location for the attribute status, or %NULL
 *
 * Gets the attribute type, value and status for an attribute key.
 *
 * Returns: %TRUE if @info has an attribute named @attribute,
 *      %FALSE otherwise.
 */
gboolean
g_file_info_get_attribute_data (GFileInfo            *info,
				const char           *attribute,
				GFileAttributeType   *type,
				gpointer             *value_pp,
				GFileAttributeStatus *status)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  value = g_file_info_find_value_by_name (info, attribute);
  if (value == NULL)
    return FALSE;

  if (status)
    *status = value->status;

  if (type)
    *type = value->type;

  if (value_pp)
    *value_pp = _g_file_attribute_value_peek_as_pointer (value);

  return TRUE;
}

/**
 * g_file_info_get_attribute_status:
 * @info: a #GFileInfo
 * @attribute: a file attribute key
 *
 * Gets the attribute status for an attribute key.
 *
 * Returns: a #GFileAttributeStatus for the given @attribute, or
 *    %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
 *
 */
GFileAttributeStatus
g_file_info_get_attribute_status (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *val;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  val = g_file_info_find_value_by_name (info, attribute);
  if (val)
    return val->status;

  return G_FILE_ATTRIBUTE_STATUS_UNSET;
}

/**
 * g_file_info_set_attribute_status:
 * @info: a #GFileInfo
 * @attribute: a file attribute key
 * @status: a #GFileAttributeStatus
 *
 * Sets the attribute status for an attribute key. This is only
 * needed by external code that implement g_file_set_attributes_from_info()
 * or similar functions.
 *
 * The attribute must exist in @info for this to work. Otherwise %FALSE
 * is returned and @info is unchanged.
 *
 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
 *
 * Since: 2.22
 */
gboolean
g_file_info_set_attribute_status (GFileInfo  *info,
				  const char *attribute,
				  GFileAttributeStatus status)
{
  GFileAttributeValue *val;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  val = g_file_info_find_value_by_name (info, attribute);
  if (val)
    {
      val->status = status;
      return TRUE;
    }

  return FALSE;
}

GFileAttributeValue *
_g_file_info_get_attribute_value (GFileInfo  *info,
				  const char *attribute)

{
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  return g_file_info_find_value_by_name (info, attribute);
}

/**
 * g_file_info_get_attribute_as_string:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of an attribute, formatted as a string.
 * This escapes things as needed to make the string valid
 * UTF-8.
 *
 * Returns: (nullable): a UTF-8 string associated with the given @attribute, or
 *    %NULL if the attribute wasn’t set.
 *    When you're done with the string it must be freed with g_free().
 **/
char *
g_file_info_get_attribute_as_string (GFileInfo  *info,
				     const char *attribute)
{
  GFileAttributeValue *val;
  val = _g_file_info_get_attribute_value (info, attribute);
  if (val)
    return _g_file_attribute_value_as_string (val);
  return NULL;
}


/**
 * g_file_info_get_attribute_object:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a #GObject attribute. If the attribute does
 * not contain a #GObject, %NULL will be returned.
 *
 * Returns: (transfer none) (nullable): a #GObject associated with the given @attribute,
 * or %NULL otherwise.
 **/
GObject *
g_file_info_get_attribute_object (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_object (value);
}

/**
 * g_file_info_get_attribute_string:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a string attribute. If the attribute does
 * not contain a string, %NULL will be returned.
 *
 * Returns: (nullable): the contents of the @attribute value as a UTF-8 string,
 * or %NULL otherwise.
 **/
const char *
g_file_info_get_attribute_string (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_string (value);
}

/**
 * g_file_info_get_attribute_byte_string:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a byte string attribute. If the attribute does
 * not contain a byte string, %NULL will be returned.
 *
 * Returns: (nullable): the contents of the @attribute value as a byte string, or
 * %NULL otherwise.
 **/
const char *
g_file_info_get_attribute_byte_string (GFileInfo  *info,
				       const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_byte_string (value);
}

/**
 * g_file_info_get_attribute_file_path:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a byte string attribute as a file path.
 *
 * If the attribute does not contain a byte string, `NULL` will be returned.
 *
 * This function is meant to be used by language bindings that have specific
 * handling for Unix paths.
 *
 * Returns: (type filename) (nullable): the contents of the @attribute value as
 * a file path, or %NULL otherwise.
 *
 * Since: 2.78
 **/
const char *
g_file_info_get_attribute_file_path (GFileInfo  *info,
                                     const char *attribute)
{
  return g_file_info_get_attribute_byte_string (info, attribute);
}

/**
 * g_file_info_get_attribute_stringv:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a stringv attribute. If the attribute does
 * not contain a stringv, %NULL will be returned.
 *
 * Returns: (transfer none) (nullable): the contents of the @attribute value as a stringv,
 * or %NULL otherwise. Do not free. These returned strings are UTF-8.
 *
 * Since: 2.22
 **/
char **
g_file_info_get_attribute_stringv (GFileInfo  *info,
				   const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_stringv (value);
}

/**
 * g_file_info_get_attribute_boolean:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a boolean attribute. If the attribute does not
 * contain a boolean value, %FALSE will be returned.
 *
 * Returns: the boolean value contained within the attribute.
 **/
gboolean
g_file_info_get_attribute_boolean (GFileInfo  *info,
				   const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_boolean (value);
}

/**
 * g_file_info_get_attribute_uint32:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets an unsigned 32-bit integer contained within the attribute. If the
 * attribute does not contain an unsigned 32-bit integer, or is invalid,
 * 0 will be returned.
 *
 * Returns: an unsigned 32-bit integer from the attribute.
 **/
guint32
g_file_info_get_attribute_uint32 (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_uint32 (value);
}

/**
 * g_file_info_get_attribute_int32:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets a signed 32-bit integer contained within the attribute. If the
 * attribute does not contain a signed 32-bit integer, or is invalid,
 * 0 will be returned.
 *
 * Returns: a signed 32-bit integer from the attribute.
 **/
gint32
g_file_info_get_attribute_int32 (GFileInfo  *info,
				 const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_int32 (value);
}

/**
 * g_file_info_get_attribute_uint64:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets a unsigned 64-bit integer contained within the attribute. If the
 * attribute does not contain an unsigned 64-bit integer, or is invalid,
 * 0 will be returned.
 *
 * Returns: a unsigned 64-bit integer from the attribute.
 **/
guint64
g_file_info_get_attribute_uint64 (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_uint64 (value);
}

/**
 * g_file_info_get_attribute_int64:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets a signed 64-bit integer contained within the attribute. If the
 * attribute does not contain a signed 64-bit integer, or is invalid,
 * 0 will be returned.
 *
 * Returns: a signed 64-bit integer from the attribute.
 **/
gint64
g_file_info_get_attribute_int64  (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_int64 (value);
}

static GFileAttributeValue *
g_file_info_create_value (GFileInfo *info,
			  guint32 attr_id)
{
  GFileAttribute *attrs;
  guint i;

  if (info->mask != NO_ATTRIBUTE_MASK &&
      !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
    return NULL;

  i = g_file_info_find_place (info, attr_id);

  attrs = (GFileAttribute *)info->attributes->data;
  if (i < info->attributes->len &&
      attrs[i].attribute == attr_id)
    return &attrs[i].value;
  else
    {
      GFileAttribute attr = { 0 };
      attr.attribute = attr_id;
      g_array_insert_val (info->attributes, i, attr);

      attrs = (GFileAttribute *)info->attributes->data;
      return &attrs[i].value;
    }
}

void
_g_file_info_set_attribute_by_id (GFileInfo                 *info,
                                  guint32                    attribute,
                                  GFileAttributeType         type,
                                  gpointer                   value_p)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);

  if (value)
    _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
}

/**
 * g_file_info_set_attribute:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @type: a #GFileAttributeType
 * @value_p: (not nullable): pointer to the value
 *
 * Sets the @attribute to contain the given value, if possible. To unset the
 * attribute, use %G_FILE_ATTRIBUTE_TYPE_INVALID for @type.
 **/
void
g_file_info_set_attribute (GFileInfo                 *info,
			   const char                *attribute,
			   GFileAttributeType         type,
			   gpointer                   value_p)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
}

void
_g_file_info_set_attribute_object_by_id (GFileInfo *info,
                                         guint32    attribute,
				         GObject   *attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_object (value, attr_value);
}

/**
 * g_file_info_set_attribute_object:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a #GObject.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_object (GFileInfo  *info,
				  const char *attribute,
				  GObject    *attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (G_IS_OBJECT (attr_value));

  _g_file_info_set_attribute_object_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
                                          guint32    attribute,
				          char     **attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_stringv (value, attr_value);
}

/**
 * g_file_info_set_attribute_stringv:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key
 * @attr_value: (array zero-terminated=1) (element-type utf8): a %NULL
 *   terminated array of UTF-8 strings.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 *
 * Sinze: 2.22
 **/
void
g_file_info_set_attribute_stringv (GFileInfo  *info,
				   const char *attribute,
				   char      **attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (attr_value != NULL);

  _g_file_info_set_attribute_stringv_by_id (info,
                                            lookup_attribute (attribute),
                                            attr_value);
}

void
_g_file_info_set_attribute_string_by_id (GFileInfo  *info,
                                         guint32     attribute,
				         const char *attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_string (value, attr_value);
}

/**
 * g_file_info_set_attribute_string:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a UTF-8 string.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_string (GFileInfo  *info,
				  const char *attribute,
				  const char *attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (attr_value != NULL);

  _g_file_info_set_attribute_string_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_byte_string_by_id (GFileInfo  *info,
                                              guint32     attribute,
				              const char *attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_byte_string (value, attr_value);
}

/**
 * g_file_info_set_attribute_byte_string:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a byte string.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_byte_string (GFileInfo  *info,
				       const char *attribute,
				       const char *attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (attr_value != NULL);

  _g_file_info_set_attribute_byte_string_by_id (info,
                                                lookup_attribute (attribute),
                                                attr_value);
}

/**
 * g_file_info_set_attribute_file_path:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: (type filename): a file path.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 *
 * This function is meant to be used by language bindings that have specific
 * handling for Unix paths.
 *
 * Since: 2.78
 **/
void
g_file_info_set_attribute_file_path (GFileInfo  *info,
                                     const char *attribute,
                                     const char *attr_value)
{
  g_file_info_set_attribute_byte_string (info, attribute, attr_value);
}

void
_g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
                                          guint32    attribute,
				          gboolean   attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_boolean (value, attr_value);
}

/**
 * g_file_info_set_attribute_boolean:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a boolean value.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_boolean (GFileInfo  *info,
				   const char *attribute,
				   gboolean    attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  _g_file_info_set_attribute_boolean_by_id (info,
                                            lookup_attribute (attribute),
                                            attr_value);
}

void
_g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
                                         guint32    attribute,
				         guint32    attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_uint32 (value, attr_value);
}

/**
 * g_file_info_set_attribute_uint32:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: an unsigned 32-bit integer.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_uint32 (GFileInfo  *info,
				  const char *attribute,
				  guint32     attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  _g_file_info_set_attribute_uint32_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_int32_by_id (GFileInfo *info,
                                        guint32    attribute,
				        gint32     attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_int32 (value, attr_value);
}

/**
 * g_file_info_set_attribute_int32:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a signed 32-bit integer
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_int32 (GFileInfo  *info,
                                 const char *attribute,
                                 gint32      attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  _g_file_info_set_attribute_int32_by_id (info,
                                          lookup_attribute (attribute),
                                          attr_value);
}

void
_g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
                                         guint32    attribute,
				         guint64    attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_uint64 (value, attr_value);
}

/**
 * g_file_info_set_attribute_uint64:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: an unsigned 64-bit integer.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 **/
void
g_file_info_set_attribute_uint64 (GFileInfo  *info,
				  const char *attribute,
				  guint64     attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  _g_file_info_set_attribute_uint64_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_int64_by_id (GFileInfo *info,
                                        guint32    attribute,
				        gint64     attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_int64 (value, attr_value);
}

/**
 * g_file_info_set_attribute_int64:
 * @info: a #GFileInfo.
 * @attribute: attribute name to set.
 * @attr_value: int64 value to set attribute to.
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 *
 **/
void
g_file_info_set_attribute_int64  (GFileInfo  *info,
				  const char *attribute,
				  gint64      attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  _g_file_info_set_attribute_int64_by_id (info,
                                          lookup_attribute (attribute),
                                          attr_value);
}

/* Helper getters */
#define get_required_attribute(value_ptr, info, attribute_name, error_value) \
  G_STMT_START { \
    static guint32 attr = 0; \
\
    if (attr == 0) \
      attr = lookup_attribute (attribute_name); \
\
    *value_ptr = g_file_info_find_value (info, attr); \
    if (G_UNLIKELY (*value_ptr == NULL)) \
      { \
        g_critical ("GFileInfo created without " attribute_name); \
        g_return_val_if_reached (error_value); \
      } \
  } G_STMT_END

/**
 * g_file_info_get_deletion_date:
 * @info: a #GFileInfo.
 *
 * Returns the #GDateTime representing the deletion date of the file, as
 * available in %G_FILE_ATTRIBUTE_TRASH_DELETION_DATE. If the
 * %G_FILE_ATTRIBUTE_TRASH_DELETION_DATE attribute is unset, %NULL is returned.
 *
 * Returns: (nullable): a #GDateTime, or %NULL.
 *
 * Since: 2.36
 **/
GDateTime *
g_file_info_get_deletion_date (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
  const char *date_str;
  GTimeZone *local_tz = NULL;
  GDateTime *dt = NULL;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_TRASH_DELETION_DATE);

  value = g_file_info_find_value (info, attr);
  date_str = _g_file_attribute_value_get_string (value);
  if (!date_str)
    return NULL;

  local_tz = g_time_zone_new_local ();
  dt = g_date_time_new_from_iso8601 (date_str, local_tz);
  g_time_zone_unref (local_tz);

  return g_steal_pointer (&dt);
}

/**
 * g_file_info_get_file_type:
 * @info: a #GFileInfo.
 *
 * Gets a file's type (whether it is a regular file, symlink, etc).
 * This is different from the file's content type, see g_file_info_get_content_type().
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_TYPE.
 *
 * Returns: a #GFileType for the given file.
 **/
GFileType
g_file_info_get_file_type (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_TYPE_UNKNOWN);
  return (GFileType)_g_file_attribute_value_get_uint32 (value);
}

/**
 * g_file_info_get_is_hidden:
 * @info: a #GFileInfo.
 *
 * Checks if a file is hidden.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
 *
 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
 **/
gboolean
g_file_info_get_is_hidden (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN, FALSE);
  return _g_file_attribute_value_get_boolean (value);
}

/**
 * g_file_info_get_is_backup:
 * @info: a #GFileInfo.
 *
 * Checks if a file is a backup file.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP.
 *
 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
 **/
gboolean
g_file_info_get_is_backup (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP, FALSE);
  return _g_file_attribute_value_get_boolean (value);
}

/**
 * g_file_info_get_is_symlink:
 * @info: a #GFileInfo.
 *
 * Checks if a file is a symlink.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
 *
 * Returns: %TRUE if the given @info is a symlink.
 **/
gboolean
g_file_info_get_is_symlink (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK, FALSE);
  return _g_file_attribute_value_get_boolean (value);
}

/**
 * g_file_info_get_name:
 * @info: a #GFileInfo.
 *
 * Gets the name for a file. This is guaranteed to always be set.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_NAME.
 *
 * Returns: (type filename) (not nullable): a string containing the file name.
 **/
const char *
g_file_info_get_name (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_NAME, NULL);
  return _g_file_attribute_value_get_byte_string (value);
}

/**
 * g_file_info_get_display_name:
 * @info: a #GFileInfo.
 *
 * Gets a display name for a file. This is guaranteed to always be set.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
 *
 * Returns: (not nullable): a string containing the display name.
 **/
const char *
g_file_info_get_display_name (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, NULL);
  return _g_file_attribute_value_get_string (value);
}

/**
 * g_file_info_get_edit_name:
 * @info: a #GFileInfo.
 *
 * Gets the edit name for a file.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
 *
 * Returns: a string containing the edit name.
 **/
const char *
g_file_info_get_edit_name (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME, NULL);
  return _g_file_attribute_value_get_string (value);
}

/**
 * g_file_info_get_icon:
 * @info: a #GFileInfo.
 *
 * Gets the icon for a file.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_ICON.
 *
 * Returns: (nullable) (transfer none): #GIcon for the given @info.
 **/
GIcon *
g_file_info_get_icon (GFileInfo *info)
{
  GFileAttributeValue *value;
  GObject *obj;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_ICON, NULL);

  obj = _g_file_attribute_value_get_object (value);
  if (G_IS_ICON (obj))
    return G_ICON (obj);
  return NULL;
}

/**
 * g_file_info_get_symbolic_icon:
 * @info: a #GFileInfo.
 *
 * Gets the symbolic icon for a file.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON.
 *
 * Returns: (nullable) (transfer none): #GIcon for the given @info.
 *
 * Since: 2.34
 **/
GIcon *
g_file_info_get_symbolic_icon (GFileInfo *info)
{
  GFileAttributeValue *value;
  GObject *obj;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON, NULL);

  obj = _g_file_attribute_value_get_object (value);
  if (G_IS_ICON (obj))
    return G_ICON (obj);
  return NULL;
}

/**
 * g_file_info_get_content_type:
 * @info: a #GFileInfo.
 *
 * Gets the file's content type.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
 *
 * Returns: (nullable): a string containing the file's content type,
 * or %NULL if unknown.
 **/
const char *
g_file_info_get_content_type (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, NULL);
  return _g_file_attribute_value_get_string (value);
}

/**
 * g_file_info_get_size:
 * @info: a #GFileInfo.
 *
 * Gets the file's size (in bytes). The size is retrieved through the value of
 * the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute and is converted
 * from #guint64 to #goffset before returning the result.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_SIZE.
 *
 * Returns: a #goffset containing the file's size (in bytes).
 **/
goffset
g_file_info_get_size (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_SIZE, (goffset) 0);
  return (goffset) _g_file_attribute_value_get_uint64 (value);
}

/**
 * g_file_info_get_modification_time:
 * @info: a #GFileInfo.
 * @result: (out caller-allocates): a #GTimeVal.
 *
 * Gets the modification time of the current @info and sets it
 * in @result.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_TIME_MODIFIED. If %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC is
 * provided it will be used too.
 *
 * Deprecated: 2.62: Use g_file_info_get_modification_date_time() instead, as
 *    #GTimeVal is deprecated due to the year 2038 problem.
 **/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_file_info_get_modification_time (GFileInfo *info,
				   GTimeVal  *result)
{
  static guint32 attr_mtime = 0, attr_mtime_usec;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (result != NULL);

  if (attr_mtime == 0)
    {
      attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
      attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
    }

  value = g_file_info_find_value (info, attr_mtime);

  if (G_UNLIKELY (value == NULL))
    {
      g_critical ("GFileInfo created without " G_FILE_ATTRIBUTE_TIME_MODIFIED);
      result->tv_sec = result->tv_usec = 0;
      g_return_if_reached ();
    }

  result->tv_sec = _g_file_attribute_value_get_uint64 (value);
  value = g_file_info_find_value (info, attr_mtime_usec);
  result->tv_usec = _g_file_attribute_value_get_uint32 (value);
}
G_GNUC_END_IGNORE_DEPRECATIONS

/**
 * g_file_info_get_modification_date_time:
 * @info: a #GFileInfo.
 *
 * Gets the modification time of the current @info and returns it as a
 * #GDateTime.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_TIME_MODIFIED. If %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC is
 * provided, the resulting #GDateTime will additionally have microsecond
 * precision.
 *
 * If nanosecond precision is needed, %G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC must
 * be queried separately using g_file_info_get_attribute_uint32().
 *
 * Returns: (transfer full) (nullable): modification time, or %NULL if unknown
 * Since: 2.62
 */
GDateTime *
g_file_info_get_modification_date_time (GFileInfo *info)
{
  static guint32 attr_mtime = 0, attr_mtime_usec;
  GFileAttributeValue *value, *value_usec;
  GDateTime *dt = NULL, *dt2 = NULL;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  if (attr_mtime == 0)
    {
      attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
      attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
    }

  value = g_file_info_find_value (info, attr_mtime);
  if (value == NULL)
    return NULL;

  dt = g_date_time_new_from_unix_utc (_g_file_attribute_value_get_uint64 (value));

  value_usec = g_file_info_find_value (info, attr_mtime_usec);
  if (value_usec == NULL)
    return g_steal_pointer (&dt);

  dt2 = g_date_time_add (dt, _g_file_attribute_value_get_uint32 (value_usec));
  g_date_time_unref (dt);

  return g_steal_pointer (&dt2);
}

/**
 * g_file_info_get_access_date_time:
 * @info: a #GFileInfo.
 *
 * Gets the access time of the current @info and returns it as a
 * #GDateTime.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_TIME_ACCESS. If %G_FILE_ATTRIBUTE_TIME_ACCESS_USEC is
 * provided, the resulting #GDateTime will additionally have microsecond
 * precision.
 *
 * If nanosecond precision is needed, %G_FILE_ATTRIBUTE_TIME_ACCESS_NSEC must
 * be queried separately using g_file_info_get_attribute_uint32().
 *
 * Returns: (transfer full) (nullable): access time, or %NULL if unknown
 * Since: 2.70
 */
GDateTime *
g_file_info_get_access_date_time (GFileInfo *info)
{
  static guint32 attr_atime = 0, attr_atime_usec;
  GFileAttributeValue *value, *value_usec;
  GDateTime *dt = NULL, *dt2 = NULL;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  if (attr_atime == 0)
    {
      attr_atime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_ACCESS);
      attr_atime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
    }

  value = g_file_info_find_value (info, attr_atime);
  if (value == NULL)
    return NULL;

  dt = g_date_time_new_from_unix_utc (_g_file_attribute_value_get_uint64 (value));

  value_usec = g_file_info_find_value (info, attr_atime_usec);
  if (value_usec == NULL)
    return g_steal_pointer (&dt);

  dt2 = g_date_time_add (dt, _g_file_attribute_value_get_uint32 (value_usec));
  g_date_time_unref (dt);

  return g_steal_pointer (&dt2);
}

/**
 * g_file_info_get_creation_date_time:
 * @info: a #GFileInfo.
 *
 * Gets the creation time of the current @info and returns it as a
 * #GDateTime.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_TIME_CREATED. If %G_FILE_ATTRIBUTE_TIME_CREATED_USEC is
 * provided, the resulting #GDateTime will additionally have microsecond
 * precision.
 *
 * If nanosecond precision is needed, %G_FILE_ATTRIBUTE_TIME_CREATED_NSEC must
 * be queried separately using g_file_info_get_attribute_uint32().
 *
 * Returns: (transfer full) (nullable): creation time, or %NULL if unknown
 * Since: 2.70
 */
GDateTime *
g_file_info_get_creation_date_time (GFileInfo *info)
{
  static guint32 attr_ctime = 0, attr_ctime_usec;
  GFileAttributeValue *value, *value_usec;
  GDateTime *dt = NULL, *dt2 = NULL;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  if (attr_ctime == 0)
    {
      attr_ctime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_CREATED);
      attr_ctime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_CREATED_USEC);
    }

  value = g_file_info_find_value (info, attr_ctime);
  if (value == NULL)
    return NULL;

  dt = g_date_time_new_from_unix_utc (_g_file_attribute_value_get_uint64 (value));

  value_usec = g_file_info_find_value (info, attr_ctime_usec);
  if (value_usec == NULL)
    return g_steal_pointer (&dt);

  dt2 = g_date_time_add (dt, _g_file_attribute_value_get_uint32 (value_usec));
  g_date_time_unref (dt);

  return g_steal_pointer (&dt2);
}

/**
 * g_file_info_get_symlink_target:
 * @info: a #GFileInfo.
 *
 * Gets the symlink target for a given #GFileInfo.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET.
 *
 * Returns: (type filename) (nullable): a string containing the symlink target.
 **/
const char *
g_file_info_get_symlink_target (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET, NULL);
  return _g_file_attribute_value_get_byte_string (value);
}

/**
 * g_file_info_get_etag:
 * @info: a #GFileInfo.
 *
 * Gets the [entity tag](iface.File.html#entity-tags) for a given
 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_ETAG_VALUE.
 *
 * Returns: (nullable): a string containing the value of the "etag:value" attribute.
 **/
const char *
g_file_info_get_etag (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_ETAG_VALUE, NULL);
  return _g_file_attribute_value_get_string (value);
}

/**
 * g_file_info_get_sort_order:
 * @info: a #GFileInfo.
 *
 * Gets the value of the sort_order attribute from the #GFileInfo.
 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
 *
 * It is an error to call this if the #GFileInfo does not contain
 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
 *
 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
 **/
gint32
g_file_info_get_sort_order (GFileInfo *info)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);

  get_required_attribute (&value, info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER, 0);
  return _g_file_attribute_value_get_int32 (value);
}

/* Helper setters: */
/**
 * g_file_info_set_file_type:
 * @info: a #GFileInfo.
 * @type: a #GFileType.
 *
 * Sets the file type in a #GFileInfo to @type.
 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
 **/
void
g_file_info_set_file_type (GFileInfo *info,
			   GFileType  type)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_uint32 (value, type);
}

/**
 * g_file_info_set_is_hidden:
 * @info: a #GFileInfo.
 * @is_hidden: a #gboolean.
 *
 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_hidden.
 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
 **/
void
g_file_info_set_is_hidden (GFileInfo *info,
			   gboolean   is_hidden)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_boolean (value, is_hidden);
}

/**
 * g_file_info_set_is_symlink:
 * @info: a #GFileInfo.
 * @is_symlink: a #gboolean.
 *
 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
 **/
void
g_file_info_set_is_symlink (GFileInfo *info,
			    gboolean   is_symlink)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_boolean (value, is_symlink);
}

/**
 * g_file_info_set_name:
 * @info: a #GFileInfo.
 * @name: (type filename): a string containing a name.
 *
 * Sets the name attribute for the current #GFileInfo.
 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
 **/
void
g_file_info_set_name (GFileInfo  *info,
		      const char *name)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (name != NULL);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_byte_string (value, name);
}

/**
 * g_file_info_set_display_name:
 * @info: a #GFileInfo.
 * @display_name: a string containing a display name.
 *
 * Sets the display name for the current #GFileInfo.
 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
 **/
void
g_file_info_set_display_name (GFileInfo  *info,
			      const char *display_name)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (display_name != NULL);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_string (value, display_name);
}

/**
 * g_file_info_set_edit_name:
 * @info: a #GFileInfo.
 * @edit_name: a string containing an edit name.
 *
 * Sets the edit name for the current file.
 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
 **/
void
g_file_info_set_edit_name (GFileInfo  *info,
			   const char *edit_name)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (edit_name != NULL);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_string (value, edit_name);
}

/**
 * g_file_info_set_icon:
 * @info: a #GFileInfo.
 * @icon: a #GIcon.
 *
 * Sets the icon for a given #GFileInfo.
 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
 **/
void
g_file_info_set_icon (GFileInfo *info,
		      GIcon     *icon)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (G_IS_ICON (icon));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_object (value, G_OBJECT (icon));
}

/**
 * g_file_info_set_symbolic_icon:
 * @info: a #GFileInfo.
 * @icon: a #GIcon.
 *
 * Sets the symbolic icon for a given #GFileInfo.
 * See %G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON.
 *
 * Since: 2.34
 **/
void
g_file_info_set_symbolic_icon (GFileInfo *info,
                               GIcon     *icon)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (G_IS_ICON (icon));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_object (value, G_OBJECT (icon));
}

/**
 * g_file_info_set_content_type:
 * @info: a #GFileInfo.
 * @content_type: a content type. See [GContentType][gio-GContentType]
 *
 * Sets the content type attribute for a given #GFileInfo.
 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
 **/
void
g_file_info_set_content_type (GFileInfo  *info,
			      const char *content_type)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (content_type != NULL);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_string (value, content_type);
}

/**
 * g_file_info_set_size:
 * @info: a #GFileInfo.
 * @size: a #goffset containing the file's size.
 *
 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
 * to the given size.
 **/
void
g_file_info_set_size (GFileInfo *info,
		      goffset    size)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_uint64 (value, size);
}

/**
 * g_file_info_set_modification_time:
 * @info: a #GFileInfo.
 * @mtime: a #GTimeVal.
 *
 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED and
 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC attributes in the file info to the
 * given time value.
 *
 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC will be cleared.
 *
 * Deprecated: 2.62: Use g_file_info_set_modification_date_time() instead, as
 *    #GTimeVal is deprecated due to the year 2038 problem.
 **/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_file_info_set_modification_time (GFileInfo *info,
				   GTimeVal  *mtime)
{
  static guint32 attr_mtime = 0, attr_mtime_usec = 0, attr_mtime_nsec = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (mtime != NULL);

  if (attr_mtime == 0)
    {
      attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
      attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
      attr_mtime_nsec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC);
    }

  value = g_file_info_create_value (info, attr_mtime);
  if (value)
    _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
  value = g_file_info_create_value (info, attr_mtime_usec);
  if (value)
    _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);

  /* nsecs can’t be known from a #GTimeVal, so remove them */
  g_file_info_remove_value (info, attr_mtime_nsec);
}
G_GNUC_END_IGNORE_DEPRECATIONS

/**
 * g_file_info_set_modification_date_time:
 * @info: a #GFileInfo.
 * @mtime: (not nullable): a #GDateTime.
 *
 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED and
 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC attributes in the file info to the
 * given date/time value.
 *
 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC will be cleared.
 *
 * Since: 2.62
 */
void
g_file_info_set_modification_date_time (GFileInfo *info,
                                        GDateTime *mtime)
{
  static guint32 attr_mtime = 0, attr_mtime_usec = 0, attr_mtime_nsec = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (mtime != NULL);

  if (attr_mtime == 0)
    {
      attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
      attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
      attr_mtime_nsec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC);
    }

  value = g_file_info_create_value (info, attr_mtime);
  if (value)
    _g_file_attribute_value_set_uint64 (value, g_date_time_to_unix (mtime));
  value = g_file_info_create_value (info, attr_mtime_usec);
  if (value)
    _g_file_attribute_value_set_uint32 (value, g_date_time_get_microsecond (mtime));

  /* nsecs can’t be known from a #GDateTime, so remove them */
  g_file_info_remove_value (info, attr_mtime_nsec);
}

/**
 * g_file_info_set_access_date_time:
 * @info: a #GFileInfo.
 * @atime: (not nullable): a #GDateTime.
 *
 * Sets the %G_FILE_ATTRIBUTE_TIME_ACCESS and
 * %G_FILE_ATTRIBUTE_TIME_ACCESS_USEC attributes in the file info to the
 * given date/time value.
 *
 * %G_FILE_ATTRIBUTE_TIME_ACCESS_NSEC will be cleared.
 *
 * Since: 2.70
 */
void
g_file_info_set_access_date_time (GFileInfo *info,
                                  GDateTime *atime)
{
  static guint32 attr_atime = 0, attr_atime_usec = 0, attr_atime_nsec = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (atime != NULL);

  if (attr_atime == 0)
    {
      attr_atime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_ACCESS);
      attr_atime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
      attr_atime_nsec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_ACCESS_NSEC);
    }

  value = g_file_info_create_value (info, attr_atime);
  if (value)
    _g_file_attribute_value_set_uint64 (value, g_date_time_to_unix (atime));
  value = g_file_info_create_value (info, attr_atime_usec);
  if (value)
    _g_file_attribute_value_set_uint32 (value, g_date_time_get_microsecond (atime));

  /* nsecs can’t be known from a #GDateTime, so remove them */
  g_file_info_remove_value (info, attr_atime_nsec);
}

/**
 * g_file_info_set_creation_date_time:
 * @info: a #GFileInfo.
 * @creation_time: (not nullable): a #GDateTime.
 *
 * Sets the %G_FILE_ATTRIBUTE_TIME_CREATED and
 * %G_FILE_ATTRIBUTE_TIME_CREATED_USEC attributes in the file info to the
 * given date/time value.
 *
 * %G_FILE_ATTRIBUTE_TIME_CREATED_NSEC will be cleared.
 *
 * Since: 2.70
 */
void
g_file_info_set_creation_date_time (GFileInfo *info,
                                    GDateTime *creation_time)
{
  static guint32 attr_ctime = 0, attr_ctime_usec = 0, attr_ctime_nsec = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (creation_time != NULL);

  if (attr_ctime == 0)
    {
      attr_ctime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_CREATED);
      attr_ctime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_CREATED_USEC);
      attr_ctime_nsec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_CREATED_NSEC);
    }

  value = g_file_info_create_value (info, attr_ctime);
  if (value)
    _g_file_attribute_value_set_uint64 (value, g_date_time_to_unix (creation_time));
  value = g_file_info_create_value (info, attr_ctime_usec);
  if (value)
    _g_file_attribute_value_set_uint32 (value, g_date_time_get_microsecond (creation_time));

  /* nsecs can’t be known from a #GDateTime, so remove them */
  g_file_info_remove_value (info, attr_ctime_nsec);
}

/**
 * g_file_info_set_symlink_target:
 * @info: a #GFileInfo.
 * @symlink_target: (type filename): a static string containing a path to a symlink target.
 *
 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
 * to the given symlink target.
 **/
void
g_file_info_set_symlink_target (GFileInfo  *info,
				const char *symlink_target)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (symlink_target != NULL);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_byte_string (value, symlink_target);
}

/**
 * g_file_info_set_sort_order:
 * @info: a #GFileInfo.
 * @sort_order: a sort order integer.
 *
 * Sets the sort order attribute in the file info structure. See
 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
 **/
void
g_file_info_set_sort_order (GFileInfo *info,
			    gint32     sort_order)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_if_fail (G_IS_FILE_INFO (info));

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);

  value = g_file_info_create_value (info, attr);
  if (value)
    _g_file_attribute_value_set_int32 (value, sort_order);
}


typedef struct {
  guint32 id;
  guint32 mask;
} SubMatcher;

struct _GFileAttributeMatcher {
  gboolean all;
  gint ref;

  GArray *sub_matchers;

  /* Iterator */
  guint32 iterator_ns;
  gint iterator_pos;
};

G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
                     g_file_attribute_matcher_ref,
                     g_file_attribute_matcher_unref)

static gint
compare_sub_matchers (gconstpointer a,
                      gconstpointer b)
{
  const SubMatcher *suba = a;
  const SubMatcher *subb = b;
  int diff;

  diff = suba->id - subb->id;

  if (diff)
    return diff;

  return suba->mask - subb->mask;
}

static gboolean
sub_matcher_matches (SubMatcher *matcher,
                     SubMatcher *submatcher)
{
  if ((matcher->mask & submatcher->mask) != matcher->mask)
    return FALSE;
  
  return matcher->id == (submatcher->id & matcher->mask);
}

/* Call this function after modifying a matcher.
 * It will ensure all the invariants other functions rely on.
 */
static GFileAttributeMatcher *
matcher_optimize (GFileAttributeMatcher *matcher)
{
  SubMatcher *submatcher, *compare;
  guint i, j;

  /* remove sub_matchers if we match everything anyway */
  if (matcher->all)
    {
      if (matcher->sub_matchers)
        {
          g_array_free (matcher->sub_matchers, TRUE);
          matcher->sub_matchers = NULL;
        }
      return matcher;
    }

  if (matcher->sub_matchers->len == 0)
    {
      g_file_attribute_matcher_unref (matcher);
      return NULL;
    }

  /* sort sub_matchers by id (and then mask), so we can bsearch
   * and compare matchers in O(N) instead of O(N²) */
  g_array_sort (matcher->sub_matchers, compare_sub_matchers);

  /* remove duplicates and specific matches when we match the whole namespace */
  j = 0;
  compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);

  for (i = 1; i < matcher->sub_matchers->len; i++)
    {
      submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
      if (sub_matcher_matches (compare, submatcher))
        continue;

      j++;
      compare++;

      if (j < i)
        *compare = *submatcher;
    }

  g_array_set_size (matcher->sub_matchers, j + 1);

  return matcher;
}

/**
 * g_file_attribute_matcher_new:
 * @attributes: an attribute string to match.
 *
 * Creates a new file attribute matcher, which matches attributes
 * against a given string. #GFileAttributeMatchers are reference
 * counted structures, and are created with a reference count of 1. If
 * the number of references falls to 0, the #GFileAttributeMatcher is
 * automatically destroyed.
 *
 * The @attributes string should be formatted with specific keys separated
 * from namespaces with a double colon. Several "namespace::key" strings may be
 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
 * The wildcard "*" may be used to match all keys and namespaces, or
 * "namespace::*" will match all keys in a given namespace.
 *
 * ## Examples of file attribute matcher strings and results
 *
 * - `"*"`: matches all attributes.
 * - `"standard::is-hidden"`: matches only the key is-hidden in the
 *   standard namespace.
 * - `"standard::type,unix::*"`: matches the type key in the standard
 *   namespace and all keys in the unix namespace.
 *
 * Returns: a #GFileAttributeMatcher
 */
GFileAttributeMatcher *
g_file_attribute_matcher_new (const char *attributes)
{
  char **split;
  char *colon;
  int i;
  GFileAttributeMatcher *matcher;

  if (attributes == NULL || *attributes == '\0')
    return NULL;

  matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
  matcher->ref = 1;
  matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));

  split = g_strsplit (attributes, ",", -1);

  for (i = 0; split[i] != NULL; i++)
    {
      if (strcmp (split[i], "*") == 0)
	matcher->all = TRUE;
      else
	{
          SubMatcher s;

	  colon = strstr (split[i], "::");
	  if (colon != NULL &&
	      !(colon[2] == 0 ||
		(colon[2] == '*' &&
		 colon[3] == 0)))
	    {
	      s.id = lookup_attribute (split[i]);
	      s.mask = 0xffffffff;
	    }
	  else
	    {
	      if (colon)
		*colon = 0;

	      s.id = lookup_namespace (split[i]) << NS_POS;
	      s.mask = NS_MASK << NS_POS;
	    }

          g_array_append_val (matcher->sub_matchers, s);
	}
    }

  g_strfreev (split);

  matcher = matcher_optimize (matcher);

  return matcher;
}

/**
 * g_file_attribute_matcher_subtract:
 * @matcher: (nullable): Matcher to subtract from 
 * @subtract: (nullable): The matcher to subtract
 *
 * Subtracts all attributes of @subtract from @matcher and returns
 * a matcher that supports those attributes.
 *
 * Note that currently it is not possible to remove a single
 * attribute when the @matcher matches the whole namespace - or remove
 * a namespace or attribute when the matcher matches everything. This
 * is a limitation of the current implementation, but may be fixed
 * in the future.
 *
 * Returns: (nullable): A file attribute matcher matching all attributes of
 *     @matcher that are not matched by @subtract
 **/
GFileAttributeMatcher *
g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
                                   GFileAttributeMatcher *subtract)
{
  GFileAttributeMatcher *result;
  guint mi, si;
  SubMatcher *msub, *ssub;

  if (matcher == NULL)
    return NULL;
  if (subtract == NULL)
    return g_file_attribute_matcher_ref (matcher);
  if (subtract->all)
    return NULL;
  if (matcher->all)
    return g_file_attribute_matcher_ref (matcher);

  result = g_malloc0 (sizeof (GFileAttributeMatcher));
  result->ref = 1;
  result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));

  si = 0;
  g_assert (subtract->sub_matchers->len > 0);
  ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);

  for (mi = 0; mi < matcher->sub_matchers->len; mi++)
    {
      msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);

retry:
      if (sub_matcher_matches (ssub, msub))
        continue;

      si++;
      if (si >= subtract->sub_matchers->len)
        break;

      ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
      if (ssub->id <= msub->id)
        goto retry;

      g_array_append_val (result->sub_matchers, *msub);
    }

  if (mi < matcher->sub_matchers->len)
    g_array_append_vals (result->sub_matchers,
                         &g_array_index (matcher->sub_matchers, SubMatcher, mi),
                         matcher->sub_matchers->len - mi);

  result = matcher_optimize (result);

  return result;
}

/**
 * g_file_attribute_matcher_ref:
 * @matcher: a #GFileAttributeMatcher.
 *
 * References a file attribute matcher.
 *
 * Returns: a #GFileAttributeMatcher.
 **/
GFileAttributeMatcher *
g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
{
  if (matcher)
    {
      g_return_val_if_fail (matcher->ref > 0, NULL);
      g_atomic_int_inc (&matcher->ref);
    }
  return matcher;
}

/**
 * g_file_attribute_matcher_unref:
 * @matcher: a #GFileAttributeMatcher.
 *
 * Unreferences @matcher. If the reference count falls below 1,
 * the @matcher is automatically freed.
 *
 **/
void
g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
{
  if (matcher)
    {
      g_return_if_fail (matcher->ref > 0);

      if (g_atomic_int_dec_and_test (&matcher->ref))
	{
	  if (matcher->sub_matchers)
	    g_array_free (matcher->sub_matchers, TRUE);

	  g_free (matcher);
	}
    }
}

/**
 * g_file_attribute_matcher_matches_only:
 * @matcher: a #GFileAttributeMatcher.
 * @attribute: a file attribute key.
 *
 * Checks if an attribute matcher only matches a given attribute. Always
 * returns %FALSE if "*" was used when creating the matcher.
 *
 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
 **/
gboolean
g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
				       const char            *attribute)
{
  SubMatcher *sub_matcher;
  guint32 id;

  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  if (matcher == NULL ||
      matcher->all)
    return FALSE;

  if (matcher->sub_matchers->len != 1)
    return FALSE;
  
  id = lookup_attribute (attribute);
  
  sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
  
  return sub_matcher->id == id &&
         sub_matcher->mask == 0xffffffff;
}

static gboolean
matcher_matches_id (GFileAttributeMatcher *matcher,
                    guint32                id)
{
  SubMatcher *sub_matchers;
  guint i;

  if (matcher->sub_matchers)
    {
      sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
      for (i = 0; i < matcher->sub_matchers->len; i++)
	{
	  if (sub_matchers[i].id == (id & sub_matchers[i].mask))
	    return TRUE;
	}
    }

  return FALSE;
}

gboolean
_g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
                                      guint32                id)
{
  /* We return a NULL matcher for an empty match string, so handle this */
  if (matcher == NULL)
    return FALSE;

  if (matcher->all)
    return TRUE;

  return matcher_matches_id (matcher, id);
}

/**
 * g_file_attribute_matcher_matches:
 * @matcher: a #GFileAttributeMatcher.
 * @attribute: a file attribute key.
 *
 * Checks if an attribute will be matched by an attribute matcher. If
 * the matcher was created with the "*" matching string, this function
 * will always return %TRUE.
 *
 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
 **/
gboolean
g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
				  const char            *attribute)
{
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  /* We return a NULL matcher for an empty match string, so handle this */
  if (matcher == NULL)
    return FALSE;

  if (matcher->all)
    return TRUE;

  return matcher_matches_id (matcher, lookup_attribute (attribute));
}

/* return TRUE -> all */
/**
 * g_file_attribute_matcher_enumerate_namespace:
 * @matcher: a #GFileAttributeMatcher.
 * @ns: a string containing a file attribute namespace.
 *
 * Checks if the matcher will match all of the keys in a given namespace.
 * This will always return %TRUE if a wildcard character is in use (e.g. if
 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
 * using "*" and namespace is anything.)
 *
 * TODO: this is awkwardly worded.
 *
 * Returns: %TRUE if the matcher matches all of the entries
 * in the given @ns, %FALSE otherwise.
 **/
gboolean
g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
					      const char            *ns)
{
  SubMatcher *sub_matchers;
  guint ns_id;
  guint i;

  g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);

  /* We return a NULL matcher for an empty match string, so handle this */
  if (matcher == NULL)
    return FALSE;

  if (matcher->all)
    return TRUE;

  ns_id = lookup_namespace (ns) << NS_POS;

  if (matcher->sub_matchers)
    {
      sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
      for (i = 0; i < matcher->sub_matchers->len; i++)
	{
	  if (sub_matchers[i].id == ns_id)
	    return TRUE;
	}
    }

  matcher->iterator_ns = ns_id;
  matcher->iterator_pos = 0;

  return FALSE;
}

/**
 * g_file_attribute_matcher_enumerate_next:
 * @matcher: a #GFileAttributeMatcher.
 *
 * Gets the next matched attribute from a #GFileAttributeMatcher.
 *
 * Returns: (nullable): a string containing the next attribute or, %NULL if
 * no more attribute exist.
 **/
const char *
g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
{
  guint i;
  SubMatcher *sub_matcher;

  /* We return a NULL matcher for an empty match string, so handle this */
  if (matcher == NULL)
    return NULL;

  while (1)
    {
      i = matcher->iterator_pos++;

      if (matcher->sub_matchers == NULL)
        return NULL;

      if (i < matcher->sub_matchers->len)
        sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
      else
        return NULL;

      if (sub_matcher->mask == 0xffffffff &&
	  (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
	return get_attribute_for_id (sub_matcher->id);
    }
}

/**
 * g_file_attribute_matcher_to_string:
 * @matcher: (nullable): a #GFileAttributeMatcher.
 *
 * Prints what the matcher is matching against. The format will be 
 * equal to the format passed to g_file_attribute_matcher_new().
 * The output however, might not be identical, as the matcher may
 * decide to use a different order or omit needless parts.
 *
 * Returns: a string describing the attributes the matcher matches
 *   against or %NULL if @matcher was %NULL.
 *
 * Since: 2.32
 **/
char *
g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
{
  GString *string;
  guint i;

  if (matcher == NULL)
    return NULL;

  if (matcher->all)
    return g_strdup ("*");

  string = g_string_new ("");
  for (i = 0; i < matcher->sub_matchers->len; i++)
    {
      SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);

      if (i > 0)
        g_string_append_c (string, ',');

      g_string_append (string, get_attribute_for_id (submatcher->id));
    }

  return g_string_free (string, FALSE);
}