1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "common.h"
#define DEBUG_FLAG SEAFILE_DEBUG_MERGE
#include "log.h"
#include "seafile-error.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <glib/gstdio.h>
#include "seafile-session.h"
#include "utils.h"
#include "fs-mgr.h"
#include "vc-utils.h"
#include "vc-common.h"
#include "index/index.h"
static gint
compare_dirents (gconstpointer a, gconstpointer b)
{
const SeafDirent *denta = a, *dentb = b;
return strcmp (dentb->name, denta->name);
}
int
commit_trees_cb (const char *repo_id, int version,
const char *worktree,
struct cache_tree *it, struct cache_entry **cache,
int entries, const char *base, int baselen)
{
SeafDir *seaf_dir;
GList *dirents = NULL, *ptr;
int i;
for (i = 0; i < entries; i++) {
SeafDirent *seaf_dent;
char *name;
struct cache_entry *ce = cache[i];
struct cache_tree_sub *sub;
const char *path, *slash;
int pathlen, entlen;
const unsigned char *sha1;
char hex[41];
unsigned mode;
guint64 mtime;
gint64 size;
char *modifier;
if (ce->ce_flags & CE_REMOVE)
continue; /* entry being removed */
path = ce->name;
pathlen = ce_namelen(ce);
if (pathlen <= baselen || memcmp(base, path, baselen))
break; /* at the end of this level */
slash = strchr(path + baselen, '/');
if (slash) {
entlen = slash - (path + baselen);
sub = cache_tree_find_subtree(it, path + baselen, entlen, 0);
g_return_val_if_fail (sub != NULL, -1);
/* Skip cache entries in the sub level. */
i += sub->cache_tree->entry_count - 1;
sha1 = sub->cache_tree->sha1;
mtime = sub->cache_tree->mtime;
mode = S_IFDIR;
name = g_strndup(path + baselen, entlen);
rawdata_to_hex (sha1, hex, 20);
seaf_dent = seaf_dirent_new (dir_version_from_repo_version(version),
hex, mode, name, mtime, NULL, -1);
g_free(name);
dirents = g_list_prepend (dirents, seaf_dent);
} else {
sha1 = ce->sha1;
mode = ce->ce_mode;
mtime = ce->ce_mtime.sec;
size = ce->ce_size;
modifier = ce->modifier;
entlen = pathlen - baselen;
name = g_strndup(path + baselen, entlen);
rawdata_to_hex (sha1, hex, 20);
if (version > 0) {
seaf_dent =
seaf_dirent_new (dir_version_from_repo_version(version),
hex, mode, name, mtime, modifier, size);
} else {
seaf_dent = seaf_dirent_new (0, hex, mode, name, 0, NULL, -1);
}
g_free(name);
dirents = g_list_prepend (dirents, seaf_dent);
}
#if DEBUG
fprintf(stderr, "cache-tree update-one %o %.*s\n",
mode, entlen, path + baselen);
#endif
}
/* Sort dirents in descending order. */
dirents = g_list_sort (dirents, compare_dirents);
seaf_dir = seaf_dir_new (NULL, dirents, dir_version_from_repo_version(version));
hex_to_rawdata (seaf_dir->dir_id, it->sha1, 20);
/* Dir's mtime is the latest of all dir entires. */
guint64 dir_mtime = 0;
SeafDirent *dent;
for (ptr = dirents; ptr; ptr = ptr->next) {
dent = ptr->data;
if (dent->mtime > dir_mtime)
dir_mtime = dent->mtime;
}
it->mtime = dir_mtime;
if (!seaf_fs_manager_object_exists (seaf->fs_mgr,
repo_id, version,
seaf_dir->dir_id))
seaf_dir_save (seaf->fs_mgr, repo_id, version, seaf_dir);
#if DEBUG
for (p = dirents; p; p = p->next) {
SeafDirent *tmp = (SeafDirent *)p->data;
fprintf(stderr, "dump dirent name %s id %s\n", tmp->name, tmp->id);
}
#endif
seaf_dir_free (seaf_dir);
return 0;
}
int
update_index (struct index_state *istate, const char *index_path)
{
char index_shadow[SEAF_PATH_MAX];
int index_fd;
int ret = 0;
snprintf (index_shadow, SEAF_PATH_MAX, "%s.shadow", index_path);
index_fd = seaf_util_create (index_shadow, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
0666);
if (index_fd < 0) {
seaf_warning ("Failed to open shadow index: %s.\n", strerror(errno));
return -1;
}
if (write_index (istate, index_fd) < 0) {
seaf_warning ("Failed to write shadow index: %s.\n", strerror(errno));
close (index_fd);
return -1;
}
close (index_fd);
ret = seaf_util_rename (index_shadow, index_path);
if (ret < 0) {
seaf_warning ("Failed to update index errno=%d %s\n", errno, strerror(errno));
return -1;
}
return 0;
}
#ifndef WIN32
int
seaf_remove_empty_dir (const char *path)
{
SeafStat st;
GDir *dir;
const char *dname;
char *full_path;
GError *error = NULL;
if (seaf_stat (path, &st) < 0 || !S_ISDIR(st.st_mode))
return 0;
if (seaf_util_rmdir (path) < 0) {
dir = g_dir_open (path, 0, &error);
if (!dir) {
seaf_warning ("Failed to open dir %s: %s.\n", path, error->message);
return -1;
}
/* Remove all ignored hidden files. */
while ((dname = g_dir_read_name (dir)) != NULL) {
if (seaf_repo_manager_is_ignored_hidden_file(dname)) {
full_path = g_build_path ("/", path, dname, NULL);
if (seaf_util_unlink (full_path) < 0)
seaf_warning ("Failed to remove file %s: %s.\n",
full_path, strerror(errno));
g_free (full_path);
}
}
g_dir_close (dir);
if (seaf_util_rmdir (path) < 0) {
seaf_warning ("Failed to remove dir %s: %s.\n", path, strerror(errno));
return -1;
}
}
return 0;
}
#else
static int
remove_hidden_file (wchar_t *parent, WIN32_FIND_DATAW *fdata,
void *user_data, gboolean *stop)
{
char *dname = NULL;
wchar_t *subpath_w = NULL;
char *subpath = NULL;
dname = g_utf16_to_utf8 (fdata->cFileName, -1, NULL, NULL, NULL);
if (!(fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
(!dname || seaf_repo_manager_is_ignored_hidden_file(dname))) {
subpath_w = g_new0 (wchar_t, wcslen(parent) + wcslen(fdata->cFileName) + 2);
wcscpy (subpath_w, parent);
wcscat (subpath_w, L"\\");
wcscat (subpath_w, fdata->cFileName);
if (!DeleteFileW (subpath_w)) {
subpath = g_utf16_to_utf8 (subpath_w, -1, NULL, NULL, NULL);
seaf_warning ("Failed to remove file %s: %lu.\n", subpath, GetLastError());
g_free (subpath);
}
g_free (subpath_w);
}
g_free (dname);
return 0;
}
int
seaf_remove_empty_dir (const char *path)
{
wchar_t *path_w = NULL;
WIN32_FILE_ATTRIBUTE_DATA attrs;
int ret = 0;
path_w = win32_long_path (path);
if (!GetFileAttributesExW (path_w, GetFileExInfoStandard, &attrs)) {
goto out;
}
if (!(attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
goto out;
}
if (RemoveDirectoryW (path_w))
goto out;
if (GetLastError() == ERROR_DIR_NOT_EMPTY) {
traverse_directory_win32 (path_w, remove_hidden_file, NULL);
if (!RemoveDirectoryW (path_w)) {
seaf_warning ("Failed to remove dir %s: %lu.\n", path, GetLastError());
ret = -1;
}
} else {
seaf_warning ("Failed to remove dir %s: %lu.\n", path, GetLastError());
ret = -1;
}
out:
g_free (path_w);
return ret;
}
#endif /* WIN32 */
static int
compute_file_id_with_cdc (const char *path, SeafStat *st,
SeafileCrypt *crypt, int repo_version,
uint32_t blk_avg_size, uint32_t blk_min_size, uint32_t blk_max_size,
unsigned char sha1[])
{
CDCFileDescriptor cdc;
memset (&cdc, 0, sizeof(cdc));
cdc.block_sz = blk_avg_size;
cdc.block_min_sz = blk_min_size;
cdc.block_max_sz = blk_max_size;
cdc.write_block = seafile_write_chunk;
if (filename_chunk_cdc (path, &cdc, crypt, FALSE) < 0) {
seaf_warning ("Failed to chunk file.\n");
return -1;
}
if (repo_version > 0)
seaf_fs_manager_calculate_seafile_id_json (repo_version, &cdc, sha1);
else
memcpy (sha1, cdc.file_sum, 20);
if (cdc.blk_sha1s)
free (cdc.blk_sha1s);
return 0;
}
int
compare_file_content (const char *path, SeafStat *st, const unsigned char *ce_sha1,
SeafileCrypt *crypt, int repo_version)
{
unsigned char sha1[20];
if (st->st_size == 0) {
memset (sha1, 0, 20);
return hashcmp (sha1, ce_sha1);
} else {
if (compute_file_id_with_cdc (path, st, crypt, repo_version,
CDC_AVERAGE_BLOCK_SIZE,
CDC_MIN_BLOCK_SIZE,
CDC_MAX_BLOCK_SIZE,
sha1) < 0) {
return -1;
}
if (hashcmp (sha1, ce_sha1) == 0)
return 0;
/* Compare with old cdc block size. */
uint32_t block_size = calculate_chunk_size (st->st_size);
if (compute_file_id_with_cdc (path, st, crypt, repo_version,
block_size,
block_size >> 2,
block_size << 2,
sha1) < 0) {
return -1;
}
return hashcmp (sha1, ce_sha1);
}
}
char *
build_checkout_path (const char *worktree, const char *ce_name, int len)
{
int base_len = strlen(worktree);
int full_len;
char path[SEAF_PATH_MAX];
int offset;
SeafStat st;
if (!len) {
seaf_warning ("entry name should not be empty.\n");
return NULL;
}
snprintf (path, SEAF_PATH_MAX, "%s/", worktree);
/* first create all leading directories. */
full_len = base_len + len + 1;
offset = base_len + 1;
while (offset < full_len) {
do {
path[offset] = ce_name[offset-base_len-1];
offset++;
} while (offset < full_len && ce_name[offset-base_len-1] != '/');
if (offset >= full_len)
break;
path[offset] = 0;
if (seaf_stat (path, &st) == 0 && S_ISDIR(st.st_mode))
continue;
if (seaf_util_mkdir (path, 0777) < 0) {
seaf_warning ("Failed to create directory %s.\n", path);
return NULL;
}
}
path[offset] = 0;
return g_strdup(path);
}
int
delete_path (const char *worktree, const char *name,
unsigned int mode, gint64 old_mtime)
{
char path[SEAF_PATH_MAX];
SeafStat st;
int len = strlen(name);
if (!len) {
seaf_warning ("entry name should not be empty.\n");
return -1;
}
snprintf (path, SEAF_PATH_MAX, "%s/%s", worktree, name);
if (!S_ISDIR(mode)) {
/* file doesn't exist in work tree */
if (seaf_stat (path, &st) < 0 || !S_ISREG(st.st_mode)) {
return 0;
}
/* file has been changed. */
if (!is_eml_file (name) && (old_mtime != st.st_mtime)) {
seaf_warning ("File %s is changed. Skip removing the file.\n", path);
return -1;
}
/* first unlink the file. */
if (seaf_util_unlink (path) < 0) {
seaf_warning ("Failed to remove %s: %s.\n", path, strerror(errno));
return -1;
}
} else {
if (seaf_remove_empty_dir (path) < 0) {
seaf_warning ("Failed to remove dir %s: %s.\n", path, strerror(errno));
return -1;
}
}
/* then remove all empty directories upwards. */
/* offset = base_len + len; */
/* do { */
/* if (path[offset] == '/') { */
/* path[offset] = '\0'; */
/* int ret = seaf_remove_empty_dir (path); */
/* if (ret < 0) { */
/* break; */
/* } */
/* } */
/* } while (--offset > base_len); */
return 0;
}
#ifdef WIN32
static gboolean
check_file_locked (const wchar_t *path_w, gboolean locked_on_server)
{
HANDLE handle;
/* If the file is locked on server, its local access right has been set to
* read-only. So trying to test GENERIC_WRITE access will certainly return
* ACCESS_DENIED. In this case, we can only test for GENERIC_READ.
* MS Office seems to gain exclusive read/write access to the file. So even
* trying read access can return a SHARING_VIOLATION error.
*/
DWORD access_mode = locked_on_server ? GENERIC_READ : GENERIC_WRITE;
handle = CreateFileW (path_w,
access_mode,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (handle != INVALID_HANDLE_VALUE) {
CloseHandle (handle);
} else if (GetLastError() == ERROR_SHARING_VIOLATION) {
return TRUE;
}
return FALSE;
}
gboolean
do_check_file_locked (const char *path, const char *worktree, gboolean locked_on_server)
{
char *real_path;
wchar_t *real_path_w;
gboolean ret;
real_path = g_build_path(PATH_SEPERATOR, worktree, path, NULL);
real_path_w = win32_long_path (real_path);
ret = check_file_locked (real_path_w, locked_on_server);
g_free (real_path);
g_free (real_path_w);
return ret;
}
static gboolean
check_dir_locked (const wchar_t *path_w)
{
HANDLE handle;
handle = CreateFileW (path_w,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (handle != INVALID_HANDLE_VALUE) {
CloseHandle (handle);
} else if (GetLastError() == ERROR_SHARING_VIOLATION) {
return TRUE;
}
return FALSE;
}
static gboolean
check_dir_locked_recursive (const wchar_t *path_w)
{
WIN32_FIND_DATAW fdata;
HANDLE handle;
wchar_t *pattern;
wchar_t *sub_path_w;
char *path;
int path_len_w;
DWORD error;
gboolean ret = FALSE;
if (check_dir_locked (path_w))
return TRUE;
path = g_utf16_to_utf8 (path_w, -1, NULL, NULL, NULL);
if (!path)
return FALSE;
path_len_w = wcslen(path_w);
pattern = g_new0 (wchar_t, (path_len_w + 3));
wcscpy (pattern, path_w);
wcscat (pattern, L"\\*");
handle = FindFirstFileW (pattern, &fdata);
if (handle == INVALID_HANDLE_VALUE) {
seaf_warning ("FindFirstFile failed %s: %lu.\n",
path, GetLastError());
goto out;
}
do {
if (wcscmp (fdata.cFileName, L".") == 0 ||
wcscmp (fdata.cFileName, L"..") == 0)
continue;
sub_path_w = g_new0 (wchar_t, path_len_w + wcslen(fdata.cFileName) + 2);
wcscpy (sub_path_w, path_w);
wcscat (sub_path_w, L"\\");
wcscat (sub_path_w, fdata.cFileName);
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (check_dir_locked_recursive (sub_path_w)) {
ret = TRUE;
g_free (sub_path_w);
goto out;
}
} else {
if (check_file_locked (sub_path_w, FALSE)) {
ret = TRUE;
g_free (sub_path_w);
goto out;
}
}
g_free (sub_path_w);
} while (FindNextFileW (handle, &fdata) != 0);
error = GetLastError();
if (error != ERROR_NO_MORE_FILES) {
seaf_warning ("FindNextFile failed %s: %lu.\n",
path, error);
}
out:
FindClose (handle);
g_free (path);
g_free (pattern);
return ret;
}
gboolean
do_check_dir_locked (const char *path, const char *worktree)
{
char *real_path = g_build_path (PATH_SEPERATOR, worktree, path, NULL);
wchar_t *real_path_w = win32_long_path (real_path);
gboolean ret = check_dir_locked_recursive (real_path_w);
g_free (real_path);
g_free (real_path_w);
return ret;
}
gboolean
files_locked_on_windows (struct index_state *index, const char *worktree)
{
gboolean ret = FALSE;
int i, entries;
struct cache_entry *ce;
entries = index->cache_nr;
for (i = 0; i < entries; ++i) {
ce = index->cache[i];
if (ce_stage(ce)) {
int mask = 0;
mask |= 1 << (ce_stage(ce) - 1);
while (i < entries) {
struct cache_entry *nce = index->cache[i];
if (strcmp(ce->name, nce->name))
break;
mask |= 1 << (ce_stage(nce) - 1);
i++;
}
i--;
/* Check unmerged cases that can potentially
update or remove current files in the worktree.
*/
if (mask == 7 || /* both changed */
mask == 6 || /* both added */
mask == 3) /* others removed */
{
if (do_check_file_locked (ce->name, worktree, FALSE))
ret = TRUE;
break;
}
} else if (ce->ce_flags & CE_UPDATE ||
ce->ce_flags & CE_WT_REMOVE) {
if (do_check_file_locked (ce->name, worktree, FALSE)) {
ret = TRUE;
break;
}
}
}
return ret;
}
#endif /* WIN32 */
#ifdef __APPLE__
/* On Mac, we only check whether an office file is opened and locked.
* The detection is done by checking ~$* tmp file.
*/
gboolean
do_check_file_locked (const char *path, const char *worktree, gboolean locked_on_server)
{
#define OFFICE_FILE_PATTERN ".*\\.(docx|xlsx|pptx|doc|xls|ppt|vsdx)"
char *dir_name = NULL, *file_name = NULL;
char *fullpath = NULL;
char *tmp_name = NULL;
int ret = FALSE;
if (!g_regex_match_simple (OFFICE_FILE_PATTERN, path, 0, 0))
return FALSE;
dir_name = g_path_get_dirname (path);
if (strcmp (dir_name, ".") == 0) {
g_free (dir_name);
dir_name = g_strdup("");
}
file_name = g_path_get_basename (path);
tmp_name = g_strconcat ("~$", file_name, NULL);
fullpath = g_build_path ("/", worktree, dir_name, tmp_name, NULL);
if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR)) {
ret = TRUE;
goto out;
}
g_free (tmp_name);
g_free (fullpath);
/* Sometimes the first two characters are replaced by ~$. */
if (g_utf8_strlen(file_name, -1) < 2)
goto out;
char *ptr = g_utf8_find_next_char(g_utf8_find_next_char (file_name, NULL), NULL);
tmp_name = g_strconcat ("~$", ptr, NULL);
fullpath = g_build_path ("/", worktree, dir_name, tmp_name, NULL);
if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR)) {
ret = TRUE;
goto out;
}
out:
g_free (fullpath);
g_free (tmp_name);
g_free (dir_name);
g_free (file_name);
return ret;
}
#endif
|