~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/internal/mf_format.cc

  • Committer: Brian Aker
  • Date: 2009-12-29 01:38:38 UTC
  • mfrom: (1251.1.1 drizzle)
  • Revision ID: brian@gaz-20091229013838-03kb2z5xbqw03ddt
Merge of Diego fix.

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 <mystrings/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
40
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
30
41
  const char *startpos = name;
37
48
  if (length == 0 || (flag & MY_REPLACE_DIR))
38
49
  {
39
50
    /* Use given directory */
40
 
    convert_dirname(dev,dir,NullS);             /* Fix to this OS */
 
51
    convert_dirname(dev,dir,NULL);              /* Fix to this OS */
41
52
  }
42
53
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
43
54
  {
44
55
    /* Put 'dir' before the given path */
45
 
    strmake(buff,dev,sizeof(buff)-1);
46
 
    pos=convert_dirname(dev,dir,NullS);
47
 
    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));
48
59
  }
49
60
 
50
 
  if (flag & MY_PACK_FILENAME)
51
 
    pack_dirname(dev,dev);                      /* Put in ./.. and ~/.. */
52
61
  if (flag & MY_UNPACK_FILENAME)
53
62
    (void) unpack_dirname(dev,dev);             /* Replace ~/.. with dir */
54
63
 
55
64
  if (!(flag & MY_APPEND_EXT) &&
56
 
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
 
65
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
57
66
  {
58
67
    if ((flag & MY_REPLACE_EXT) == 0)           /* If we should keep old ext */
59
68
    {
77
86
    /* To long path, return original or NULL */
78
87
    size_t tmp_length;
79
88
    if (flag & MY_SAFE_PATH)
80
 
      return NullS;
81
 
    tmp_length= strlength(startpos);
82
 
    (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';
83
93
  }
84
94
  else
85
95
  {
86
96
    if (to == startpos)
87
97
    {
88
 
      memcpy(buff, name, length); /* Save name for last copy */
 
98
      memmove(buff, name, length); /* Save name for last copy */
89
99
      name=buff;
90
100
    }
91
 
    pos=strmake(stpcpy(to,dev),name,length);
92
 
    (void) stpcpy(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 */
93
104
  }
94
105
  /*
95
106
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
96
107
    realpath if the file is a symbolic link
97
108
  */
98
109
  if (flag & MY_RETURN_REAL_PATH)
99
 
    (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
100
 
                                   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
  }
101
122
  else if (flag & MY_RESOLVE_SYMLINKS)
102
123
  {
103
 
    stpcpy(buff,to);
104
 
    (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';
105
128
  }
106
129
  return(to);
107
130
} /* fn_format */