Skip to content
/ server Public
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions mysys/my_symlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ int my_is_symlink(const char *filename __attribute__((unused)))
}

/*
Resolve all symbolic links in path
Resolve all symbolic links in path.

If path is relative, assume current directory and replace with absolute.
If filename is empty string, resolve path to current directory.

'to' may be equal to 'filename'

to is guaranteed to never set to a string longer than FN_REFLEN
Expand All @@ -144,10 +148,12 @@ int my_realpath(char *to, const char *filename, myf MyFlags)
int result=0;
char buff[BUFF_LEN];
char *ptr;
static const char cur_dir[] = {FN_CURLIB, '\0'};
DBUG_ENTER("my_realpath");

DBUG_PRINT("info",("executing realpath"));
if ((ptr=realpath(filename,buff)))
/* realpath won't accept empty string - use "." instead */
if ((ptr=realpath(filename[0] ? filename : cur_dir, buff)))
strmake(to, ptr, FN_REFLEN-1);
else
{
Expand Down
3 changes: 2 additions & 1 deletion unittest/mysys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA

MY_ADD_TESTS(base64 bitmap byte_order crc32 dynstring lf my_atomic my_getopt
my_malloc my_rdtsc queues stack_allocation stacktrace waiting_threads
my_malloc my_rdtsc my_symlink queues stack_allocation stacktrace
waiting_threads
LINK_LIBRARIES mysys)
MY_ADD_TESTS(my_vsnprintf LINK_LIBRARIES strings mysys)
ADD_DEFINITIONS(${SSL_DEFINES})
Expand Down
82 changes: 82 additions & 0 deletions unittest/mysys/my_symlink-t.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <my_global.h>
#include <my_sys.h>

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "tap.h"

static void test_my_realpath()
{
#ifndef HAVE_READLINK
skip(2, "realpath not supported");
return;
#else
char cwd[PATH_MAX];
char *resolved_cwd;
char result[FN_REFLEN];
char link_name[64] = {0};
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
diag("getcwd() failed: %s", strerror(errno));
skip(2, "getcwd() failed");
return;
}

resolved_cwd = realpath(cwd, NULL);
if (!resolved_cwd)
{
diag("realpath() failed: %s", strerror(errno));
skip(2, "realpath() failed");
return;
}

/* Call my_realpath with empty string in current working directory,
expecting resolved abolute path to that directory */
my_realpath(result, "", MYF(0));
ok(!strcmp(result, resolved_cwd),
"Output of my_realpath: %s, expected: %s",
result, resolved_cwd);
/* Creae a symlink, chdir to it and resolve empty string */

/* Generate a unique name for the symlink */
srand(time(NULL));
snprintf(link_name, sizeof(link_name), "my_link_%d.%06d", getpid(), rand() % 1000000);
if (symlink(cwd, link_name) != 0)
{
diag("symlink() failed: %s", strerror(errno));
skip(1, "symlink() failed");
goto cwd_cleanup;
}
if (chdir(link_name) != 0)
{
diag("chdir() failed: %s", strerror(errno));
skip(1, "chdir() failed");
goto link_cleanup;
}
my_realpath(result, "", MYF(0));
ok(!strcmp(result, resolved_cwd),
"Output of my_realpath: %s, expected: %s",
result, resolved_cwd);
link_cleanup:
unlink(link_name);
cwd_cleanup:
free(resolved_cwd);
#endif
}

int main(int argc __attribute__((unused)),char *argv[])
{
MY_INIT(argv[0]);
plan(2);

test_my_realpath();

my_end(0);
return exit_status();
}