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 863 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 1863 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 2863 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 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
|
/* GIO - GLib Input, Output and Streaming Library
* MTP Backend
*
* Copyright (C) 2012 Philip Langdale <[email protected]>
*
* 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 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/>.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <libmtp.h>
#if HAVE_LIBUSB
#include <libusb.h>
#endif
#include "gvfsbackendmtp.h"
#include "gvfsicon.h"
#include "gvfsjobopenforread.h"
#include "gvfsjobread.h"
#include "gvfsjobseekread.h"
#include "gvfsjobopenforwrite.h"
#include "gvfsjobwrite.h"
#include "gvfsjobclosewrite.h"
#include "gvfsjobseekwrite.h"
#include "gvfsjobsetdisplayname.h"
#include "gvfsjobqueryinfo.h"
#include "gvfsjobdelete.h"
#include "gvfsjobqueryfsinfo.h"
#include "gvfsjobqueryattributes.h"
#include "gvfsjobenumerate.h"
#include "gvfsdaemonprotocol.h"
#include "gvfsjobcreatemonitor.h"
#include "gvfsjobmakedirectory.h"
#include "gvfsmonitor.h"
#include "gvfsgphoto2utils.h"
/************************************************
* Storage constants copied from ptp.h
*
* ptp.h is treated as a private header by libmtp
************************************************/
/* PTP Storage Types */
#define PTP_ST_Undefined 0x0000
#define PTP_ST_FixedROM 0x0001
#define PTP_ST_RemovableROM 0x0002
#define PTP_ST_FixedRAM 0x0003
#define PTP_ST_RemovableRAM 0x0004
/************************************************
* Constants
************************************************/
#if HAVE_LIBUSB
#define EVENT_POLL_PERIOD { 3600, 0 }
#else
#define EVENT_POLL_PERIOD { 1, 0 }
#endif
/************************************************
* Private Types
************************************************/
typedef enum {
HANDLE_FILE,
HANDLE_PREVIEW,
} HandleType;
typedef struct {
HandleType handle_type;
uint32_t id;
goffset offset;
gsize size;
/* For previews only */
GByteArray *bytes;
} RWHandle;
typedef struct {
uint32_t storage;
uint32_t id;
} CacheEntry;
typedef struct {
LIBMTP_event_t event;
uint32_t param1;
} EventData;
/************************************************
* Static prototypes
************************************************/
static void
emit_delete_event (gpointer key,
gpointer value,
gpointer user_data);
static void
handle_event (EventData *data, GVfsBackendMtp *backend);
/************************************************
* Storage name helper
************************************************/
static char *create_storage_name (const LIBMTP_devicestorage_t *storage)
{
/* The optional post-fixing of storage's name with ID requires us to
know in advance whether the storage's description string is unique
or not. Since this function is called in several places, it is
safest to perform this check here, each time that storage name needs
to be created. */
gboolean is_unique = TRUE;
const LIBMTP_devicestorage_t *tmp_storage;
/* Forward search for duplicates */
for (tmp_storage = storage->next; tmp_storage != 0; tmp_storage = tmp_storage->next) {
if (!g_strcmp0 (storage->StorageDescription, tmp_storage->StorageDescription)) {
is_unique = FALSE;
break;
}
}
/* Backward search, if necessary */
if (is_unique) {
for (tmp_storage = storage->prev; tmp_storage != 0; tmp_storage = tmp_storage->prev) {
/* Compare descriptions */
if (!g_strcmp0 (storage->StorageDescription, tmp_storage->StorageDescription)) {
is_unique = FALSE;
break;
}
}
}
/* If description is unique, we can use it as storage name; otherwise,
we add storage ID to it */
if (is_unique) {
return g_strdup (storage->StorageDescription);
} else {
return g_strdup_printf ("%s (%X)", storage->StorageDescription, storage->id);
}
}
/************************************************
* Cache Helpers
************************************************/
static void
add_cache_entry (GVfsBackendMtp *backend,
char *path,
uint32_t storage,
uint32_t id)
{
CacheEntry *entry = g_new0 (CacheEntry, 1);
entry->storage = storage;
entry->id = id;
g_debug ("(II) add_cache_entry: %s: %X, %X\n",
path, entry->storage, entry->id);
g_hash_table_replace (backend->file_cache,
path, entry);
}
static char *
build_partial_path (char **elements,
unsigned int ne)
{
char **pe = g_new0 (char *, ne 2);
int i;
pe[0] = g_strdup("/");
for (i = 0; i < ne; i ) {
pe[i 1] = elements[i];
}
char *path = g_build_filenamev(pe);
g_free (pe);
return path;
}
/**
* get_file_for_filename:
*
* Get the entity ID for an element given its filename and
* the IDs of its parents.
*
* Called with backend mutex lock held.
*/
static void
add_cache_entries_for_filename (GVfsBackendMtp *backend,
const char *path)
{
LIBMTP_file_t *file = NULL;
LIBMTP_mtpdevice_t *device = backend->device;
gchar **elements = g_strsplit_set (path, "/", -1);
unsigned int ne = g_strv_length (elements);
g_debug ("(III) add_cache_entries_for_filename: %s, %u\n", path, ne);
if (ne < 2) {
g_debug ("(III) Ignoring query on invalid path\n");
goto exit;
}
/* Identify Storage */
LIBMTP_devicestorage_t *storage;
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
goto exit;
}
for (storage = device->storage; storage != 0; storage = storage->next) {
/* Construct the name for storage and compare it to first element of path */
char *storage_name = create_storage_name (storage);
int is_equal = !g_strcmp0 (elements[1], storage_name);
g_free(storage_name);
if (is_equal) {
char *partial = build_partial_path (elements, 2);
add_cache_entry (backend, partial, storage->id, -1);
break;
}
}
if (!storage) {
g_debug ("(III) Ignoring query on invalid storage\n");
goto exit;
}
long parent_id = -1;
int i;
for (i = 2; i < ne; i ) {
LIBMTP_file_t *f =
LIBMTP_Get_Files_And_Folders (device, storage->id, parent_id);
while (f != NULL) {
g_debug ("(III) query (entity = %s, name = %s)\n", f->filename, elements[i]);
if (strcmp (f->filename, elements[i]) == 0) {
file = f;
f = f->next;
char *partial = build_partial_path (elements, i 1);
add_cache_entry (backend, partial, file->storage_id, file->item_id);
break;
} else {
LIBMTP_file_t *tmp = f;
f = f->next;
LIBMTP_destroy_file_t (tmp);
}
}
while (f != NULL) {
LIBMTP_file_t *tmp = f;
f = f->next;
LIBMTP_destroy_file_t (tmp);
}
if (!file) {
g_debug ("(III) Ignoring query for non-existent file\n");
goto exit;
}
parent_id = file->item_id;
}
exit:
g_strfreev (elements);
g_debug ("(III) add_cache_entries_for_filename done\n");
}
static CacheEntry *get_cache_entry (GVfsBackendMtp *backend,
const char *path)
{
g_debug ("(III) get_cache_entry: %s\n", path);
CacheEntry *entry = g_hash_table_lookup (backend->file_cache, path);
if (!entry) {
add_cache_entries_for_filename (backend, path);
entry = g_hash_table_lookup (backend->file_cache, path);
}
g_debug ("(III) get_cache_entry done: %p\n", entry);
return entry;
}
static gboolean
remove_cache_entry_by_prefix (gpointer key,
gpointer value,
gpointer user_data)
{
const char *path = key;
const char *prefix = user_data;
return g_str_has_prefix (path, prefix);
}
static void
remove_cache_entry (GVfsBackendMtp *backend,
const char *path)
{
g_debug ("(III) remove_cache_entry: %s\n", path);
g_hash_table_foreach_remove (backend->file_cache,
remove_cache_entry_by_prefix,
(gpointer) path);
g_debug ("(III) remove_cache_entry done\n");
}
static void
remove_cache_entry_by_id (GVfsBackendMtp *backend,
uint32_t id)
{
GHashTableIter iter;
gpointer key, value;
g_debug ("(III) remove_cache_entry_by_id: %X\n", id);
g_hash_table_iter_init (&iter, backend->file_cache);
while (g_hash_table_iter_next (&iter, &key, &value)) {
const char *path = key;
const CacheEntry *entry = value;
if (entry->id == id || (entry->id == -1 && entry->storage == id)) {
g_hash_table_foreach (backend->monitors,
emit_delete_event,
(char *)path);
g_hash_table_iter_remove (&iter);
/* We do not break here because we can end up with multiple entries
that have same storage/object ID, and we should find and remove
them all instead of just the first one (Bug #733886) */
}
}
g_debug ("(III) remove_cache_entry_by_id done\n");
}
/************************************************
* Initialization
************************************************/
G_DEFINE_TYPE (GVfsBackendMtp, g_vfs_backend_mtp, G_VFS_TYPE_BACKEND)
static void
g_vfs_backend_mtp_init (GVfsBackendMtp *backend)
{
g_debug ("(I) g_vfs_backend_mtp_init\n");
GMountSpec *mount_spec;
const char *debug;
g_mutex_init (&backend->mutex);
g_vfs_backend_set_display_name (G_VFS_BACKEND (backend), "mtp");
g_vfs_backend_set_icon_name (G_VFS_BACKEND (backend), "multimedia-player");
mount_spec = g_mount_spec_new ("mtp");
g_vfs_backend_set_mount_spec (G_VFS_BACKEND (backend), mount_spec);
g_mount_spec_unref (mount_spec);
backend->monitors = g_hash_table_new (NULL, NULL);
backend->event_pool = g_thread_pool_new ((GFunc) handle_event,
backend, 1, FALSE, NULL);
debug = g_getenv ("GVFS_MTP_DEBUG");
if (debug != NULL) {
int level;
if (g_ascii_strcasecmp ("ptp", debug) == 0)
level = LIBMTP_DEBUG_PTP;
else if (g_ascii_strcasecmp ("usb", debug) == 0)
level = LIBMTP_DEBUG_USB | LIBMTP_DEBUG_PTP;
else if (g_ascii_strcasecmp ("data", debug) == 0)
level = LIBMTP_DEBUG_DATA | LIBMTP_DEBUG_USB | LIBMTP_DEBUG_PTP;
else /* "all" */
level = LIBMTP_DEBUG_ALL;
LIBMTP_Set_Debug (level);
}
g_debug ("(I) g_vfs_backend_mtp_init done.\n");
}
static void
remove_monitor_weak_ref (gpointer monitor,
gpointer unused,
gpointer monitors)
{
g_object_weak_unref (G_OBJECT(monitor), (GWeakNotify)g_hash_table_remove, monitors);
}
static void
g_vfs_backend_mtp_finalize (GObject *object)
{
GVfsBackendMtp *backend;
g_debug ("(I) g_vfs_backend_mtp_finalize\n");
backend = G_VFS_BACKEND_MTP (object);
g_thread_pool_free (backend->event_pool, TRUE, TRUE);
g_hash_table_foreach (backend->monitors, remove_monitor_weak_ref, backend->monitors);
g_hash_table_unref (backend->monitors);
/* Leak the mutex if the backend is force unmounted to avoid crash caused by
* abort(), when trying to clear already locked mutex. */
if (!backend->force_unmounted)
g_mutex_clear (&backend->mutex);
(*G_OBJECT_CLASS (g_vfs_backend_mtp_parent_class)->finalize) (object);
g_debug ("(I) g_vfs_backend_mtp_finalize done.\n");
}
/************************************************
* Monitors
************************************************/
/**
* do_create_dir_monitor:
*
* Called with backend mutex lock held.
*/
static void
do_create_dir_monitor (GVfsBackend *backend,
GVfsJobCreateMonitor *job,
const char *filename,
GFileMonitorFlags flags)
{
GVfsBackendMtp *mtp_backend = G_VFS_BACKEND_MTP (backend);
g_debug ("(I) create_dir_monitor (%s)\n", filename);
GVfsMonitor *vfs_monitor = g_vfs_monitor_new (backend);
g_object_set_data_full (G_OBJECT (vfs_monitor), "gvfsbackendmtp:path",
g_strdup (filename), g_free);
g_vfs_job_create_monitor_set_monitor (job, vfs_monitor);
g_hash_table_add (mtp_backend->monitors, vfs_monitor);
g_object_weak_ref (G_OBJECT (vfs_monitor), (GWeakNotify)g_hash_table_remove, mtp_backend->monitors);
g_object_unref (vfs_monitor);
g_vfs_job_succeeded (G_VFS_JOB (job));
g_debug ("(I) create_dir_monitor done.\n");
}
/**
* do_create_file_monitor:
*
* Called with backend mutex lock held.
*/
static void
do_create_file_monitor (GVfsBackend *backend,
GVfsJobCreateMonitor *job,
const char *filename,
GFileMonitorFlags flags)
{
GVfsBackendMtp *mtp_backend = G_VFS_BACKEND_MTP (backend);
g_debug ("(I) create_file_monitor (%s)\n", filename);
GVfsMonitor *vfs_monitor = g_vfs_monitor_new (backend);
g_object_set_data_full (G_OBJECT (vfs_monitor), "gvfsbackendmtp:path",
g_strdup (filename), g_free);
g_vfs_job_create_monitor_set_monitor (job, vfs_monitor);
g_hash_table_add (mtp_backend->monitors, vfs_monitor);
g_object_weak_ref (G_OBJECT (vfs_monitor), (GWeakNotify)g_hash_table_remove, mtp_backend->monitors);
g_object_unref (vfs_monitor);
g_vfs_job_succeeded (G_VFS_JOB (job));
g_debug ("(I) create_file_monitor done.\n");
}
static void
emit_event_internal (GVfsMonitor *monitor,
const char *path,
GFileMonitorEvent event)
{
g_debug ("(III) emit_event_internal (%s, %d)\n", path, event);
char *dir = g_path_get_dirname (path);
const char *monitored_path = g_object_get_data (G_OBJECT (monitor), "gvfsbackendmtp:path");
if (g_strcmp0 (dir, monitored_path) == 0) {
g_debug ("(III) emit_event_internal: Event %d on directory %s for %s\n", event, dir, path);
g_vfs_monitor_emit_event (monitor, event, path, NULL);
} else if (g_strcmp0 (path, monitored_path) == 0) {
g_debug ("(III) emit_event_internal: Event %d on file %s\n", event, path);
g_vfs_monitor_emit_event (monitor, event, path, NULL);
}
g_free (dir);
g_debug ("(III) emit_event_internal done.\n");
}
static void
emit_create_event (gpointer key,
gpointer value,
gpointer user_data)
{
g_debug ("(II) emit_create_event.\n");
emit_event_internal (key, user_data, G_FILE_MONITOR_EVENT_CREATED);
}
static void
emit_delete_event (gpointer key,
gpointer value,
gpointer user_data)
{
g_debug ("(II) emit_delete_event.\n");
emit_event_internal (key, user_data, G_FILE_MONITOR_EVENT_DELETED);
}
/************************************************
* Errors
************************************************/
static void
fail_job (GVfsJob *job, LIBMTP_mtpdevice_t *device)
{
const char *text;
LIBMTP_error_t *error = LIBMTP_Get_Errorstack (device);
if (error) {
text = g_strrstr (error->error_text, ":") 1;
} else {
text = _("Unknown error.");
}
g_vfs_job_failed (job, G_IO_ERROR,
g_vfs_job_is_cancelled (job) ?
G_IO_ERROR_CANCELLED :
G_IO_ERROR_FAILED,
_("libmtp error: %s"),
text);
LIBMTP_Clear_Errorstack (device);
}
/************************************************
* Mounts
************************************************/
static LIBMTP_mtpdevice_t *
get_device (GVfsBackend *backend, uint32_t bus_num, uint32_t dev_num , GVfsJob *job);
static void
on_uevent (GUdevClient *client, gchar *action, GUdevDevice *device, gpointer user_data)
{
const char *dev_path = g_udev_device_get_device_file (device);
g_debug ("(I) on_uevent (action %s, device %s)\n", action, dev_path);
if (dev_path == NULL) {
return;
}
GVfsBackendMtp *op_backend = G_VFS_BACKEND_MTP (user_data);
if (g_strcmp0 (op_backend->dev_path, dev_path) == 0 &&
g_str_equal (action, "remove")) {
g_debug ("(I) on_uevent: Quiting after remove event on device %s\n", dev_path);
/* Emit delete events to tell clients files are gone. */
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, op_backend->file_cache);
while (g_hash_table_iter_next (&iter, &key, &value)) {
const char *path = key;
g_hash_table_foreach (op_backend->monitors,
emit_delete_event,
(char *)path);
}
op_backend->force_unmounted = TRUE;
g_atomic_int_set (&op_backend->unmount_started, TRUE);
g_vfs_backend_force_unmount ((GVfsBackend*)op_backend);
g_signal_handlers_disconnect_by_func (op_backend->gudev_client, on_uevent, op_backend);
}
g_debug ("(I) on_uevent done.\n");
}
static void
check_event_cb(int ret, LIBMTP_event_t event, uint32_t param1, void *user_data)
{
GVfsBackendMtp *backend = user_data;
g_debug ("(II) check_event_cb: %d, %d, %d\n", ret, event, param1);
backend->event_completed = TRUE;
if (ret != LIBMTP_HANDLER_RETURN_OK ||
g_atomic_int_get (&backend->unmount_started)) {
return;
}
EventData *data = g_new(EventData, 1);
data->event = event;
data->param1 = param1;
gboolean tret = g_thread_pool_push (backend->event_pool, data, NULL);
g_debug ("(II) check_event_cb push work to pool: %d\n", tret);
}
static gpointer
check_event (gpointer user_data)
{
GVfsBackendMtp *backend = user_data;
while (!g_atomic_int_get (&backend->unmount_started)) {
int ret;
LIBMTP_mtpdevice_t *device = backend->device;
if (backend->event_completed) {
g_debug ("(I) check_event: Read event needs to be issued.\n");
ret = LIBMTP_Read_Event_Async (device, check_event_cb, backend);
if (ret != 0) {
g_debug ("(I) check_event: Read_Event_Async failed: %d\n", ret);
}
backend->event_completed = FALSE;
}
/*
* Return from polling periodically to check for unmount.
*/
struct timeval tv = EVENT_POLL_PERIOD;
g_debug ("(I) check_event: Polling for events.\n");
ret = LIBMTP_Handle_Events_Timeout_Completed (&tv, &(backend->event_completed));
if (ret != 0) {
g_debug ("(I) check_event: polling returned error: %d\n", ret);
}
}
return NULL;
}
void
handle_event (EventData *ed, GVfsBackendMtp *backend)
{
LIBMTP_event_t event = ed->event;
uint32_t param1 = ed->param1;
g_free (ed);
g_mutex_lock (&backend->mutex);
if (!g_atomic_int_get (&backend->unmount_started)) {
switch (event) {
case LIBMTP_EVENT_STORE_ADDED:
{
LIBMTP_mtpdevice_t *device = backend->device;
LIBMTP_devicestorage_t *storage;
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
} else {
for (storage = device->storage; storage != 0; storage = storage->next) {
if (storage->id == param1) {
char *storage_name = create_storage_name (storage);
char *path = g_build_filename ("/", storage_name, NULL);
g_free (storage_name);
add_cache_entry (G_VFS_BACKEND_MTP (backend),
path,
storage->id,
-1);
g_hash_table_foreach (backend->monitors, emit_create_event, path);
}
}
}
break;
}
case LIBMTP_EVENT_OBJECT_REMOVED:
remove_cache_entry_by_id (G_VFS_BACKEND_MTP (backend), param1);
break;
case LIBMTP_EVENT_STORE_REMOVED:
{
/* Clear the cache entries and emit delete event; first for all
entries under the storage in question... */
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, backend->file_cache);
while (g_hash_table_iter_next (&iter, &key, &value)) {
const char *path = key;
const CacheEntry *entry = value;
if (entry->storage == param1) {
g_hash_table_foreach (backend->monitors,
emit_delete_event,
(char *)path);
g_hash_table_iter_remove (&iter);
}
}
/* ... and then for the storage itself */
remove_cache_entry_by_id (G_VFS_BACKEND_MTP (backend), param1);
break;
}
case LIBMTP_EVENT_OBJECT_ADDED:
{
LIBMTP_file_t *object = LIBMTP_Get_Filemetadata(backend->device, param1);
if (object) {
/* Obtain parent's path by searching cache by object's parent
ID; if the latter is zero, the object is located in storage's
root, otherwise, it is located in one of subfolders */
const char *parent_path = NULL;
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, backend->file_cache);
while (g_hash_table_iter_next (&iter, &key, &value)) {
const char *path = key;
const CacheEntry *entry = value;
if (object->parent_id != 0) {
if (object->parent_id == entry->id && object->storage_id == entry->storage) {
parent_path = path;
break;
}
} else {
if (entry->id == -1 && object->storage_id == entry->storage) {
parent_path = path;
break;
}
}
}
if (parent_path) {
/* Create path, add cache entry and emit create event(s) */
char *path = g_build_filename (parent_path, object->filename, NULL);
add_cache_entry (G_VFS_BACKEND_MTP (backend),
path,
object->storage_id,
object->item_id);
g_hash_table_foreach (backend->monitors, emit_create_event, path);
}
LIBMTP_destroy_file_t (object);
}
break;
}
default:
break;
}
}
g_mutex_unlock (&backend->mutex);
}
static gboolean
mtp_heartbeat (GVfsBackendMtp *backend)
{
if (g_mutex_trylock (&backend->mutex)) {
char *name = LIBMTP_Get_Friendlyname (backend->device);
if (name) {
free (name);
}
g_mutex_unlock (&backend->mutex);
}
return TRUE;
}
static char *
get_dev_path_and_device_from_host (GVfsJob *job,
GUdevClient *gudev_client,
const char *host,
uint32_t *bus_num,
uint32_t *dev_num,
GUdevDevice **device)
{
GList *devices, *l;
g_debug ("(II) get_dev_path_from_host: %s\n", host);
*device = NULL;
/* find corresponding GUdevDevice */
devices = g_udev_client_query_by_subsystem (gudev_client, "usb");
for (l = devices; l != NULL; l = l->next) {
const char *id = g_udev_device_get_property (l->data, "ID_SERIAL");
if (g_strcmp0 (id, host) == 0) {
*device = g_object_ref (l->data);
*bus_num = g_ascii_strtoull (g_udev_device_get_property (l->data, "BUSNUM"),
NULL, 10);
*dev_num = g_ascii_strtoull (g_udev_device_get_property (l->data, "DEVNUM"),
NULL, 10);
break;
}
}
g_list_free_full (devices, g_object_unref);
if (*device) {
return g_strdup_printf ("/dev/bus/usb/u/u", *bus_num, *dev_num);
}
/* For compatibility, handle old style host specifications. */
if (g_str_has_prefix (host, "[usb:")) {
/* Split [usb:001,002] into: '[usb', '001', '002', '' */
char **elements = g_strsplit_set (host, ":,]", -1);
if (g_strv_length (elements) == 4 && elements[3][0] == '\0') {
*bus_num = g_ascii_strtoull (elements[1], NULL, 10);
*dev_num = g_ascii_strtoull (elements[2], NULL, 10);
/* These values are non-zero, so zero means a parsing error. */
if (*bus_num != 0 && *dev_num != 0) {
char *dev_path = g_strdup_printf ("/dev/bus/usb/%s/%s",
elements[1], elements[2]);
*device = g_udev_client_query_by_device_file (gudev_client, dev_path);
if (*device) {
g_strfreev (elements);
return dev_path;
}
g_free (dev_path);
}
}
g_strfreev (elements);
}
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Couldn’t find matching udev device."));
return NULL;
}
static void
do_mount (GVfsBackend *backend,
GVfsJobMount *job,
GMountSpec *mount_spec,
GMountSource *mount_source,
gboolean is_automount)
{
GVfsBackendMtp *op_backend = G_VFS_BACKEND_MTP (backend);
GUdevDevice *device;
uint32_t bus_num, dev_num;
g_debug ("(I) do_mount\n");
const char *host = g_mount_spec_get (mount_spec, "host");
g_debug ("(I) do_mount: host=%s\n", host);
if (host == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job), G_IO_ERROR,
G_IO_ERROR_FAILED, _("No device specified"));
return;
}
const char *subsystems[] = {"usb", NULL};
op_backend->gudev_client = g_udev_client_new (subsystems);
if (op_backend->gudev_client == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job), G_IO_ERROR,
G_IO_ERROR_FAILED, _("Cannot create gudev client"));
return;
}
char *dev_path = get_dev_path_and_device_from_host (G_VFS_JOB (job),
op_backend->gudev_client,
host,
&bus_num,
&dev_num,
&device);
if (dev_path == NULL) {
g_object_unref (op_backend->gudev_client);
/* get_dev_path_from_host() sets job state. */
return;
}
op_backend->dev_path = dev_path;
op_backend->volume_name = g_vfs_get_volume_name (device, "ID_MTP");
op_backend->volume_icon = g_vfs_get_volume_icon (device);
op_backend->volume_symbolic_icon = g_vfs_get_volume_symbolic_icon (device);
g_object_unref (device);
LIBMTP_Init ();
get_device (backend, bus_num, dev_num, G_VFS_JOB (job));
if (!G_VFS_JOB (job)->failed) {
g_signal_connect (op_backend->gudev_client, "uevent", G_CALLBACK (on_uevent), op_backend);
op_backend->file_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
GMountSpec *mtp_mount_spec = g_mount_spec_new ("mtp");
g_mount_spec_set (mtp_mount_spec, "host", host);
g_vfs_backend_set_mount_spec (backend, mtp_mount_spec);
g_mount_spec_unref (mtp_mount_spec);
g_vfs_job_succeeded (G_VFS_JOB (job));
op_backend->hb_id =
g_timeout_add_seconds (900, (GSourceFunc)mtp_heartbeat, op_backend);
op_backend->event_completed = TRUE;
op_backend->event_thread = g_thread_new ("events", check_event, backend);
}
g_debug ("(I) do_mount done.\n");
}
static void
do_unmount (GVfsBackend *backend, GVfsJobUnmount *job,
GMountUnmountFlags flags,
GMountSource *mount_source)
{
GVfsBackendMtp *op_backend;
g_debug ("(I) do_umount\n");
op_backend = G_VFS_BACKEND_MTP (backend);
g_mutex_lock (&op_backend->mutex);
g_atomic_int_set (&op_backend->unmount_started, TRUE);
#if HAVE_LIBUSB
libusb_interrupt_event_handler (NULL);
#endif
/* Thread will terminate after flag is set. */
g_thread_join (op_backend->event_thread);
/* It's no longer safe to handle events. */
g_thread_pool_set_max_threads (op_backend->event_pool, 0, NULL);
/* Emit delete events to tell clients files are gone. */
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, op_backend->file_cache);
while (g_hash_table_iter_next (&iter, &key, &value)) {
const char *path = key;
g_hash_table_foreach (op_backend->monitors,
emit_delete_event,
(char *)path);
}
g_hash_table_unref (op_backend->file_cache);
g_source_remove (op_backend->hb_id);
g_signal_handlers_disconnect_by_func (op_backend->gudev_client, on_uevent, op_backend);
g_object_unref (op_backend->gudev_client);
g_clear_pointer (&op_backend->dev_path, g_free);
g_clear_pointer (&op_backend->volume_name, g_free);
g_clear_pointer (&op_backend->volume_icon, g_free);
g_clear_pointer (&op_backend->volume_symbolic_icon, g_free);
LIBMTP_Release_Device (op_backend->device);
g_mutex_unlock (&op_backend->mutex);
g_vfs_job_succeeded (G_VFS_JOB (job));
g_debug ("(I) do_umount done.\n");
}
/************************************************
* Queries
*
*/
/**
* get_device:
*
* Called with backend mutex lock held.
*/
LIBMTP_mtpdevice_t *
get_device (GVfsBackend *backend, uint32_t bus_num, uint32_t dev_num,
GVfsJob *job) {
g_debug ("(II) get_device: %u,%u\n", bus_num, dev_num);
LIBMTP_mtpdevice_t *device = NULL;
if (G_VFS_BACKEND_MTP (backend)->device != NULL) {
g_debug ("(II) get_device: Returning cached device %p\n", device);
return G_VFS_BACKEND_MTP (backend)->device;
}
LIBMTP_raw_device_t * rawdevices;
int numrawdevices;
LIBMTP_error_number_t err;
err = LIBMTP_Detect_Raw_Devices (&rawdevices, &numrawdevices);
switch (err) {
case LIBMTP_ERROR_NONE:
break;
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("No MTP devices found"));
goto exit;
case LIBMTP_ERROR_CONNECTING:
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED,
_("Unable to connect to MTP device"));
goto exit;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_FILE_ERROR, G_FILE_ERROR_NOMEM,
_("Unable to allocate memory while detecting MTP devices"));
goto exit;
case LIBMTP_ERROR_GENERAL:
default:
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_FAILED,
_("Generic libmtp error"));
goto exit;
}
/* Iterate over connected MTP devices */
int i;
for (i = 0; i < numrawdevices; i ) {
if (rawdevices[i].bus_location == bus_num &&
rawdevices[i].devnum == dev_num) {
device = LIBMTP_Open_Raw_Device_Uncached (&rawdevices[i]);
if (device == NULL) {
g_vfs_job_failed (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_FAILED,
_("Unable to open MTP device “u,u”"),
bus_num, dev_num);
goto exit;
}
g_debug ("(II) get_device: Storing device u,u\n", bus_num, dev_num);
G_VFS_BACKEND_MTP (backend)->device = device;
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
break;
}
}
if (device == NULL) {
g_vfs_job_failed (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Device not found"));
goto exit;
}
/* Check supported methods/extensions. */
LIBMTP_device_extension_t *extension;
for (extension = device->extensions; extension != NULL; extension = extension->next) {
if (g_strcmp0 ("android.com", extension->name) == 0) {
G_VFS_BACKEND_MTP (backend)->android_extension = TRUE;
break;
}
}
G_VFS_BACKEND_MTP (backend)->get_partial_object_capability
= LIBMTP_Check_Capability (device, LIBMTP_DEVICECAP_GetPartialObject);
#if HAVE_LIBMTP_1_1_15
G_VFS_BACKEND_MTP (backend)->move_object_capability
= LIBMTP_Check_Capability (device, LIBMTP_DEVICECAP_MoveObject);
G_VFS_BACKEND_MTP (backend)->copy_object_capability
= LIBMTP_Check_Capability (device, LIBMTP_DEVICECAP_CopyObject);
#endif
exit:
g_debug ("(II) get_device done.\n");
return device;
}
/**
* get_device_info:
*
* Called with backend mutex lock held.
*/
static void
get_device_info (GVfsBackendMtp *backend, GFileInfo *info)
{
LIBMTP_mtpdevice_t *device = backend->device;
const char *name;
name = g_mount_spec_get (g_vfs_backend_get_mount_spec (G_VFS_BACKEND (backend)), "host");
g_debug ("(II) get_device_info: %s\n", name);
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
g_file_info_set_name (info, name);
char *friendlyname = LIBMTP_Get_Friendlyname (device);
g_file_info_set_display_name (info, friendlyname == NULL ?
backend->volume_name : friendlyname);
free (friendlyname);
g_file_info_set_content_type (info, "inode/directory");
g_file_info_set_size (info, 0);
GIcon *icon = g_themed_icon_new (backend->volume_icon);
g_file_info_set_icon (info, icon);
g_object_unref (icon);
icon = g_themed_icon_new (backend->volume_symbolic_icon);
g_file_info_set_symbolic_icon (info, icon);
g_object_unref (icon);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, FALSE);
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, "mtpfs");
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE, FALSE);
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
g_debug ("(II) get_device_info done with no stores.\n");
return;
}
guint64 freeSpace = 0;
guint64 maxSpace = 0;
LIBMTP_devicestorage_t *storage;
for (storage = device->storage; storage != 0; storage = storage->next) {
freeSpace = storage->FreeSpaceInBytes;
maxSpace = storage->MaxCapacity;
}
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, freeSpace);
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, maxSpace);
g_debug ("(II) get_device_info done.\n");
}
/**
* get_storage_info:
*
* Called with backend mutex lock held.
*/
static void
get_storage_info (LIBMTP_devicestorage_t *storage, GFileInfo *info) {
g_debug ("(II) get_storage_info: %X\n", storage->id);
char *storage_name = create_storage_name(storage);
g_file_info_set_name (info, storage_name);
g_file_info_set_display_name (info, storage_name);
g_free(storage_name);
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
g_file_info_set_content_type (info, "inode/directory");
g_file_info_set_size (info, 0);
GIcon *icon, *symbolic_icon;
switch (storage->StorageType) {
case PTP_ST_FixedROM:
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk");
symbolic_icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk-symbolic");
break;
case PTP_ST_RemovableROM:
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
icon = g_themed_icon_new_with_default_fallbacks ("media-flash-sd");
symbolic_icon = g_themed_icon_new_with_default_fallbacks ("media-flash-sd-symbolic");
break;
case PTP_ST_RemovableRAM:
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, FALSE);
icon = g_themed_icon_new_with_default_fallbacks ("media-flash-sd");
symbolic_icon = g_themed_icon_new_with_default_fallbacks ("media-flash-sd-symbolic");
break;
case PTP_ST_FixedRAM:
default:
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, FALSE);
icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk");
symbolic_icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk-symbolic");
break;
}
g_file_info_set_icon (info, icon);
g_file_info_set_symbolic_icon (info, symbolic_icon);
g_object_unref (icon);
g_object_unref (symbolic_icon);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, FALSE);
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, storage->FreeSpaceInBytes);
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, storage->MaxCapacity);
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, "mtpfs");
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE, FALSE);
g_debug ("(II) get_storage_info done.\n");
}
/**
* get_file_info:
*
* Called with backend mutex lock held.
*/
static void
get_file_info (GVfsBackend *backend,
LIBMTP_mtpdevice_t *device,
GFileInfo *info,
LIBMTP_file_t *file) {
GIcon *icon = NULL;
GIcon *symbolic_icon = NULL;
char *content_type = NULL;
char *mount_id = NULL;
char *file_id = NULL;
g_debug ("(II) get_file_info: %X\n", file->item_id);
g_file_info_set_name (info, file->filename);
g_file_info_set_display_name (info, file->filename);
mount_id = g_mount_spec_to_string (g_vfs_backend_get_mount_spec (G_VFS_BACKEND (backend)));
file_id = g_strdup_printf ("%s:%d", mount_id, file->item_id);
g_free (mount_id);
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, file_id);
g_free (file_id);
switch (file->filetype) {
case LIBMTP_FILETYPE_FOLDER:
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, TRUE);
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
content_type = g_strdup ("inode/directory");
break;
default:
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, FALSE);
g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR);
content_type = g_content_type_guess (file->filename, NULL, 0, NULL);
break;
}
g_file_info_set_content_type (info, content_type);
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
content_type);
icon = g_content_type_get_icon (content_type);
symbolic_icon = g_content_type_get_symbolic_icon (content_type);
if (LIBMTP_FILETYPE_IS_IMAGE (file->filetype) ||
LIBMTP_FILETYPE_IS_VIDEO (file->filetype) ||
LIBMTP_FILETYPE_IS_AUDIOVIDEO (file->filetype)) {
GIcon *preview;
char *icon_id;
GMountSpec *mount_spec;
mount_spec = g_vfs_backend_get_mount_spec (backend);
icon_id = g_strdup_printf ("%X", file->item_id);
preview = g_vfs_icon_new (mount_spec,
icon_id);
g_file_info_set_attribute_object (info,
G_FILE_ATTRIBUTE_PREVIEW_ICON,
G_OBJECT (preview));
g_object_unref (preview);
g_free (icon_id);
}
g_file_info_set_size (info, file->filesize);
GTimeVal modtime = { file->modificationdate, 0 };
g_file_info_set_modification_time (info, &modtime);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, TRUE);
if (icon != NULL) {
g_file_info_set_icon (info, icon);
g_object_unref (icon);
}
if (symbolic_icon != NULL) {
g_file_info_set_symbolic_icon (info, symbolic_icon);
g_object_unref (symbolic_icon);
}
g_free (content_type);
g_debug ("(II) get_file_info done.\n");
}
static void
do_enumerate (GVfsBackend *backend,
GVfsJobEnumerate *job,
const char *filename,
GFileAttributeMatcher *attribute_matcher,
GFileQueryInfoFlags flags)
{
GVfsBackendMtp *op_backend = G_VFS_BACKEND_MTP (backend);
GFileInfo *info;
gchar **elements = g_strsplit_set (filename, "/", -1);
unsigned int ne = g_strv_length (elements);
g_debug ("(I) do_enumerate (filename = %s, n_elements = %d)\n", filename, ne);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
LIBMTP_mtpdevice_t *device;
device = op_backend->device;
if (ne == 2 && elements[1][0] == '\0') {
LIBMTP_devicestorage_t *storage;
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
goto success;
}
for (storage = device->storage; storage != 0; storage = storage->next) {
info = g_file_info_new ();
get_storage_info (storage, info);
g_vfs_job_enumerate_add_info (job, info);
g_object_unref (info);
char *storage_name = create_storage_name (storage);
add_cache_entry (G_VFS_BACKEND_MTP (backend),
g_build_filename (filename, storage_name, NULL),
storage->id,
-1);
g_free (storage_name);
}
} else {
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend),
filename);
if (entry == NULL) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File not found"));
goto exit;
}
/* Invalidate existing cache entries in case any are stale. */
char *remove_prefix = g_strdup_printf("%s/", filename);
remove_cache_entry (G_VFS_BACKEND_MTP (backend), remove_prefix);
g_free (remove_prefix);
LIBMTP_file_t *files;
LIBMTP_Clear_Errorstack (device);
files = LIBMTP_Get_Files_And_Folders (device, entry->storage, entry->id);
if (files == NULL && LIBMTP_Get_Errorstack (device) != NULL) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
while (files != NULL) {
LIBMTP_file_t *file = files;
files = files->next;
info = g_file_info_new ();
get_file_info (backend, device, info, file);
g_vfs_job_enumerate_add_info (job, info);
g_object_unref (info);
add_cache_entry (G_VFS_BACKEND_MTP (backend),
g_build_filename (filename, file->filename, NULL),
file->storage_id,
file->item_id);
LIBMTP_destroy_file_t (file);
}
}
success:
g_vfs_job_enumerate_done (job);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_strfreev (elements);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_enumerate done.\n");
}
static void
do_query_info (GVfsBackend *backend,
GVfsJobQueryInfo *job,
const char *filename,
GFileQueryInfoFlags flags,
GFileInfo *info,
GFileAttributeMatcher *matcher)
{
g_debug ("(I) do_query_info (filename = %s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
gchar **elements = g_strsplit_set (filename, "/", -1);
unsigned int ne = g_strv_length (elements);
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
if (ne == 2 && elements[1][0] == '\0') {
get_device_info (G_VFS_BACKEND_MTP (backend), info);
} else if (ne < 3) {
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend),
filename);
if (!entry) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
LIBMTP_devicestorage_t *storage;
gboolean found = FALSE;
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
for (storage = device->storage; storage != 0; storage = storage->next) {
if (storage->id == entry->storage) {
g_debug ("(I) found storage %X\n", storage->id);
found = TRUE;
get_storage_info (storage, info);
break;
}
}
if (!found) {
g_debug ("(W) storage %X not found?!\n", entry->storage);
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
} else {
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend),
filename);
if (!entry) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File not found"));
goto exit;
}
LIBMTP_file_t *file = NULL;
file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file != NULL) {
get_file_info (backend, device, info, file);
LIBMTP_destroy_file_t (file);
} else {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
}
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_strfreev (elements);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_query_info done.\n");
}
static void
do_query_fs_info (GVfsBackend *backend,
GVfsJobQueryFsInfo *job,
const char *filename,
GFileInfo *info,
GFileAttributeMatcher *attribute_matcher)
{
g_debug ("(I) do_query_fs_info (filename = %s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
gchar **elements = g_strsplit_set (filename, "/", -1);
unsigned int ne = g_strv_length (elements);
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
if (ne == 2 && elements[1][0] == '\0') {
get_device_info (G_VFS_BACKEND_MTP (backend), info);
} else {
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend),
filename);
if (entry == NULL) {
LIBMTP_Dump_Errorstack (device);
LIBMTP_Clear_Errorstack (device);
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File not found"));
goto exit;
}
LIBMTP_devicestorage_t *storage;
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
for (storage = device->storage; storage != 0; storage = storage->next) {
if (storage->id == entry->storage) {
get_storage_info (storage, info);
}
}
}
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_strfreev (elements);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_query_fs_info done.\n");
}
/************************************************
* Operations
*
*/
typedef struct {
GFileProgressCallback progress_callback;
gpointer progress_callback_data;
GVfsJob *job;
} MtpProgressData;
static int
mtp_progress (uint64_t const sent, uint64_t const total,
MtpProgressData const * const data)
{
if (data->progress_callback) {
data->progress_callback (sent, total, data->progress_callback_data);
}
return g_vfs_job_is_cancelled (data->job);
}
/**
* Validate whether a given combination of source and destination
* are valid for copying/moving. If not valid, set the appropriate
* error on the job.
*/
static gboolean
validate_source_and_dest (gboolean dest_exists,
gboolean dest_is_dir,
gboolean source_is_dir,
gboolean source_can_be_dir,
GFileCopyFlags flags,
GVfsJob *job)
{
/* Test all the GIO defined failure conditions */
if (dest_exists) {
if (flags & G_FILE_COPY_OVERWRITE) {
if (!source_is_dir && dest_is_dir) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
_("Target is a directory"));
return FALSE;
} else if (source_is_dir && dest_is_dir) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
_("Can’t merge directories"));
return FALSE;
} else if (source_is_dir && !dest_is_dir) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
_("Can’t recursively copy directory"));
return FALSE;
}
/* Source can overwrite dest as both are files */
return TRUE;
} else {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_EXISTS,
_("Target file already exists"));
return FALSE;
}
} else if (source_is_dir && !source_can_be_dir) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
_("Can’t recursively copy directory"));
return FALSE;
}
/* Source is valid and dest doesn't exist */
return TRUE;
}
static void
do_make_directory (GVfsBackend *backend,
GVfsJobMakeDirectory *job,
const char *filename)
{
g_debug ("(I) do_make_directory (filename = %s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
char *dir_name = g_path_get_dirname (filename);
char *base_name = g_path_get_basename (filename);
gchar **elements = g_strsplit_set (filename, "/", -1);
unsigned int ne = g_strv_length (elements);
if (ne < 3) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Cannot make directory in this location"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), filename);
if (entry != NULL && entry->id != -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_EXISTS,
_("Target file already exists"));
goto exit;
}
entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), dir_name);
if (!entry) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
int ret = LIBMTP_Create_Folder (device, base_name, entry->id, entry->storage);
if (ret == 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors, emit_create_event, (char *)filename);
exit:
g_strfreev (elements);
g_free (dir_name);
g_free (base_name);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_make_directory done.\n");
}
static void
do_pull (GVfsBackend *backend,
GVfsJobPull *job,
const char *source,
const char *local_path,
GFileCopyFlags flags,
gboolean remove_source,
GFileProgressCallback progress_callback,
gpointer progress_callback_data)
{
g_debug ("(I) do_pull (filename = %s, local_path = %s)\n", source, local_path);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
GFile *local_file = NULL;
GFileInfo *local_info = NULL;
GFileInfo *info = NULL;
guint64 mtime;
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), source);
if (entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
_("Not a regular file"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
}
info = g_file_info_new ();
get_file_info (backend, device, info, file);
LIBMTP_destroy_file_t (file);
local_file = g_file_new_for_path (local_path);
gboolean source_is_dir =
g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
GError *error = NULL;
local_info =
g_file_query_info (local_file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
0, G_VFS_JOB (job)->cancellable, &error);
gboolean dest_exists = local_info || error->code != G_IO_ERROR_NOT_FOUND;
if (!local_info && error->code != G_IO_ERROR_NOT_FOUND) {
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
goto exit;
} else if (error != NULL) {
g_error_free (error);
}
gboolean dest_is_dir = dest_exists &&
g_file_info_get_file_type (local_info) == G_FILE_TYPE_DIRECTORY;
gboolean valid_pull = validate_source_and_dest (dest_exists,
dest_is_dir,
source_is_dir,
FALSE, // source_can_be_dir
flags,
G_VFS_JOB (job));
if (!valid_pull) {
goto exit;
} else if (dest_exists) {
/* Source and Dest are files */
g_debug ("(I) Removing destination.\n");
GError *error = NULL;
gboolean ret = g_file_delete (local_file,
G_VFS_JOB (job)->cancellable,
&error);
if (!ret) {
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
goto exit;
}
}
MtpProgressData mtp_progress_data;
mtp_progress_data.progress_callback = progress_callback;
mtp_progress_data.progress_callback_data = progress_callback_data;
mtp_progress_data.job = G_VFS_JOB (job);
int ret = LIBMTP_Get_File_To_File (device,
entry->id,
local_path,
(LIBMTP_progressfunc_t)mtp_progress,
&mtp_progress_data);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
/* Ignore errors here. Failure to copy metadata is not a hard error */
mtime = g_file_info_get_attribute_uint64 (info,
G_FILE_ATTRIBUTE_TIME_MODIFIED);
g_file_set_attribute_uint64 (local_file,
G_FILE_ATTRIBUTE_TIME_MODIFIED, mtime,
G_FILE_QUERY_INFO_NONE,
G_VFS_JOB (job)->cancellable, NULL);
g_file_set_attribute_uint32 (local_file,
G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, 0,
G_FILE_QUERY_INFO_NONE,
G_VFS_JOB (job)->cancellable, NULL);
/* Attempt to delete object if requested but don't fail it it fails. */
if (remove_source) {
g_debug ("(I) Removing source.\n");
LIBMTP_Delete_Object (device, entry->id);
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)source);
remove_cache_entry (G_VFS_BACKEND_MTP (backend),
source);
}
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_clear_object (&local_info);
g_clear_object (&local_file);
g_clear_object (&info);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_pull done.\n");
}
static LIBMTP_filetype_t
get_filetype_from_info (GFileInfo *info) {
const char *content_type = g_file_info_get_content_type (info);
g_debug ("(II) get_filetype_from_info (info = %s)\n", content_type);
LIBMTP_filetype_t ret;
if (!content_type) {
ret = LIBMTP_FILETYPE_UNKNOWN;
} else if (strcmp (content_type, "audio/wav") == 0 ||
strcmp (content_type, "audio/x-wav") == 0 ||
strcmp (content_type, "audio/vnd.wave") == 0) {
ret = LIBMTP_FILETYPE_WAV;
} else if (strcmp (content_type, "audio/mpeg") == 0 ||
strcmp (content_type, "audio/x-mp3") == 0 ||
strcmp (content_type, "audio/x-mpeg") == 0 ||
strcmp (content_type, "audio/mp3") == 0) {
ret = LIBMTP_FILETYPE_MP3;
} else if (strcmp (content_type, "audio/x-ms-wma") == 0 ||
strcmp (content_type, "audio/wma") == 0) {
ret = LIBMTP_FILETYPE_WMA;
} else if (strcmp (content_type, "audio/ogg") == 0 ||
strcmp (content_type, "audio/x-ogg") == 0) {
ret = LIBMTP_FILETYPE_OGG;
} else if (strcmp (content_type, "audio/audible") == 0 ||
strcmp (content_type, "audio/x-pn-audibleaudio") == 0) {
ret = LIBMTP_FILETYPE_AUDIBLE;
} else if (strcmp (content_type, "video/mp4") == 0 ||
strcmp (content_type, "video/x-m4v") == 0 ||
strcmp (content_type, "video/mp4v-es") == 0) {
ret = LIBMTP_FILETYPE_MP4;
} else if (strcmp (content_type, "video/x-ms-wmv") == 0) {
ret = LIBMTP_FILETYPE_WMV;
} else if (strcmp (content_type, "video/x-msvideo") == 0 ||
strcmp (content_type, "video/x-avi") == 0 ||
strcmp (content_type, "video/avi") == 0 ||
strcmp (content_type, "video/divx") == 0 ||
strcmp (content_type, "video/msvideo") == 0 ||
strcmp (content_type, "video/vnd.divx") == 0) {
ret = LIBMTP_FILETYPE_AVI;
} else if (strcmp (content_type, "video/mpeg") == 0 ||
strcmp (content_type, "video/x-mpeg") == 0 ||
strcmp (content_type, "video/x-mpeg2") == 0) {
ret = LIBMTP_FILETYPE_MPEG;
} else if (strcmp (content_type, "video/x-ms-asf") == 0 ||
strcmp (content_type, "video/x-ms-wm") == 0 ||
strcmp (content_type, "video/vnd.ms-asf") == 0) {
ret = LIBMTP_FILETYPE_ASF;
} else if (strcmp (content_type, "video/quicktime") == 0) {
ret = LIBMTP_FILETYPE_QT;
} else if (strcmp (content_type, "image/jpeg") == 0 ||
strcmp (content_type, "image/pjpeg") == 0) {
ret = LIBMTP_FILETYPE_JPEG;
} else if (strcmp (content_type, "image/tiff") == 0) {
ret = LIBMTP_FILETYPE_TIFF;
} else if (strcmp (content_type, "image/bmp") == 0 ||
strcmp (content_type, "image/x-bmp") == 0 ||
strcmp (content_type, "image/x-MS-bmp") == 0) {
ret = LIBMTP_FILETYPE_BMP;
} else if (strcmp (content_type, "image/gif") == 0) {
ret = LIBMTP_FILETYPE_GIF;
} else if (strcmp (content_type, "image/x-pict") == 0) {
ret = LIBMTP_FILETYPE_PICT;
} else if (strcmp (content_type, "image/png") == 0) {
ret = LIBMTP_FILETYPE_PNG;
} else if (strcmp (content_type, "text/x-vcalendar") == 0) {
ret = LIBMTP_FILETYPE_VCALENDAR1;
} else if (strcmp (content_type, "text/calendar") == 0 ||
strcmp (content_type, "application/ics") == 0) {
ret = LIBMTP_FILETYPE_VCALENDAR2;
} else if (strcmp (content_type, "text/x-vcard") == 0 ||
strcmp (content_type, "text/directory") == 0) {
ret = LIBMTP_FILETYPE_VCARD2;
} else if (strcmp (content_type, "text/vcard") == 0) {
ret = LIBMTP_FILETYPE_VCARD3;
} else if (strcmp (content_type, "image/x-wmf") == 0 ||
strcmp (content_type, "image/wmf") == 0 ||
strcmp (content_type, "image/x-win-metafile") == 0 ||
strcmp (content_type, "application/x-wmf") == 0 ||
strcmp (content_type, "application/wmf") == 0 ||
strcmp (content_type, "application/x-msmetafile") == 0) {
ret = LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT;
} else if (strcmp (content_type, "application/x-ms-dos-executable") == 0) {
ret = LIBMTP_FILETYPE_WINEXEC;
} else if (strcmp (content_type, "text/plain") == 0) {
ret = LIBMTP_FILETYPE_TEXT;
} else if (strcmp (content_type, "text/html") == 0) {
ret = LIBMTP_FILETYPE_HTML;
} else if (strcmp (content_type, "audio/aac") == 0) {
ret = LIBMTP_FILETYPE_AAC;
} else if (strcmp (content_type, "audio/flac") == 0 ||
strcmp (content_type, "audio/x-flac") == 0 ||
strcmp (content_type, "audio/x-flac ogg") == 0 ||
strcmp (content_type, "audio/x-oggflac") == 0) {
ret = LIBMTP_FILETYPE_FLAC;
} else if (strcmp (content_type, "audio/mp2") == 0 ||
strcmp (content_type, "audio/x-mp2") == 0) {
ret = LIBMTP_FILETYPE_MP2;
} else if (strcmp (content_type, "audio/mp4") == 0 ||
strcmp (content_type, "audio/x-m4a") == 0) {
ret = LIBMTP_FILETYPE_M4A;
} else if (strcmp (content_type, "application/msword") == 0 ||
strcmp (content_type, "application/vnd.ms-word") == 0 ||
strcmp (content_type, "application/x-msword") == 0 ||
strcmp (content_type, "zz-application/zz-winassoc-doc") == 0) {
ret = LIBMTP_FILETYPE_DOC;
} else if (strcmp (content_type, "text/xml") == 0 ||
strcmp (content_type, "application/xml") == 0) {
ret = LIBMTP_FILETYPE_XML;
} else if (strcmp (content_type, "application/msexcel") == 0 ||
strcmp (content_type, "application/vnd.ms-excel") == 0 ||
strcmp (content_type, "application/x-msexcel") == 0 ||
strcmp (content_type, "zz-application/zz-winassoc-xls") == 0) {
ret = LIBMTP_FILETYPE_XLS;
} else if (strcmp (content_type, "application/mspowerpoint") == 0 ||
strcmp (content_type, "application/vnd.ms-powerpoint") == 0 ||
strcmp (content_type, "application/x-mspowerpoint") == 0 ||
strcmp (content_type, "application/powerpoint") == 0) {
ret = LIBMTP_FILETYPE_PPT;
} else if (strcmp (content_type, "message/rfc822") == 0) {
ret = LIBMTP_FILETYPE_MHT;
} else if (strcmp (content_type, "image/jp2") == 0) {
ret = LIBMTP_FILETYPE_JP2;
} else if (strcmp (content_type, "image/jpx") == 0) {
ret = LIBMTP_FILETYPE_JPX;
} else if (strcmp (content_type, "audio/x-mpegurl") == 0 ||
strcmp (content_type, "audio/mpegurl") == 0 ||
strcmp (content_type, "application/m3u") == 0 ||
strcmp (content_type, "audio/x-mp3-playlist") == 0 ||
strcmp (content_type, "audio/m3u") == 0 ||
strcmp (content_type, "audio/x-m3u") == 0) {
ret = LIBMTP_FILETYPE_PLAYLIST;
} else if (strncmp (content_type, "audio/", 6) == 0) {
// Must come after all other audio types.
ret = LIBMTP_FILETYPE_UNDEF_AUDIO;
} else if (strncmp (content_type, "video/", 6) == 0) {
// Must come after all other video types.
ret = LIBMTP_FILETYPE_UNDEF_VIDEO;
} else {
ret = LIBMTP_FILETYPE_UNKNOWN;
}
/* Unmappable Types
LIBMTP_FILETYPE_JFIF,
LIBMTP_FILETYPE_FIRMWARE,
LIBMTP_FILETYPE_MEDIACARD,
LIBMTP_FILETYPE_ALBUM,
*/
g_debug ("(II) get_filetype_from_info done.\n");
return ret;
}
static void
do_push (GVfsBackend *backend,
GVfsJobPush *job,
const char *destination,
const char *local_path,
GFileCopyFlags flags,
gboolean remove_source,
GFileProgressCallback progress_callback,
gpointer progress_callback_data)
{
g_debug ("(I) do_push (filename = %s, local_path = %s)\n", destination, local_path);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
char *dir_name = g_path_get_dirname (destination);
char *filename = g_path_get_basename (destination);
GFile *local_file = NULL;
GFileInfo *info = NULL;
gchar **elements = g_strsplit_set (destination, "/", -1);
unsigned int ne = g_strv_length (elements);
if (ne < 3) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Cannot write to this location"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), destination);
gboolean dest_exists = entry != NULL && entry->id != -1;
gboolean dest_is_dir = FALSE;
if (dest_exists) {
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file != NULL) {
dest_is_dir = file->filetype == LIBMTP_FILETYPE_FOLDER;
LIBMTP_destroy_file_t (file);
}
}
CacheEntry *parent = get_cache_entry (G_VFS_BACKEND_MTP (backend), dir_name);
if (!parent) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
GError *error = NULL;
local_file = g_file_new_for_path (local_path);
info =
g_file_query_info (local_file,
G_FILE_ATTRIBUTE_STANDARD_TYPE","
G_FILE_ATTRIBUTE_STANDARD_SIZE","
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
0, G_VFS_JOB (job)->cancellable, &error);
if (!info) {
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
goto exit;
}
gboolean source_is_dir =
g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
gboolean valid_push = validate_source_and_dest (dest_exists,
dest_is_dir,
source_is_dir,
FALSE, // source_can_be_dir
flags,
G_VFS_JOB (job));
if (!valid_push) {
goto exit;
} else if (dest_exists) {
/* Source and Dest are files */
g_debug ("(I) Removing destination.\n");
int ret = LIBMTP_Delete_Object (device, entry->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)destination);
remove_cache_entry (G_VFS_BACKEND_MTP (backend),
destination);
}
LIBMTP_file_t *mtpfile = LIBMTP_new_file_t ();
mtpfile->filename = strdup (filename);
mtpfile->parent_id = parent->id;
mtpfile->storage_id = parent->storage;
mtpfile->filetype = get_filetype_from_info (info);
mtpfile->filesize = g_file_info_get_size (info);
MtpProgressData mtp_progress_data;
mtp_progress_data.progress_callback = progress_callback;
mtp_progress_data.progress_callback_data = progress_callback_data;
mtp_progress_data.job = G_VFS_JOB (job);
int ret = LIBMTP_Send_File_From_File (device, local_path, mtpfile,
(LIBMTP_progressfunc_t)mtp_progress,
&mtp_progress_data);
LIBMTP_destroy_file_t (mtpfile);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
/* Attempt to delete object if requested but don't fail it it fails. */
if (remove_source) {
g_debug ("(I) Removing source.\n");
g_file_delete (local_file, G_VFS_JOB (job)->cancellable, NULL);
}
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_create_event,
(char *)destination);
exit:
g_clear_object (&local_file);
g_clear_object (&info);
g_strfreev (elements);
g_free (dir_name);
g_free (filename);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_push done.\n");
}
static void
do_delete (GVfsBackend *backend,
GVfsJobDelete *job,
const char *filename)
{
g_debug ("(I) do_delete (filename = %s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), filename);
if (entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
LIBMTP_file_t *files;
LIBMTP_Clear_Errorstack (device);
files = LIBMTP_Get_Files_And_Folders (device, entry->storage, entry->id);
if (LIBMTP_Get_Errorstack (device) != NULL) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
if (files != NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_EMPTY,
g_strerror (ENOTEMPTY));
g_debug ("(II) Directory size %" G_GSIZE_FORMAT "\n", file->filesize);
goto exit;
}
}
int ret = LIBMTP_Delete_Object (device, entry->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)filename);
remove_cache_entry (G_VFS_BACKEND_MTP (backend),
filename);
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_delete done.\n");
}
static void
do_set_display_name (GVfsBackend *backend,
GVfsJobSetDisplayName *job,
const char *filename,
const char *display_name)
{
g_debug ("(I) do_set_display_name '%s' --> '%s'\n", filename, display_name);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), filename);
if (entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
int ret = LIBMTP_Set_File_Name (device, file, display_name);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
char *dir_name = g_path_get_dirname (filename);
char *new_name = g_build_filename (dir_name, display_name, NULL);
remove_cache_entry (G_VFS_BACKEND_MTP (backend),
filename);
add_cache_entry (G_VFS_BACKEND_MTP (backend), new_name, file->storage_id, file->item_id);
LIBMTP_destroy_file_t (file);
file = NULL;
g_vfs_job_set_display_name_set_new_path (job, new_name);
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_create_event,
(char *)new_name);
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)filename);
g_free (dir_name);
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_set_display_name done.\n");
}
static void
do_open_for_read (GVfsBackend *backend,
GVfsJobOpenForRead *job,
const char *filename)
{
if (!G_VFS_BACKEND_MTP (backend)->android_extension &&
!G_VFS_BACKEND_MTP (backend)->get_partial_object_capability) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
return;
}
g_debug ("(I) do_open_for_read (%s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), filename);
if (entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file == NULL) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
LIBMTP_destroy_file_t (file);
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
_("Can’t open directory"));
goto exit;
}
RWHandle *handle = g_new0(RWHandle, 1);
handle->handle_type = HANDLE_FILE;
handle->id = entry->id;
handle->offset = 0;
handle->size = file->filesize;
LIBMTP_destroy_file_t (file);
g_vfs_job_open_for_read_set_can_seek (G_VFS_JOB_OPEN_FOR_READ (job), TRUE);
g_vfs_job_open_for_read_set_handle (G_VFS_JOB_OPEN_FOR_READ (job), handle);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_open_for_read done.\n");
}
static void
do_open_icon_for_read (GVfsBackend *backend,
GVfsJobOpenIconForRead *job,
const char *icon_id)
{
g_debug ("(I) do_open_icon_for_read (%s)\n", icon_id);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
guint id = strtol (icon_id, NULL, 16);
if (id > 0) {
GByteArray *bytes = NULL;
unsigned char *data;
unsigned int size;
int ret;
ret = LIBMTP_Get_Thumbnail (G_VFS_BACKEND_MTP (backend)->device, id,
&data, &size);
if (ret == 0) {
g_debug ("File %X has thumbnail: %u\n", id, size);
if (size > 0) {
bytes = g_byte_array_sized_new (size);
g_byte_array_append (bytes, data, size);
}
free (data);
}
if (!bytes) {
LIBMTP_filesampledata_t *sample_data = LIBMTP_new_filesampledata_t ();
ret = LIBMTP_Get_Representative_Sample (G_VFS_BACKEND_MTP (backend)->device,
id, sample_data);
if (ret == 0) {
g_debug ("File %X has sampledata: %" G_GSIZE_FORMAT "\n", id, sample_data->size);
if (sample_data->size > 0) {
bytes = g_byte_array_sized_new (sample_data->size);
g_byte_array_append (bytes, (const guint8 *)sample_data->data, sample_data->size);
size = sample_data->size;
}
}
LIBMTP_destroy_filesampledata_t (sample_data);
}
if (!bytes) {
g_debug ("File %X has no thumbnail or sampledata\n", id);
g_vfs_job_failed (G_VFS_JOB (job),
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
_("No thumbnail for entity “%s”"),
icon_id);
goto exit;
}
RWHandle *handle = g_new0(RWHandle, 1);
handle->handle_type = HANDLE_PREVIEW;
handle->id = id;
handle->offset = 0;
handle->size = size;
handle->bytes = bytes;
g_vfs_job_open_for_read_set_can_seek (G_VFS_JOB_OPEN_FOR_READ (job), TRUE);
g_vfs_job_open_for_read_set_handle (G_VFS_JOB_OPEN_FOR_READ (job), handle);
g_vfs_job_succeeded (G_VFS_JOB (job));
} else {
g_vfs_job_failed (G_VFS_JOB (job),
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Malformed icon identifier “%s”"),
icon_id);
goto exit;
}
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_open_icon_for_read done.\n");
}
static void
do_seek_on_read (GVfsBackend *backend,
GVfsJobSeekRead *job,
GVfsBackendHandle opaque_handle,
goffset offset,
GSeekType type)
{
RWHandle *handle = opaque_handle;
uint32_t id = handle->id;
goffset old_offset = handle->offset;
gsize size = handle->size;
g_debug ("(I) do_seek_on_read (%X %lu %ld %u)\n", id, old_offset, offset, type);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
if (type == G_SEEK_END) {
offset = size offset;
} else if (type == G_SEEK_CUR) {
offset = old_offset;
}
if (offset < 0) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("End of stream"));
goto exit;
}
handle->offset = offset;
g_vfs_job_seek_read_set_offset (job, offset);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_seek_on_read done. (%lu)\n", offset);
}
static void
do_read (GVfsBackend *backend,
GVfsJobRead *job,
GVfsBackendHandle opaque_handle,
char *buffer,
gsize bytes_requested)
{
RWHandle *handle = opaque_handle;
uint32_t id = handle->id;
goffset offset = handle->offset;
g_debug ("(I) do_read (%X %lu %lu)\n", id, offset, bytes_requested);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
uint32_t actual;
if (handle->handle_type == HANDLE_FILE) {
if (!G_VFS_BACKEND_MTP (backend)->android_extension &&
offset > G_MAXUINT32) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
goto exit;
}
/*
* Almost all android devices have a bug where they do not enforce
* POSIX semantics for read past EOF, leading to undefined
* behaviour including device-side hangs. We'd better handle it
* here.
*/
if (offset >= handle->size) {
g_debug ("(II) skipping read with offset past EOF\n");
actual = 0;
goto finished;
} else if (offset bytes_requested > handle->size) {
g_debug ("(II) reducing bytes_requested to avoid reading past EOF\n");
bytes_requested = handle->size - offset;
}
unsigned char *temp;
int ret = LIBMTP_GetPartialObject (G_VFS_BACKEND_MTP (backend)->device, id, offset,
bytes_requested, &temp, &actual);
if (ret != 0) {
fail_job (G_VFS_JOB (job), G_VFS_BACKEND_MTP (backend)->device);
g_debug ("(I) job failed.\n");
goto exit;
}
memcpy (buffer, temp, actual);
free (temp);
} else {
GByteArray *bytes = handle->bytes;
actual = MIN (bytes->len - offset, bytes_requested);
memcpy (buffer, bytes->data offset, actual);
}
finished:
handle->offset = offset actual;
g_vfs_job_read_set_size (job, actual);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_read done.\n");
}
static void
do_close_read (GVfsBackend *backend,
GVfsJobCloseRead *job,
GVfsBackendHandle opaque_handle)
{
g_debug ("(I) do_close_read\n");
RWHandle *handle = opaque_handle;
if (handle->bytes) {
g_byte_array_unref (handle->bytes);
}
g_free(handle);
g_vfs_job_succeeded (G_VFS_JOB (job));
g_debug ("(I) do_close_read done.\n");
}
static uint16_t
zero_get_func (void* params,
void* priv,
uint32_t wantlen,
unsigned char *data,
uint32_t *gotlen)
{
*gotlen = 0;
return LIBMTP_HANDLER_RETURN_OK;
}
static void
do_create (GVfsBackend *backend,
GVfsJobOpenForWrite *job,
const char *filename,
GFileCreateFlags flags)
{
if (!G_VFS_BACKEND_MTP (backend)->android_extension) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
return;
}
g_debug ("(I) do_create (%s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
char *dir_name = g_path_get_dirname (filename);
char *basename = g_path_get_basename (filename);
gchar **elements = g_strsplit_set (filename, "/", -1);
unsigned int ne = g_strv_length (elements);
if (ne < 3) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Cannot write to this location"));
goto exit;
}
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), dir_name);
if (!entry) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *mtpfile = LIBMTP_new_file_t ();
mtpfile->filename = strdup (basename);
mtpfile->parent_id = entry->id;
mtpfile->storage_id = entry->storage;
mtpfile->filetype = LIBMTP_FILETYPE_UNKNOWN;
mtpfile->filesize = 0;
int ret = LIBMTP_Send_File_From_Handler (device, zero_get_func, NULL,
mtpfile, NULL, NULL);
uint32_t id = mtpfile->item_id;
LIBMTP_destroy_file_t (mtpfile);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to create empty file.\n");
goto exit;
}
ret = LIBMTP_BeginEditObject (device, id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to begin edit.\n");
goto exit;
}
RWHandle *handle = g_new0(RWHandle, 1);
handle->handle_type = HANDLE_FILE;
handle->id = id;
handle->offset = 0;
handle->size = 0;
g_vfs_job_open_for_write_set_can_seek (G_VFS_JOB_OPEN_FOR_WRITE (job), TRUE);
g_vfs_job_open_for_write_set_can_truncate (G_VFS_JOB_OPEN_FOR_WRITE (job), TRUE);
g_vfs_job_open_for_write_set_handle (G_VFS_JOB_OPEN_FOR_WRITE (job), handle);
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_create_event,
(char *)filename);
exit:
g_strfreev (elements);
g_free (basename);
g_free (dir_name);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_create done.\n");
}
static void
do_append_to (GVfsBackend *backend,
GVfsJobOpenForWrite *job,
const char *filename,
GFileCreateFlags flags)
{
if (!G_VFS_BACKEND_MTP (backend)->android_extension) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
return;
}
g_debug ("(I) do_append_to (%s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), filename);
if (entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file == NULL) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to get metadata.\n");
goto exit;
}
int ret = LIBMTP_BeginEditObject (device, entry->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to begin edit.\n");
goto exit;
}
RWHandle *handle = g_new0(RWHandle, 1);
handle->handle_type = HANDLE_FILE;
handle->id = entry->id;
handle->offset = file->filesize;
handle->size = file->filesize;
LIBMTP_destroy_file_t (file);
g_vfs_job_open_for_write_set_can_seek (G_VFS_JOB_OPEN_FOR_WRITE (job), TRUE);
g_vfs_job_open_for_write_set_can_truncate (G_VFS_JOB_OPEN_FOR_WRITE (job), TRUE);
g_vfs_job_open_for_write_set_handle (G_VFS_JOB_OPEN_FOR_WRITE (job), handle);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_append_to done.\n");
}
static void
do_replace (GVfsBackend *backend,
GVfsJobOpenForWrite *job,
const char *filename,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags)
{
if (!G_VFS_BACKEND_MTP (backend)->android_extension) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
return;
}
g_debug ("(I) do_replace (%s)\n", filename);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), filename);
if (entry == NULL) {
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
return do_create(backend, job, filename, flags);
} else if (entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file == NULL) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to get metadata.\n");
goto exit;
}
int ret = LIBMTP_BeginEditObject (device, entry->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to begin edit.\n");
goto exit;
}
ret = LIBMTP_TruncateObject (device, entry->id, 0);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
g_debug ("(I) Failed to truncate.\n");
goto exit;
}
RWHandle *handle = g_new0(RWHandle, 1);
handle->handle_type = HANDLE_FILE;
handle->id = entry->id;
handle->offset = 0;
handle->size = file->filesize;
LIBMTP_destroy_file_t (file);
g_vfs_job_open_for_write_set_can_seek (G_VFS_JOB_OPEN_FOR_WRITE (job), TRUE);
g_vfs_job_open_for_write_set_can_truncate (G_VFS_JOB_OPEN_FOR_WRITE (job), TRUE);
g_vfs_job_open_for_write_set_handle (G_VFS_JOB_OPEN_FOR_WRITE (job), handle);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_replace done.\n");
}
static void
do_write (GVfsBackend *backend,
GVfsJobWrite *job,
GVfsBackendHandle opaque_handle,
char *buffer,
gsize buffer_size)
{
RWHandle *handle = opaque_handle;
uint32_t id = handle->id;
goffset offset = handle->offset;
g_debug ("(I) do_write (%X %lu %lu)\n", id, offset, buffer_size);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
int ret = LIBMTP_SendPartialObject (G_VFS_BACKEND_MTP (backend)->device, id, offset,
(unsigned char *)buffer, buffer_size);
if (ret != 0) {
fail_job (G_VFS_JOB (job), G_VFS_BACKEND_MTP (backend)->device);
g_debug ("(I) job failed.\n");
goto exit;
}
handle->offset = offset buffer_size;
g_vfs_job_write_set_written_size (job, buffer_size);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_write done.\n");
}
static void
do_seek_on_write (GVfsBackend *backend,
GVfsJobSeekWrite *job,
GVfsBackendHandle opaque_handle,
goffset offset,
GSeekType type)
{
RWHandle *handle = opaque_handle;
uint32_t id = handle->id;
goffset old_offset = handle->offset;
gsize size = handle->size;
g_debug ("(I) do_seek_on_write (%X %lu %ld %u)\n", id, old_offset, offset, type);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
if (type == G_SEEK_END) {
offset = size offset;
} else if (type == G_SEEK_CUR) {
offset = old_offset;
}
if (offset < 0) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("End of stream"));
goto exit;
}
handle->offset = offset;
g_vfs_job_seek_write_set_offset (job, offset);
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_seek_on_write done. (%lu)\n", offset);
}
static void
do_truncate (GVfsBackend *backend,
GVfsJobTruncate *job,
GVfsBackendHandle opaque_handle,
goffset size)
{
RWHandle *handle = opaque_handle;
uint32_t id = handle->id;
g_debug("(I) do_truncate (%ld)\n", size);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
if (LIBMTP_TruncateObject (G_VFS_BACKEND_MTP (backend)->device, id, size) == 0) {
handle->size = size;
g_vfs_job_succeeded (G_VFS_JOB (job));
} else {
fail_job (G_VFS_JOB (job), G_VFS_BACKEND_MTP (backend)->device);
g_debug ("(I) Failed to truncate.\n");
}
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) truncate done.\n");
}
static void
do_close_write (GVfsBackend *backend,
GVfsJobCloseWrite *job,
GVfsBackendHandle opaque_handle)
{
g_debug ("(I) do_close_write\n");
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
RWHandle *handle = opaque_handle;
int ret = LIBMTP_EndEditObject (G_VFS_BACKEND_MTP (backend)->device, handle->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), G_VFS_BACKEND_MTP (backend)->device);
goto exit;
}
g_vfs_job_succeeded (G_VFS_JOB (job));
exit:
g_free(handle);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_close_write done.\n");
}
#if HAVE_LIBMTP_1_1_15
static void
do_move (GVfsBackend *backend,
GVfsJobMove *job,
const char *source,
const char *destination,
GFileCopyFlags flags,
GFileProgressCallback progress_callback,
gpointer progress_callback_data)
{
if (!G_VFS_BACKEND_MTP (backend)->move_object_capability) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
return;
}
g_debug ("(I) do_move (source = %s, dest = %s)\n", source, destination);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
char *dir_name = g_path_get_dirname (destination);
char *filename = g_path_get_basename (destination);
gchar **elements = g_strsplit_set (destination, "/", -1);
unsigned int ne = g_strv_length (elements);
if (ne < 3) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Cannot write to this location"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
CacheEntry *src_entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), source);
if (src_entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (src_entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
gboolean source_is_dir = FALSE;
uint64_t filesize = 0;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, src_entry->id);
if (file != NULL) {
source_is_dir = (file->filetype == LIBMTP_FILETYPE_FOLDER);
/*
* filesize is 0 for directories. However, given that we will only move
* a directory if it's staying on the same storage, then these moves
* will always be fast, and will finish too quickly for the progress
* value to matter. Moves between storages will be decomposed, with
* each file moved separately.
*/
filesize = file->filesize;
LIBMTP_destroy_file_t (file);
}
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), destination);
gboolean dest_exists = (entry != NULL && entry->id != -1);
gboolean dest_is_dir = FALSE;
if (dest_exists) {
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file != NULL) {
dest_is_dir = (file->filetype == LIBMTP_FILETYPE_FOLDER);
LIBMTP_destroy_file_t (file);
}
}
CacheEntry *parent = get_cache_entry (G_VFS_BACKEND_MTP (backend), dir_name);
if (!parent) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
// Only do directory moves on the same storage, where they are fast.
gboolean source_can_be_dir = (parent->storage == src_entry->storage);
gboolean valid_move = validate_source_and_dest (dest_exists,
dest_is_dir,
source_is_dir,
source_can_be_dir,
flags,
G_VFS_JOB (job));
if (!valid_move) {
goto exit;
} else if (dest_exists) {
/* Source and Dest are files */
g_debug ("(I) Removing destination.\n");
int ret = LIBMTP_Delete_Object (device, entry->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)destination);
remove_cache_entry (G_VFS_BACKEND_MTP (backend),
destination);
}
/* Unlike most calls, we must pass 0 for the root directory.*/
uint32_t parent_id = (parent->id == -1 ? 0 : parent->id);
int ret = LIBMTP_Move_Object (device,
src_entry->id,
parent->storage,
parent_id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
if (progress_callback) {
progress_callback (filesize, filesize, progress_callback_data);
}
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)source);
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_create_event,
(char *)destination);
exit:
g_strfreev (elements);
g_free (dir_name);
g_free (filename);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_move done.\n");
}
static void
do_copy (GVfsBackend *backend,
GVfsJobCopy *job,
const char *source,
const char *destination,
GFileCopyFlags flags,
GFileProgressCallback progress_callback,
gpointer progress_callback_data)
{
if (!G_VFS_BACKEND_MTP (backend)->copy_object_capability) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Operation unsupported"));
return;
}
g_debug ("(I) do_copy (source = %s, dest = %s)\n", source, destination);
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex);
char *dir_name = g_path_get_dirname (destination);
char *filename = g_path_get_basename (destination);
gchar **elements = g_strsplit_set (destination, "/", -1);
unsigned int ne = g_strv_length (elements);
if (ne < 3) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Cannot write to this location"));
goto exit;
}
LIBMTP_mtpdevice_t *device;
device = G_VFS_BACKEND_MTP (backend)->device;
CacheEntry *src_entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), source);
if (src_entry == NULL) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("File doesn’t exist"));
goto exit;
} else if (src_entry->id == -1) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
_("Not a regular file"));
goto exit;
}
gboolean source_is_dir = FALSE;
uint64_t filesize = 0;
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, src_entry->id);
if (file != NULL) {
source_is_dir = (file->filetype == LIBMTP_FILETYPE_FOLDER);
filesize = file->filesize;
LIBMTP_destroy_file_t (file);
}
CacheEntry *entry = get_cache_entry (G_VFS_BACKEND_MTP (backend), destination);
gboolean dest_exists = (entry != NULL && entry->id != -1);
gboolean dest_is_dir = FALSE;
if (dest_exists) {
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, entry->id);
if (file != NULL) {
dest_is_dir = (file->filetype == LIBMTP_FILETYPE_FOLDER);
LIBMTP_destroy_file_t (file);
}
}
CacheEntry *parent = get_cache_entry (G_VFS_BACKEND_MTP (backend), dir_name);
if (!parent) {
g_vfs_job_failed_literal (G_VFS_JOB (job),
G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Directory doesn’t exist"));
goto exit;
}
/*
* We ignore the ability to copy whole folders because we get poor progress
* updates in this situation. At least with file-by-file copies, we can
* notify as each file completes.
*/
gboolean valid_copy = validate_source_and_dest (dest_exists,
dest_is_dir,
source_is_dir,
FALSE, // source_can_be_dir
flags,
G_VFS_JOB (job));
if (!valid_copy) {
goto exit;
} else if (dest_exists) {
/* Source and Dest are files */
g_debug ("(I) Removing destination.\n");
int ret = LIBMTP_Delete_Object (device, entry->id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_delete_event,
(char *)destination);
remove_cache_entry (G_VFS_BACKEND_MTP (backend),
destination);
}
/* Unlike most calls, we must pass 0 for the root directory.*/
uint32_t parent_id = (parent->id == -1) ? 0 : parent->id;
int ret = LIBMTP_Copy_Object (device,
src_entry->id,
parent->storage,
parent_id);
if (ret != 0) {
fail_job (G_VFS_JOB (job), device);
goto exit;
}
if (progress_callback) {
progress_callback (filesize, filesize, progress_callback_data);
}
g_vfs_job_succeeded (G_VFS_JOB (job));
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors,
emit_create_event,
(char *)destination);
exit:
g_strfreev (elements);
g_free (dir_name);
g_free (filename);
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex);
g_debug ("(I) do_copy done.\n");
}
#endif /* HAVE_LIBMTP_1_1_15 */
/************************************************
* Class init
*
*/
static void
g_vfs_backend_mtp_class_init (GVfsBackendMtpClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GVfsBackendClass *backend_class = G_VFS_BACKEND_CLASS (klass);
gobject_class->finalize = g_vfs_backend_mtp_finalize;
backend_class->mount = do_mount;
backend_class->unmount = do_unmount;
backend_class->query_info = do_query_info;
backend_class->enumerate = do_enumerate;
backend_class->query_fs_info = do_query_fs_info;
backend_class->pull = do_pull;
backend_class->push = do_push;
backend_class->make_directory = do_make_directory;
backend_class->delete = do_delete;
backend_class->set_display_name = do_set_display_name;
backend_class->create_dir_monitor = do_create_dir_monitor;
backend_class->create_file_monitor = do_create_file_monitor;
backend_class->open_for_read = do_open_for_read;
backend_class->open_icon_for_read = do_open_icon_for_read;
backend_class->seek_on_read = do_seek_on_read;
backend_class->read = do_read;
backend_class->close_read = do_close_read;
backend_class->create = do_create;
backend_class->append_to = do_append_to;
backend_class->replace = do_replace;
backend_class->write = do_write;
backend_class->seek_on_write = do_seek_on_write;
backend_class->truncate = do_truncate;
backend_class->close_write = do_close_write;
#if HAVE_LIBMTP_1_1_15
backend_class->move = do_move;
backend_class->copy = do_copy;
#endif
}
|