~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201207201942

« back to all changes in this revision

Viewing changes to lib/file/filePosix.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-10-18 12:28:19 UTC
  • mfrom: (1.1.7 upstream) (2.4.9 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091018122819-00vqew6m0ztpqcqp
Tags: 2009.10.15-201664-1
MergingĀ upstreamĀ versionĀ 2009.10.15-201664.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
# endif
33
33
# include <limits.h>
34
34
# include <stdio.h>      /* Needed before sys/mnttab.h in Solaris */
35
 
# ifdef sun
 
35
# if defined(sun)
36
36
#  include <sys/mnttab.h>
37
37
# elif __APPLE__
38
38
#  include <sys/mount.h>
41
41
# endif
42
42
#include <signal.h>
43
43
#endif
 
44
#if defined(GLIBC_VERSION_24)
 
45
#define _GNU_SOURCE
 
46
#endif
44
47
#include <unistd.h>
45
48
#include <errno.h>
46
49
#include <sys/stat.h>
48
51
#include <stdlib.h>
49
52
#include <fcntl.h>
50
53
#include <dirent.h>
51
 
#ifdef __linux__
 
54
#if defined(__linux__)
52
55
#   include <pwd.h>
53
56
#endif
54
57
 
72
75
#endif
73
76
static char *FilePosixNearestExistingAncestor(char const *path);
74
77
 
75
 
# ifdef VMX86_SERVER
76
 
#define VMFS2CONST 456
 
78
#if defined(VMX86_SERVER)
77
79
#define VMFS3CONST 256
78
80
#include "hostType.h"
79
81
/* Needed for VMFS implementation of File_GetFreeSpace() */
81
83
# endif
82
84
#endif
83
85
 
84
 
#ifdef VMX86_SERVER
 
86
#if defined(VMX86_SERVER)
85
87
#include "fs_user.h"
86
88
#endif
87
89
 
280
282
   int err;
281
283
   struct stat statbuf;
282
284
 
 
285
#if defined(GLIBC_VERSION_24)
 
286
   char *path;
 
287
 
 
288
   if (fileData == NULL) {
 
289
      if (!PosixConvertToCurrent(pathName, &path)) {
 
290
         return -1;
 
291
      }
 
292
      ret = euidaccess(path, F_OK);
 
293
      free(path);
 
294
 
 
295
      return ret;
 
296
   }
 
297
#endif
 
298
 
283
299
   if (Posix_Stat(pathName, &statbuf) == -1) {
284
300
      err = errno;
285
301
   } else {
1005
1021
   if (statfsbuf.f_type == VMFS_MAGIC_NUMBER) {
1006
1022
      int fd;
1007
1023
      FS_FreeSpaceArgs args = { 0 };
1008
 
      Unicode directory = NULL;
1009
 
 
1010
 
      File_SplitName(fullPath, NULL, &directory, NULL);
1011
 
      /* Must use an ioctl() to get free space for a VMFS file. */
 
1024
      Unicode specialPath = NULL;
 
1025
 
 
1026
      /*
 
1027
       * If the file exists and can be opened we're all set. If the file
 
1028
       * doesn't exist we can use the parent directory for the ioctl.
 
1029
       * However, if the file exists and can't be opened (e.g. permissions
 
1030
       * issues) a correct answer can only be returned if the target isn't a
 
1031
       * directory. If the target is a directory its parent may be a mount
 
1032
       * point - leading across a mount point to a different file system.
 
1033
       * PR 412387
 
1034
       */
 
1035
 
1012
1036
      ret = -1;
1013
 
      fd = Posix_Open(directory, O_RDONLY, 0);
 
1037
 
 
1038
      fd = Posix_Open(fullPath, O_RDONLY, 0);
 
1039
 
 
1040
      if (fd == -1) {
 
1041
         switch (errno) {
 
1042
         case EPERM:
 
1043
         case EACCES:
 
1044
            {
 
1045
               int err = errno;
 
1046
               struct stat statbuf;
 
1047
 
 
1048
               if (Posix_Stat(fullPath, &statbuf) == -1) {
 
1049
                  errno = err;
 
1050
                  break;
 
1051
               }
 
1052
 
 
1053
               if (S_ISDIR(statbuf.st_mode)) {
 
1054
                  Warning(LGPFX" %s: directory (%s) present but inaccessible\n",
 
1055
                          __func__, UTF8(fullPath));
 
1056
                  errno = err;
 
1057
                  break;
 
1058
               }
 
1059
            }
 
1060
            /* FALLTHROUGH */
 
1061
 
 
1062
         case ENOENT:
 
1063
         default:
 
1064
            File_SplitName(fullPath, NULL, &specialPath, NULL);
 
1065
 
 
1066
            fd = Posix_Open(specialPath, O_RDONLY, 0);
 
1067
         }
 
1068
      }
 
1069
 
1014
1070
      if (fd == -1) {
1015
1071
         Warning(LGPFX" %s: open of %s failed with: %s\n", __func__,
1016
 
                 UTF8(directory), Msg_ErrString());
 
1072
                 (specialPath == NULL) ? UTF8(fullPath) : UTF8(specialPath),
 
1073
                 Msg_ErrString());
1017
1074
      } else {
1018
1075
         if (ioctl(fd, IOCTLCMD_VMFS_GET_FREE_SPACE, &args) == -1) {
1019
1076
            Warning(LGPFX" %s: ioctl on %s failed with: %s\n", __func__,
1020
 
                    UTF8(fullPath), Msg_ErrString());
 
1077
                    (specialPath == NULL) ? UTF8(fullPath) : UTF8(specialPath),
 
1078
                    Msg_ErrString());
1021
1079
         } else {
1022
1080
            ret = args.bytesFree;
1023
1081
         }
 
1082
 
1024
1083
         close(fd);
1025
1084
      }
1026
1085
 
1027
 
      Unicode_Free(directory);
 
1086
      Unicode_Free(specialPath);
1028
1087
   }
