595 int sync_conflict = 0; |
595 int sync_conflict = 0; |
596 |
596 |
597 UcxList *res_modified = NULL; |
597 UcxList *res_modified = NULL; |
598 UcxList *res_new = NULL; |
598 UcxList *res_new = NULL; |
599 UcxList *res_moved = NULL; // type: MovedFile |
599 UcxList *res_moved = NULL; // type: MovedFile |
600 UcxList *res_copied = NULL; // type: MovedFile |
|
601 UcxList *res_conflict = NULL; |
600 UcxList *res_conflict = NULL; |
602 UcxList *res_mkdir = NULL; |
601 UcxList *res_mkdir = NULL; |
603 UcxList *res_metadata = NULL; |
602 UcxList *res_metadata = NULL; |
604 UcxList *res_broken = NULL; |
603 UcxList *res_broken = NULL; |
605 UcxMap *lres_removed = ucx_map_new(16); // type: LocalResource* |
604 UcxMap *lres_removed = ucx_map_new(16); // type: LocalResource* |
1822 UCX_FOREACH(elm, removed_res) { |
1821 UCX_FOREACH(elm, removed_res) { |
1823 LocalResource *local = elm->data; |
1822 LocalResource *local = elm->data; |
1824 ucx_map_cstr_remove(db->resources, local->path); |
1823 ucx_map_cstr_remove(db->resources, local->path); |
1825 } |
1824 } |
1826 |
1825 |
1827 ls_delete = ucx_list_sort(ls_delete, (cmp_func)resource_pathlen_cmp, NULL); |
|
1828 |
|
1829 // |
1826 // |
1830 // BEGIN PUSH |
1827 // BEGIN PUSH |
1831 // |
1828 // |
1832 |
1829 |
1833 // upload changed files |
|
1834 ls_modified = ucx_list_concat(ls_new, ls_modified); |
|
1835 |
|
1836 int ret = 0; |
1830 int ret = 0; |
1837 int error = 0; |
1831 int error = 0; |
1838 for(UcxList *elm=ls_modified;elm && !sync_shutdown;elm=elm->next) { |
1832 |
1839 LocalResource *local_res = elm->data; |
1833 // copy/move files |
1840 |
|
1841 DavResource *res = dav_resource_new(sn, local_res->path); |
|
1842 if(!res) { |
|
1843 print_resource_error(sn, local_res->path); |
|
1844 ret = -1; |
|
1845 sync_error++; |
|
1846 } |
|
1847 |
|
1848 if(local_res->isdirectory) { |
|
1849 dav_exists(res); |
|
1850 if(sn->error == DAV_NOT_FOUND) { |
|
1851 int abort = 0; |
|
1852 // make sure to store tags for newly created cols |
|
1853 local_res->tags_updated = 1; |
|
1854 // create collection |
|
1855 // TODO: show 405 |
|
1856 printf("mkcol: %s\n", local_res->path); |
|
1857 if(sync_mkdir(dir, res, local_res) && sn->error != DAV_METHOD_NOT_ALLOWED) { |
|
1858 print_resource_error(sn, res->path); |
|
1859 ret = -1; |
|
1860 sync_error++; |
|
1861 error = 1; |
|
1862 abort = 1; |
|
1863 } |
|
1864 |
|
1865 if(local_res->metadata_updated && !abort) { |
|
1866 sync_update_metadata(dir, sn, res, local_res); |
|
1867 } |
|
1868 } else if(sn->error != DAV_OK) { |
|
1869 // dav_exists() failed |
|
1870 print_resource_error(sn, local_res->path); |
|
1871 ret = -1; |
|
1872 sync_error++; |
|
1873 error = 1; |
|
1874 } |
|
1875 } else { |
|
1876 int changed = remote_resource_is_changed(sn, dir, db, res, local_res); |
|
1877 if(cdt && changed) { |
|
1878 printf("conflict: %s\n", local_res->path); |
|
1879 local_res->last_modified = 0; |
|
1880 nullfree(local_res->etag); |
|
1881 local_res->etag = NULL; |
|
1882 nullfree(local_res->hash); |
|
1883 local_res->hash = NULL; |
|
1884 local_res->skipped = TRUE; |
|
1885 sync_conflict++; |
|
1886 } else { |
|
1887 printf("put: %s\n", local_res->path); |
|
1888 if(sync_put_resource(dir, res, local_res, &sync_success)) { |
|
1889 sync_error++; |
|
1890 print_resource_error(sn, res->path); |
|
1891 ret = -1; |
|
1892 error = 1; |
|
1893 } |
|
1894 } |
|
1895 } |
|
1896 dav_resource_free(res); |
|
1897 |
|
1898 LocalResource *dbres = ucx_map_cstr_remove(db->resources, local_res->path); |
|
1899 ucx_map_cstr_put(db->resources, local_res->path, local_res); |
|
1900 //if(dbres) local_resource_free(dbres); |
|
1901 } |
|
1902 |
|
1903 DavBool copy = TRUE; |
1834 DavBool copy = TRUE; |
1904 if(!ls_copy) { |
1835 if(!ls_copy) { |
1905 copy = FALSE; |
1836 copy = FALSE; |
1906 ls_copy = ls_move; |
1837 ls_copy = ls_move; |
1907 } |
1838 } |
1927 db, |
1858 db, |
1928 origin_res, |
1859 origin_res, |
1929 local->origin); |
1860 local->origin); |
1930 if(origin_changed) { |
1861 if(origin_changed) { |
1931 // upload with put |
1862 // upload with put |
1932 printf("put: %s\n", local->path); |
1863 ls_modified = ucx_list_prepend(ls_modified, local); |
1933 err = sync_put_resource(dir, res, local, &sync_success); |
|
1934 |
|
1935 // TODO: if move, delete old resource |
|
1936 } else { |
1864 } else { |
1937 printf("%s: %s -> %s\n", copy ? "copy":"move", local->origin->path, local->path); |
1865 printf("%s: %s -> %s\n", copy ? "copy":"move", local->origin->path, local->path); |
1938 err = sync_move_remote_resource( |
1866 err = sync_move_remote_resource( |
1939 dir, |
1867 dir, |
1940 origin_res, |
1868 origin_res, |
1959 elm->next = ls_move; |
1887 elm->next = ls_move; |
1960 copy = FALSE; |
1888 copy = FALSE; |
1961 } |
1889 } |
1962 } |
1890 } |
1963 |
1891 |
|
1892 // upload changed files |
|
1893 ls_modified = ucx_list_concat(ls_new, ls_modified); |
|
1894 |
|
1895 for(UcxList *elm=ls_modified;elm && !sync_shutdown;elm=elm->next) { |
|
1896 LocalResource *local_res = elm->data; |
|
1897 |
|
1898 DavResource *res = dav_resource_new(sn, local_res->path); |
|
1899 if(!res) { |
|
1900 print_resource_error(sn, local_res->path); |
|
1901 ret = -1; |
|
1902 sync_error++; |
|
1903 } |
|
1904 |
|
1905 if(local_res->isdirectory) { |
|
1906 dav_exists(res); |
|
1907 if(sn->error == DAV_NOT_FOUND) { |
|
1908 int abort = 0; |
|
1909 // make sure to store tags for newly created cols |
|
1910 local_res->tags_updated = 1; |
|
1911 // create collection |
|
1912 // TODO: show 405 |
|
1913 printf("mkcol: %s\n", local_res->path); |
|
1914 if(sync_mkdir(dir, res, local_res) && sn->error != DAV_METHOD_NOT_ALLOWED) { |
|
1915 print_resource_error(sn, res->path); |
|
1916 ret = -1; |
|
1917 sync_error++; |
|
1918 error = 1; |
|
1919 abort = 1; |
|
1920 } |
|
1921 |
|
1922 if(local_res->metadata_updated && !abort) { |
|
1923 sync_update_metadata(dir, sn, res, local_res); |
|
1924 } |
|
1925 } else if(sn->error != DAV_OK) { |
|
1926 // dav_exists() failed |
|
1927 print_resource_error(sn, local_res->path); |
|
1928 ret = -1; |
|
1929 sync_error++; |
|
1930 error = 1; |
|
1931 } |
|
1932 } else { |
|
1933 int changed = remote_resource_is_changed(sn, dir, db, res, local_res); |
|
1934 if(cdt && changed) { |
|
1935 printf("conflict: %s\n", local_res->path); |
|
1936 local_res->last_modified = 0; |
|
1937 nullfree(local_res->etag); |
|
1938 local_res->etag = NULL; |
|
1939 nullfree(local_res->hash); |
|
1940 local_res->hash = NULL; |
|
1941 local_res->skipped = TRUE; |
|
1942 sync_conflict++; |
|
1943 } else { |
|
1944 printf("put: %s\n", local_res->path); |
|
1945 if(sync_put_resource(dir, res, local_res, &sync_success)) { |
|
1946 sync_error++; |
|
1947 print_resource_error(sn, res->path); |
|
1948 ret = -1; |
|
1949 error = 1; |
|
1950 } |
|
1951 } |
|
1952 } |
|
1953 dav_resource_free(res); |
|
1954 |
|
1955 LocalResource *dbres = ucx_map_cstr_remove(db->resources, local_res->path); |
|
1956 ucx_map_cstr_put(db->resources, local_res->path, local_res); |
|
1957 //if(dbres) local_resource_free(dbres); |
|
1958 } |
|
1959 |
1964 // metadata updates |
1960 // metadata updates |
1965 for(UcxList *elm=ls_update;elm && !sync_shutdown;elm=elm->next) { |
1961 for(UcxList *elm=ls_update;elm && !sync_shutdown;elm=elm->next) { |
1966 LocalResource *local_res = elm->data; |
1962 LocalResource *local_res = elm->data; |
1967 |
1963 |
1968 DavResource *res = dav_resource_new(sn, local_res->path); |
1964 DavResource *res = dav_resource_new(sn, local_res->path); |
1973 } |
1969 } |
1974 } |
1970 } |
1975 } |
1971 } |
1976 |
1972 |
1977 // delete all removed files |
1973 // delete all removed files |
|
1974 ls_delete = ucx_list_sort(ls_delete, (cmp_func)resource_pathlen_cmp, NULL); |
|
1975 |
1978 UcxList *cols = NULL; |
1976 UcxList *cols = NULL; |
1979 UcxList **col_list = &cols; |
1977 UcxList **col_list = &cols; |
1980 UcxList *deletelist = ls_delete; |
1978 UcxList *deletelist = ls_delete; |
1981 for(int i=0;i<2;i++) { |
1979 for(int i=0;i<2;i++) { |
1982 // the first iteration deletes everything from ls_delete except |
1980 // the first iteration deletes everything from ls_delete except |