int paths_are_same(const char s[], const char t[]) { char s_real[PATH_MAX]; char t_real[PATH_MAX]; if(os_realpath(s, s_real) != s_real || os_realpath(t, t_real) != t_real) { return (stroscmp(s, t) == 0); } return (stroscmp(s_real, t_real) == 0); }
TEST(symlinks_to_dirs_are_recognized_as_dirs, IF(not_windows)) { char test_dir[PATH_MAX]; assert_non_null(os_realpath(TEST_DATA_PATH "/existing-files", test_dir)); assert_success(chdir(SANDBOX_PATH)); /* symlink() is not available on Windows, but other code is fine. */ #ifndef _WIN32 assert_success(symlink(test_dir, "dir-link")); #endif (void)test_dir; assert_false(flist_custom_active(&lwin)); flist_custom_start(&lwin, "test"); flist_custom_add(&lwin, "./dir-link"); assert_true(flist_custom_finish(&lwin, 0) == 0); assert_int_equal(1, lwin.list_rows); assert_true(is_directory_entry(&lwin.dir_entry[0])); assert_success(remove("dir-link")); }
TEST(reload_does_not_remove_broken_symlinks, IF(not_windows)) { char test_file[PATH_MAX]; assert_non_null(os_realpath(TEST_DATA_PATH "/existing-files/a", test_file)); assert_success(chdir(SANDBOX_PATH)); /* symlink() is not available on Windows, but other code is fine. */ #ifndef _WIN32 assert_success(symlink("/wrong/path", "broken-link")); #endif assert_false(flist_custom_active(&lwin)); flist_custom_start(&lwin, "test"); flist_custom_add(&lwin, test_file); flist_custom_add(&lwin, "./broken-link"); assert_true(flist_custom_finish(&lwin, 0) == 0); assert_int_equal(2, lwin.list_rows); load_dir_list(&lwin, 1); assert_int_equal(2, lwin.list_rows); assert_success(remove("broken-link")); }
SymLinkType get_symlink_type(const char path[]) { char cwd[PATH_MAX]; char linkto[PATH_MAX + NAME_MAX]; int saved_errno; char *filename_copy; char *p; if(get_cwd(cwd, sizeof(cwd)) == NULL) { /* getcwd() failed, just use "." rather than fail. */ strcpy(cwd, "."); } /* Use readlink() (in get_link_target_abs) before realpath() to check for * target at slow file system. realpath() doesn't fit in this case as it * resolves chains of symbolic links and we want to try only the first one. */ if(get_link_target_abs(path, cwd, linkto, sizeof(linkto)) != 0) { LOG_SERROR_MSG(errno, "Can't readlink \"%s\"", path); log_cwd(); return SLT_UNKNOWN; } if(refers_to_slower_fs(path, linkto)) { return SLT_SLOW; } filename_copy = strdup(path); chosp(filename_copy); p = os_realpath(filename_copy, linkto); saved_errno = errno; free(filename_copy); if(p == linkto) { return is_dir(linkto) ? SLT_DIR : SLT_UNKNOWN; } LOG_SERROR_MSG(saved_errno, "Can't realpath \"%s\"", path); log_cwd(); return SLT_UNKNOWN; }