view common/portpath.c @ 1022:55a2ace0937d

Merged.
author Emanuel Schuetze <emanuel@intevation.de>
date Wed, 03 Sep 2014 09:55:13 +0200
parents 66f6361fba1b
children f110a3f6e387
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */
#include "portpath.h"
#include "strhelp.h"
#include "util.h"

#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

char *
port_dirname(char *path)
{
#ifndef _WIN32
  return dirname(path);
#else
  char drive[_MAX_DRIVE];
  char dir[_MAX_DIR];
  _splitpath(path, drive, dir, NULL, NULL);
  size_t dlen = strlen(dir);
  if ((dlen > 0) &&
      ((dir[dlen-1] == '/') || (dir[dlen-1] == '\\')))
    dir[dlen-1] = '\0';
  /* We assume: drive + dir is shorter than
   * drive + dir + fname + ext */
  sprintf(path, "%s%s", drive, dir);
  return path;
#endif
}

bool
port_mkdir(const char *path)
{
#ifndef _WIN32
  return mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
#else
  wchar_t *wchar_path = utf8_to_wchar(path, strlen(path));
  bool ret;

  if (!wchar_path)
    {
      return false;
    }
  ret = create_restricted_directory (wchar_path);
  xfree (wchar_path);
  return ret;
#endif
}

char *
port_realpath(const char *path)
{
#ifndef _WIN32
  return realpath(path, NULL);
#else
  char *fp = _fullpath(NULL, path, 0);
  if (port_fileexits(fp))
    return fp;
  else
    return NULL;
#endif
}

bool
port_fileexits(char *path)
{
  int ret;
#ifndef _WIN32
  struct stat sb;
  ret = stat(path, &sb);
#else
  struct _stat sb;
  ret = _stat(path, &sb);
#endif

  if (ret == 0)
    return true;
  else
    return false;
}

bool
port_mkdir_p(const char *path)
{
  char *parent_path,
       *p;
  if (!path) {
      return false;
  }
  if (port_isdir(path)) {
      return true;
  }
  parent_path = xstrndup (path, strlen(path));
  p = strrchr(parent_path, '/');
  if (!p)
    {
      p = strrchr(parent_path, '\\');
    }
  if (!p)
    {
      return false;
    }
  *p = '\0';
  if (!port_isdir(parent_path))
    {
      port_mkdir_p(parent_path);
    }
  return port_mkdir(path);
}

bool
port_isdir(const char *path)
{
  int ret;
#ifndef _WIN32
  struct stat sb;
  ret = stat(path, &sb);
#else
  struct _stat sb;
  ret = _stat(path, &sb);
#endif

  if ((ret == 0) && S_ISDIR(sb.st_mode))
    return true;
  else
    return false;
}

http://wald.intevation.org/projects/trustbridge/