Beispiel #1
0
char * directory_file_name (char * dst, const char *src)
{
  /* Process as Unix format: just remove test the final slash. */
  char *end;
  DBUG_ASSERT(strlen(src) < (FN_REFLEN + 1));

  if (src[0] == 0)
    src= (char*) ".";				/* Use empty as current */
  end= my_stpnmov(dst, src, FN_REFLEN + 1);
  if (end[-1] != FN_LIBCHAR)
  {
    end[0]=FN_LIBCHAR;				/* Add last '/' */
    end[1]='\0';
  }
  return dst;
}
static char *process_str_arg(const CHARSET_INFO *cs, char *to, char *end,
                             size_t width, char *par, uint print_type)
{
  int well_formed_error;
  size_t plen, left_len= (size_t) (end - to) + 1;
  if (!par)
    par = (char*) "(null)";

  plen= strnlen(par, width);
  if (left_len <= plen)
    plen = left_len - 1;
  plen= cs->cset->well_formed_len(cs, par, par + plen,
                                  width, &well_formed_error);
  if (print_type & ESCAPED_ARG)
    to= backtick_string(cs, to, end, par, plen, '`');
  else
    to= my_stpnmov(to,par,plen);
  return to;
}
static char *backtick_string(const CHARSET_INFO *cs, char *to, char *end,
                             char *par, size_t par_len, char quote_char)
{
  uint char_len;
  char *start= to;
  char *par_end= par + par_len;
  size_t buff_length= (size_t) (end - to);

  if (buff_length <= par_len)
    goto err;
  *start++= quote_char;

  for ( ; par < par_end; par+= char_len)
  {
    if (!(char_len= my_mbcharlen_ptr(cs, par, par_end)))
      goto err;
    if (char_len == 1 && *par == quote_char)
    {
      if (start + 1 >= end)
        goto err;
      *start++= quote_char;
    }
    if (start + char_len >= end)
      goto err;
    start= my_stpnmov(start, par, char_len);
  }

  if (start + 1 >= end)
    goto err;
  *start++= quote_char;
  return start;

err:
    *to='\0';
  return to;
}
Beispiel #4
0
MY_DIR	*my_dir(const char *path, myf MyFlags)
{
  char          *buffer;
  MY_DIR        *result= 0;
  FILEINFO      finfo;
  DYNAMIC_ARRAY *dir_entries_storage;
  MEM_ROOT      *names_storage;
  struct _finddata_t find;
  ushort	mode;
  char		tmp_path[FN_REFLEN],*tmp_file,attrib;
#ifdef _WIN64
  __int64       handle;
#else
  long		handle;
#endif
  DBUG_ENTER("my_dir");
  DBUG_PRINT("my",("path: '%s' stat: %d  MyFlags: %d",path,MyFlags));

  /* Put LIB-CHAR as last path-character if not there */
  tmp_file=tmp_path;
  if (!*path)
    *tmp_file++ ='.';				/* From current dir */
  tmp_file= my_stpnmov(tmp_file, path, FN_REFLEN-5);
  if (tmp_file[-1] == FN_DEVCHAR)
    *tmp_file++= '.';				/* From current dev-dir */
  if (tmp_file[-1] != FN_LIBCHAR)
    *tmp_file++ =FN_LIBCHAR;
  tmp_file[0]='*';				/* Windows needs this !??? */
  tmp_file[1]='.';
  tmp_file[2]='*';
  tmp_file[3]='\0';

  if (!(buffer= my_malloc(key_memory_MY_DIR,
                          ALIGN_SIZE(sizeof(MY_DIR)) + 
                          ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) +
                          sizeof(MEM_ROOT), MyFlags)))
    goto error;

  dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); 
  names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) +
                             ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)));
  
  if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO),
                            NULL,               /* init_buffer */
                            ENTRIES_START_SIZE, ENTRIES_INCREMENT))
  {
    my_free(buffer);
    goto error;
  }
  init_alloc_root(key_memory_MY_DIR, names_storage, NAMES_START_SIZE, NAMES_START_SIZE);
  
  /* MY_DIR structure is allocated and completly initialized at this point */
  result= (MY_DIR*)buffer;

  if ((handle=_findfirst(tmp_path,&find)) == -1L)
  {
    DBUG_PRINT("info", ("findfirst returned error, errno: %d", errno));
    if  (errno != EINVAL)
      goto error;
    /*
      Could not read the directory, no read access.
      Probably because by "chmod -r".
      continue and return zero files in dir
    */
  }
  else
  {

    do
    {
      attrib= find.attrib;
      /*
        Do not show hidden and system files which Windows sometimes create.
        Note. Because Borland's findfirst() is called with the third
        argument = 0 hidden/system files are excluded from the search.
      */
      if (attrib & (_A_HIDDEN | _A_SYSTEM))
        continue;
      if (!(finfo.name= strdup_root(names_storage, find.name)))
        goto error;
      if (MyFlags & MY_WANT_STAT)
      {
        if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage,
                                                 sizeof(MY_STAT))))
          goto error;

        memset(finfo.mystat, 0, sizeof(MY_STAT));
        finfo.mystat->st_size=find.size;
        mode= MY_S_IREAD;
        if (!(attrib & _A_RDONLY))
          mode|= MY_S_IWRITE;
        if (attrib & _A_SUBDIR)
          mode|= MY_S_IFDIR;
        finfo.mystat->st_mode= mode;
        finfo.mystat->st_mtime= ((uint32) find.time_write);
      }
      else
        finfo.mystat= NULL;

      if (insert_dynamic(dir_entries_storage, (uchar*)&finfo))
        goto error;
    }
    while (_findnext(handle,&find) == 0);

    _findclose(handle);
  }

  result->dir_entry= (FILEINFO *)dir_entries_storage->buffer;
  result->number_off_files= dir_entries_storage->elements;

  if (!(MyFlags & MY_DONT_SORT))
    my_qsort((void *) result->dir_entry, result->number_off_files,
          sizeof(FILEINFO), (qsort_cmp) comp_names);
  DBUG_PRINT("exit", ("found %d files", result->number_off_files));
  DBUG_RETURN(result);
