~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/internal/mf_format.cc

Merge Monty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys_priv.h"
17
 
#include <m_string.h>
 
16
#include "drizzled/internal/mysys_priv.h"
 
17
 
 
18
#include <fcntl.h>
 
19
 
 
20
#ifdef HAVE_SYS_STAT_H
 
21
# include <sys/stat.h>
 
22
#endif
 
23
 
 
24
#include <algorithm>
 
25
 
 
26
#include "drizzled/internal/m_string.h"
 
27
 
 
28
using namespace std;
18
29
 
19
30
/*
20
31
  Formats a filename with possible replace of directory of extension
24
35
*/
25
36
 
26
37
char * fn_format(char * to, const char *name, const char *dir,
27
 
                    const char *extension, uint flag)
 
38
                    const char *extension, uint32_t flag)
28
39
{
29
 
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos;
 
40
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
 
41
  const char *startpos = name;
30
42
  const char *ext;
31
43
  register size_t length;
32
44
  size_t dev_length;
33
45
 
34
46
  /* Copy and skip directory */
35
 
  name+=(length=dirname_part(dev, (startpos=(char *) name), &dev_length));
 
47
  name+=(length=dirname_part(dev, startpos, &dev_length));
36
48
  if (length == 0 || (flag & MY_REPLACE_DIR))
37
49
  {
38
50
    /* Use given directory */
39
 
    convert_dirname(dev,dir,NullS);             /* Fix to this OS */
 
51
    convert_dirname(dev,dir,NULL);              /* Fix to this OS */
40
52
  }
41
53
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
42
54
  {
43
55
    /* Put 'dir' before the given path */
44
 
    strmake(buff,dev,sizeof(buff)-1);
45
 
    pos=convert_dirname(dev,dir,NullS);
46
 
    strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev));
 
56
    strncpy(buff,dev,sizeof(buff)-1);
 
57
    pos=convert_dirname(dev,dir,NULL);
 
58
    strncpy(pos,buff,sizeof(buff)-1- (int) (pos-dev));
47
59
  }
48
60
 
49
 
  if (flag & MY_PACK_FILENAME)
50
 
    pack_dirname(dev,dev);                      /* Put in ./.. and ~/.. */
51
61
  if (flag & MY_UNPACK_FILENAME)
52
62
    (void) unpack_dirname(dev,dev);             /* Replace ~/.. with dir */
53
63
 
54
64
  if (!(flag & MY_APPEND_EXT) &&
55
 
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
 
65
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
56
66
  {
57
67
    if ((flag & MY_REPLACE_EXT) == 0)           /* If we should keep old ext */
58
68
    {
76
86
    /* To long path, return original or NULL */
77
87
    size_t tmp_length;
78
88
    if (flag & MY_SAFE_PATH)
79
 
      return NullS;
80
 
    tmp_length= strlength(startpos);
81
 
    (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
 
89
      return NULL;
 
90
    tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
 
91
    strncpy(to,startpos,tmp_length);
 
92
    to[tmp_length]= '\0';
82
93
  }
83
94
  else
84
95
  {
85
96
    if (to == startpos)
86
97
    {
87
 
      bmove(buff,(uchar*) name,length);         /* Save name for last copy */
 
98
      memmove(buff, name, length); /* Save name for last copy */
88
99
      name=buff;
89
100
    }
90
 
    pos=strmake(strmov(to,dev),name,length);
91
 
    (void) strmov(pos,ext);                     /* Don't convert extension */
 
101
    char *tmp= strcpy(to, dev) + strlen(dev);
 
102
    pos= strncpy(tmp,name,length) + length;
 
103
    (void) strcpy(pos,ext);                     /* Don't convert extension */
92
104
  }
93
105
  /*
94
106
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
95
107
    realpath if the file is a symbolic link
96
108
  */
97
109
  if (flag & MY_RETURN_REAL_PATH)
98
 
    (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
99
 
                                   MY_RESOLVE_LINK: 0));
 
110
  {
 
111
    struct stat stat_buff;
 
112
    char rp_buff[PATH_MAX];
 
113
    if ((!flag & MY_RESOLVE_SYMLINKS) || 
 
114
       (!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
 
115
    {
 
116
      if (!realpath(to,rp_buff))
 
117
        my_load_path(rp_buff, to, NULL);
 
118
      rp_buff[FN_REFLEN-1]= '\0';
 
119
      strcpy(to,rp_buff);
 
120
    }
 
121
  }
100
122
  else if (flag & MY_RESOLVE_SYMLINKS)
101
123
  {
102
 
    strmov(buff,to);
103
 
    (void) my_readlink(to, buff, MYF(0));
 
124
    strcpy(buff,to);
 
125
    ssize_t sym_link_size= readlink(buff,to,FN_REFLEN-1);
 
126
    if (sym_link_size >= 0)
 
127
      to[sym_link_size]= '\0';
104
128
  }
105
129
  return(to);
106
130
} /* fn_format */