Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD readlink support for linux (POC,needs remerge, needs more features) #1067

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions src/csync/vio/csync_vio_local_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@

#include "vio/csync_vio_local.h"

#include <unistd.h>

#ifndef _WIN32
#define LINKPATHBUF 2048
#endif

Q_LOGGING_CATEGORY(lcCSyncVIOLocal, "sync.csync.vio_local", QtInfoMsg)

/*
Expand All @@ -47,6 +53,7 @@ typedef struct dhandle_s {
} dhandle_t;

static int _csync_vio_local_stat_mb(const mbchar_t *wuri, csync_file_stat_t *buf);
void _csync_vio_local_proc_filestat(const mbchar_t *wuri, csync_file_stat_t* p_file_stat ) ;

csync_vio_handle_t *csync_vio_local_opendir(const char *name) {
dhandle_t *handle = NULL;
Expand Down Expand Up @@ -90,11 +97,10 @@ int csync_vio_local_closedir(csync_vio_handle_t *dhandle) {
std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *dhandle) {

dhandle_t *handle = NULL;

handle = (dhandle_t *) dhandle;
struct _tdirent *dirent = NULL;
std::unique_ptr<csync_file_stat_t> file_stat;

handle = (dhandle_t *) dhandle;
do {
dirent = _treaddir(handle->dh);
if (dirent == NULL)
Expand Down Expand Up @@ -133,13 +139,11 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *d
if (file_stat->path.isNull())
return file_stat;

if (_csync_vio_local_stat_mb(fullPath.constData(), file_stat.get()) < 0) {
// Will get excluded by _csync_detect_update.
file_stat->type = ItemTypeSkip;
}
_csync_vio_local_proc_filestat( fullPath.constData(), file_stat.get() );

return file_stat;
}

}

int csync_vio_local_stat(const char *uri, csync_file_stat_t *buf)
{
Expand All @@ -150,6 +154,49 @@ int csync_vio_local_stat(const char *uri, csync_file_stat_t *buf)
return rc;
}

void _csync_vio_local_proc_filestat(const mbchar_t *wuri, csync_file_stat_t* file_stat )
{
if (_csync_vio_local_stat_mb(wuri, file_stat) < 0) {
// Will get excluded by _csync_detect_update.
file_stat->type = ItemTypeSkip;
}

#ifndef _WIN32
// Handle symlinks
mbchar_t buf[LINKPATHBUF];
const mbchar_t* pbuf = buf;
int len;
if (file_stat->type == ItemTypeSoftLink) {
// Resolve the link,
// CASE A: File
// Stat the file
// CASE B: Directory
// Stat the directory, also csync_vio_local_readdir should handle symlinks
// CASE C: Broken
// Ignore (TypeSoftLink is actually broken/unsupported link)

// TODO check if readlink combat with mbchar_t
if ((len = readlink(wuri, buf, sizeof(buf)-2)) > -1) {
buf[len] = '\0';
//if (len == sizeof(buf)-1) {
// // CORNER CASE
// // Warning maybe truncated, path too long
// qCWarning(lcCSyncVIOLocal) << "Link set too long, maybe path is truncated:" << wuri << ", " << buf;
//}
// CASE A
// CASE B
_csync_vio_local_proc_filestat( pbuf, file_stat);

}
else {
// CASE C:
qCWarning(lcCSyncVIOLocal) << "Link read error: " << wuri << ", err: " << errno << strerror(errno) ;
// file_stat->type = ItemTypeSoftLink;
}
}
#endif
}

static int _csync_vio_local_stat_mb(const mbchar_t *wuri, csync_file_stat_t *buf)
{
csync_stat_t sb;
Expand Down