~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_symlink.cc

  • Committer: Monty Taylor
  • Date: 2008-11-16 05:36:13 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116053613-bld4rqxhlkb49c02
Split out cache_row and type_holder.

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 <mystrings/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
  my_stpcpy(to,filename);
 
39
  return 1;
 
40
#else
 
41
  int result=0;
 
42
  int length;
 
43
 
 
44
  if ((length=readlink(filename, to, FN_REFLEN-1)) < 0)
 
45
  {
 
46
    /* Don't give an error if this wasn't a symlink */
 
47
    if ((my_errno=errno) == EINVAL)
 
48
    {
 
49
      result= 1;
 
50
      my_stpcpy(to,filename);
 
51
    }
 
52
    else
 
53
    {
 
54
      if (MyFlags & MY_WME)
 
55
        my_error(EE_CANT_READLINK, MYF(0), filename, errno);
 
56
      result= -1;
 
57
    }
 
58
  }
 
59
  else
 
60
    to[length]=0;
 
61
  return(result);
 
62
#endif /* HAVE_READLINK */
 
63
}
 
64
 
 
65
 
 
66
/* Create a symbolic link */
 
67
 
 
68
int my_symlink(const char *content, const char *linkname, myf MyFlags)
 
69
{
 
70
#ifndef HAVE_READLINK
 
71
  return 0;
 
72
#else
 
73
  int result;
 
74
 
 
75
  result= 0;
 
76
  if (symlink(content, linkname))
 
77
  {
 
78
    result= -1;
 
79
    my_errno=errno;
 
80
    if (MyFlags & MY_WME)
 
81
      my_error(EE_CANT_SYMLINK, MYF(0), linkname, content, errno);
 
82
  }
 
83
  else if ((MyFlags & MY_SYNC_DIR) && my_sync_dir_by_file(linkname, MyFlags))
 
84
    result= -1;
 
85
  return(result);
 
86
#endif /* HAVE_READLINK */
 
87
}
 
88
 
 
89
/*
 
90
  Resolve all symbolic links in path
 
91
  'to' may be equal to 'filename'
 
92
 
 
93
  Because purify gives a lot of UMR errors when using realpath(),
 
94
  this code is disabled when using purify.
 
95
 
 
96
  If MY_RESOLVE_LINK is given, only do realpath if the file is a link.
 
97
*/
 
98
 
 
99
#if defined(SCO)
 
100
#define BUFF_LEN 4097
 
101
#elif defined(MAXPATHLEN)
 
102
#define BUFF_LEN MAXPATHLEN
 
103
#else
 
104
#define BUFF_LEN FN_LEN
 
105
#endif
 
106
 
 
107
int my_realpath(char *to, const char *filename,
 
108
                myf MyFlags __attribute__((unused)))
 
109
{
 
110
#if defined(HAVE_REALPATH) && !defined(HAVE_purify) && !defined(HAVE_BROKEN_REALPATH)
 
111
  int result=0;
 
112
  char buff[BUFF_LEN];
 
113
  struct stat stat_buff;
 
114
 
 
115
  if (!(MyFlags & MY_RESOLVE_LINK) ||
 
116
      (!lstat(filename,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
 
117
  {
 
118
    char *ptr;
 
119
    if ((ptr=realpath(filename,buff)))
 
120
    {
 
121
      strmake(to,ptr,FN_REFLEN-1);
 
122
    }
 
123
    else
 
124
    {
 
125
      /*
 
126
        Realpath didn't work;  Use my_load_path() which is a poor substitute
 
127
        original name but will at least be able to resolve paths that starts
 
128
        with '.'.
 
129
      */
 
130
      my_errno=errno;
 
131
      if (MyFlags & MY_WME)
 
132
        my_error(EE_REALPATH, MYF(0), filename, my_errno);
 
133
      my_load_path(to, filename, NULL);
 
134
      result= -1;
 
135
    }
 
136
  }
 
137
  return(result);
 
138
#else
 
139
  my_load_path(to, filename, NULL);
 
140
  return 0;
 
141
#endif
 
142
}