error:
  my_errno=errno;
  if (handle != -1)
      _findclose(handle);
  my_dirend(result);
  if (MyFlags & MY_FAE+MY_WME)
  {
    char errbuf[MYSYS_STRERROR_SIZE];
    my_error(EE_DIR, MYF(0), path,
             errno, my_strerror(errbuf, sizeof(errbuf), errno));
  }
  DBUG_RETURN((MY_DIR *) NULL);
} /* my_dir */
Beispiel #5
0
File create_temp_file(char *to, const char *dir, const char *prefix,
                      int mode, myf MyFlags)
{
    File file= -1;
#ifdef _WIN32
    TCHAR path_buf[MAX_PATH-14];
#endif

    DBUG_ENTER("create_temp_file");
    DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
#if defined(_WIN32)

    /*
      Use GetTempPath to determine path for temporary files.
      This is because the documentation for GetTempFileName
      has the following to say about this parameter:
      "If this parameter is NULL, the function fails."
    */
    if (!dir)
    {
        if(GetTempPath(sizeof(path_buf), path_buf) > 0)
            dir = path_buf;
    }
    /*
      Use GetTempFileName to generate a unique filename, create
      the file and release it's handle
       - uses up to the first three letters from prefix
    */
    if (GetTempFileName(dir, prefix, 0, to) == 0)
        DBUG_RETURN(-1);

    DBUG_PRINT("info", ("name: %s", to));

    /*
      Open the file without the "open only if file doesn't already exist"
      since the file has already been created by GetTempFileName
    */
    if ((file= my_open(to,  (mode & ~O_EXCL), MyFlags)) < 0)
    {
        /* Open failed, remove the file created by GetTempFileName */
        int tmp= my_errno();
        (void) my_delete(to, MYF(0));
        set_my_errno(tmp);
    }

#else /* mkstemp() is available on all non-Windows supported platforms. */
    {
        char prefix_buff[30];
        uint pfx_len;
        File org_file;

        pfx_len= (uint) (my_stpcpy(my_stpnmov(prefix_buff,
                                              prefix ? prefix : "tmp.",
                                              sizeof(prefix_buff)-7),"XXXXXX") -
                         prefix_buff);
        if (!dir && ! (dir =getenv("TMPDIR")))
            dir= DEFAULT_TMPDIR;
        if (strlen(dir)+ pfx_len > FN_REFLEN-2)
        {
            errno=ENAMETOOLONG;
            set_my_errno(ENAMETOOLONG);
            DBUG_RETURN(file);
        }
        my_stpcpy(convert_dirname(to,dir,NullS),prefix_buff);
        org_file=mkstemp(to);
        if (mode & O_TEMPORARY)
            (void) my_delete(to, MYF(MY_WME));
        file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
                                  EE_CANTCREATEFILE, MyFlags);
        /* If we didn't manage to register the name, remove the temp file */
        if (org_file >= 0 && file < 0)
        {
            int tmp=my_errno();
            close(org_file);
            (void) my_delete(to, MYF(MY_WME));
            set_my_errno(tmp);
        }
    }
#endif
    if (file >= 0)
    {
        mysql_mutex_lock(&THR_LOCK_open);
        my_tmp_file_created++;
        mysql_mutex_unlock(&THR_LOCK_open);
    }
    DBUG_RETURN(file);
}