~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_symlink.c

  • Committer: Jay Pipes
  • Date: 2008-07-17 17:54:00 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717175400-xm2aazihjra8mdzq
Removal of DBUG from libdrizzle/ - Round 2

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include <config.h>
17
 
 
18
 
#include <drizzled/internal/my_sys.h>
19
 
#include <drizzled/error.h>
20
 
#include <drizzled/internal/m_string.h>
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "mysys_priv.h"
 
17
#include "mysys_err.h"
 
18
#include <m_string.h>
21
19
#include <errno.h>
22
 
 
23
 
namespace drizzled
24
 
{
25
 
namespace internal
26
 
{
27
 
 
28
 
bool test_if_hard_path(const char *dir_name)
29
 
{
30
 
  if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR)
31
 
    return (home_dir != NULL && test_if_hard_path(home_dir));
32
 
  if (dir_name[0] == FN_LIBCHAR)
33
 
    return (true);
34
 
  return false;
35
 
} /* test_if_hard_path */
36
 
 
37
 
} /* namespace internal */
38
 
} /* namespace drizzled */
 
20
#ifdef HAVE_REALPATH
 
21
#include <sys/param.h>
 
22
#include <sys/stat.h>
 
23
#endif
 
24
 
 
25
/*
 
26
  Reads the content of a symbolic link
 
27
  If the file is not a symbolic link, return the original file name in to.
 
28
 
 
29
  RETURN
 
30
    0  If filename was a symlink,    (to will be set to value of symlink)
 
31
    1  If filename was a normal file (to will be set to filename)
 
32
   -1  on error.
 
33
*/
 
34
 
 
35
int my_readlink(char *to, const char *filename, myf MyFlags)
 
36
{
 
37
#ifndef HAVE_READLINK
 
38
  strmov(to,filename);
 
39
  return 1;
 
40
#else
 
41
  int result=0;
 
42
  int length;
 
43
  DBUG_ENTER("my_readlink");
 
44
 
 
45
  if ((length=readlink(filename, to, FN_REFLEN-1)) < 0)
 
46
  {
 
47
    /* Don't give an error if this wasn't a symlink */
 
48
    if ((my_errno=errno) == EINVAL)
 
49
    {
 
50
      result= 1;
 
51
      strmov(to,filename);
 
52
    }
 
53
    else
 
54
    {
 
55
      if (MyFlags & MY_WME)
 
56
        my_error(EE_CANT_READLINK, MYF(0), filename, errno);
 
57
      result= -1;
 
58
    }
 
59
  }
 
60
  else
 
61
    to[length]=0;
 
62
  DBUG_PRINT("exit" ,("result: %d", result));
 
63
  DBUG_RETURN(result);
 
64
#endif /* HAVE_READLINK */
 
65
}
 
66
 
 
67
 
 
68
/* Create a symbolic link */
 
69
 
 
70
int my_symlink(const char *content, const char *linkname, myf MyFlags)
 
71
{
 
72
#ifndef HAVE_READLINK
 
73
  return 0;
 
74
#else
 
75
  int result;
 
76
  DBUG_ENTER("my_symlink");
 
77
  DBUG_PRINT("enter",("content: %s  linkname: %s", content, linkname));
 
78
 
 
79
  result= 0;
 
80
  if (symlink(content, linkname))
 
81
  {
 
82
    result= -1;
 
83
    my_errno=errno;
 
84
    if (MyFlags & MY_WME)
 
85
      my_error(EE_CANT_SYMLINK, MYF(0), linkname, content, errno);
 
86
  }
 
87
  else if ((MyFlags & MY_SYNC_DIR) && my_sync_dir_by_file(linkname, MyFlags))
 
88
    result= -1;
 
89
  DBUG_RETURN(result);
 
90
#endif /* HAVE_READLINK */
 
91
}
 
92
 
 
93
/*
 
94
  Resolve all symbolic links in path
 
95
  'to' may be equal to 'filename'
 
96
 
 
97
  Because purify gives a lot of UMR errors when using realpath(),
 
98
  this code is disabled when using purify.
 
99
 
 
100
  If MY_RESOLVE_LINK is given, only do realpath if the file is a link.
 
101
*/
 
102
 
 
103
#if defined(SCO)
 
104
#define BUFF_LEN 4097
 
105
#elif defined(MAXPATHLEN)
 
106
#define BUFF_LEN MAXPATHLEN
 
107
#else
 
108
#define BUFF_LEN FN_LEN
 
109
#endif
 
110
 
 
111
int my_realpath(char *to, const char *filename,
 
112
                myf MyFlags __attribute__((unused)))
 
113
{
 
114
#if defined(HAVE_REALPATH) && !defined(HAVE_purify) && !defined(HAVE_BROKEN_REALPATH)
 
115
  int result=0;
 
116
  char buff[BUFF_LEN];
 
117
  struct stat stat_buff;
 
118
  DBUG_ENTER("my_realpath");
 
119
 
 
120
  if (!(MyFlags & MY_RESOLVE_LINK) ||
 
121
      (!lstat(filename,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
 
122
  {
 
123
    char *ptr;
 
124
    DBUG_PRINT("info",("executing realpath"));
 
125
    if ((ptr=realpath(filename,buff)))
 
126
    {
 
127
      strmake(to,ptr,FN_REFLEN-1);
 
128
    }
 
129
    else
 
130
    {
 
131
      /*
 
132
        Realpath didn't work;  Use my_load_path() which is a poor substitute
 
133
        original name but will at least be able to resolve paths that starts
 
134
        with '.'.
 
135
      */
 
136
      DBUG_PRINT("error",("realpath failed with errno: %d", errno));
 
137
      my_errno=errno;
 
138
      if (MyFlags & MY_WME)
 
139
        my_error(EE_REALPATH, MYF(0), filename, my_errno);
 
140
      my_load_path(to, filename, NullS);
 
141
      result= -1;
 
142
    }
 
143
  }
 
144
  DBUG_RETURN(result);
 
145
#else
 
146
  my_load_path(to, filename, NullS);
 
147
  return 0;
 
148
#endif
 
149
}