1029
1088
#endif
1030
1089
 
1663
1722
{
1664
1723
   size_t resultSize;
1665
1724
   char *result;
 
1725
   struct stat statbuf;
1666
1726
 
1667
1727
   resultSize = MAX(strlen(path), 1) + 1;
1668
1728
   result = Util_SafeMalloc(resultSize);
1676
1736
         break;
1677
1737
      }
1678
1738
 
1679
 
      if (File_Exists(result)) {
 
1739
      if (Posix_Stat(result, &statbuf) == 0) {
1680
1740
         break;
1681
1741
      }
1682
1742
 
2008
2068
 *
2009
2069
 *      Check if the given file is on a VMFS supports such a file size
2010
2070
 *
2011
 
 *      In the case of VMFS2, the largest supported file size is
2012
 
 *         456 * 1024 * B bytes
2013
 
 *
2014
2071
 *      In the case of VMFS3/4, the largest supported file size is
2015
2072
 *         256 * 1024 * B bytes
2016
2073
 *
2052
2109
   }
2053
2110
 
2054
2111
   if (strcmp(fsType, "VMFS") == 0) {
2055
 
      if (version == 2) {
2056
 
         maxFileSize = (VMFS2CONST * (uint64) blockSize * 1024);
2057
 
      } else if (version >= 3) {
 
2112
      if (version >= 3) {
2058
2113
         /* Get ready for VMFS4 and perform sanity check on version */
2059
2114
         ASSERT(version == 3 || version == 4);
2060
2115
 
2061
2116
         maxFileSize = (VMFS3CONST * (uint64) blockSize * 1024);
2062
 
      } 
 
2117
      }
2063
2118
 
2064
2119
      if (fileSize <= maxFileSize && maxFileSize != -1) {
2065
2120
         free(fsType);
2818
2873
   return (FileAttributes(pathName, &fileData) == 0) &&
2819
2874
           (fileData.fileType == FILE_TYPE_CHARDEVICE);
2820
2875
}
2821
 
 
2822
 
 
2823
 
/*
2824
 
 *----------------------------------------------------------------------------
2825
 
 *
2826
 
 * File_SupportsPrealloc --
2827
 
 *  
2828
 
 *
2829
 
 * Results:
2830
 
 *      TRUE    if APPLE or file system type is ext2/ext3/ext4
2831
 
 *      FALSE   if not supported by default in auto mode
2832
 
 *
2833
 
 * Side effects:
2834
 
 *      None
2835
 
 *
2836
 
 *----------------------------------------------------------------------------
2837
 
 */
2838
 
 
2839
 
Bool
2840
 
File_SupportsPrealloc(const char *file)
2841
 
{
2842
 
 
2843
 
#if (defined( __linux__) && !defined(VMX86_SERVER)) 
2844
 
   struct statfs statBuf;   
2845
 
   if (FileGetStats(file, FALSE, &statBuf) && 
2846
 
       statBuf.f_type == EXT4_SUPER_MAGIC) {
2847
 
       return TRUE;     
2848
 
   }
2849
 
#elif APPLE
2850
 
   return TRUE
2851
 
#endif  
2852
 
   return FALSE;
2853
 
}