summaryrefslogtreecommitdiff
path: root/portable/src/UNIX/PANSIFileSystemUNIXImpl.c
blob: 2c561b19709d078adccdb7f451e25b199fd6db6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*---------------------------------------------------------------------------*
 *  PANSIFileSystemUNIXImpl.c  *
 *                                                                           *
 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
 *                                                                           *
 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
 *  you may not use this file except in compliance with the License.         *
 *                                                                           *
 *  You may obtain a copy of the License at                                  *
 *      http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                           *
 *  Unless required by applicable law or agreed to in writing, software      *
 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
 *  See the License for the specific language governing permissions and      *
 *  limitations under the License.                                           *
 *                                                                           *
 *---------------------------------------------------------------------------*/

#include <sys/types.h>
#include <sys/stat.h>

#include "errno.h"
#include "PFileSystemImpl.h"
#include "PANSIFileSystem.h"
#include "PANSIFileSystemImpl.h"
#include "phashtable.h"
#include "LCHAR.h"
#include "plog.h"

ESR_ReturnCode PANSIFileSystemGetVirtualPathImpl(PFileSystem* self, LCHAR* path, size_t* len)
{
  PANSIFileSystemImpl* impl = (PANSIFileSystemImpl*) self;
  PHashTableEntry* entry;
  LCHAR driveLetter = 0;
  LCHAR* key;
  LCHAR* value;
  LCHAR* bestKey = NULL;
  LCHAR* bestValue = NULL;
  ESR_BOOL isAbsolute;
  ESR_ReturnCode rc;

  CHKLOG(rc, lstrtrim(path));
  CHKLOG(rc, PFileSystemCanonicalSlashes(path));
  CHKLOG(rc, PFileSystemIsAbsolutePath(path, &isAbsolute));
  if (isAbsolute && path[0] != L('/'))
  {
    /* Skip drive letters in absolute paths */
    driveLetter = path[0];
    LSTRCPY(path, path + 2);
  }
  CHKLOG(rc, PHashTableEntryGetFirst(impl->directoryMap, &entry));
  while (entry!=NULL)
  {
    CHKLOG(rc, PHashTableEntryGetKeyValue(entry, (void **)&key, (void **)&value));
    if (LSTRSTR(path, value)==path)
    {
      /* File-system handles file path */

      if (bestValue==NULL || LSTRLEN(value) > LSTRLEN(bestValue))
      {
        /* Found a better match -- the new key is a subdirectory of the previous bestKey */
        bestKey = key;
        bestValue = value;
      }
    }
    CHKLOG(rc, PHashTableEntryAdvance(&entry));
  }
  if (bestKey == NULL)
  {
    rc = ESR_INVALID_STATE;
    PLogError(L("PANSIFileSystem does not handle the specified path: %s"), path);
    goto CLEANUP;
  }

  /* Delete the real-path */
  LSTRCPY(path, path + LSTRLEN(bestValue));
  /* Insert the virtual-path */
  CHKLOG(rc, lstrinsert(bestKey, path, 0, len));

  /* Bring back the drive letter */
  if (driveLetter!=0)
  {
    CHKLOG(rc, lstrinsert(L("X:/"), path, LSTRLEN(bestKey), len));
    path[LSTRLEN(bestKey)] = driveLetter;
  }
  return ESR_SUCCESS;
 CLEANUP:
  return rc;
}
ESR_ReturnCode PANSIFileSystemMkdirImpl(PFileSystem* self, const LCHAR* path)
{
  LCHAR realPath[P_PATH_MAX];
  size_t len;
  ESR_ReturnCode rc;

  passert(path!=NULL);
  LSTRCPY(realPath, path);
  len = P_PATH_MAX;
  CHKLOG(rc, PANSIFileSystemGetRealPathImpl(self, realPath, &len));

  if (mkdir(realPath, S_IRWXU|S_IRWXG|S_IRWXO ) != 0)
    {
      switch (errno)
      {
       case EEXIST:
         return ESR_IDENTIFIER_COLLISION;
       case ENOENT:
         return ESR_NO_MATCH_ERROR;
       default:
         PLogError(L("ESR_INVALID_STATE"));
         return ESR_INVALID_STATE;
      }
    }
  return ESR_SUCCESS;
CLEANUP:
  return rc;
}

ESR_ReturnCode PANSIFileSystemGetcwdImpl(PFileSystem* self, LCHAR* path, size_t* len)
{
	ESR_ReturnCode rc;

	if (path==NULL)
	{
		rc = ESR_INVALID_ARGUMENT;
		PLogError(ESR_rc2str(rc));
		goto CLEANUP;
	}
    if (getcwd(path, *len) == NULL)
    {
      switch (errno)
      {
       case ERANGE:
         *len = P_PATH_MAX;
         return ESR_BUFFER_OVERFLOW;
       case ENOMEM:
       default:
         PLogError(L("ESR_INVALID_STATE"));
         return ESR_INVALID_STATE;
      }
    }

	CHKLOG(rc, PANSIFileSystemGetVirtualPathImpl(self, path, len));
	return ESR_SUCCESS;
CLEANUP:
	return rc;
}

ESR_ReturnCode PANSIFileSystemChdirImpl(PFileSystem* self, const LCHAR* path)
{
	LCHAR realPath[P_PATH_MAX];
	size_t len;
	ESR_ReturnCode rc;

	passert(path!=NULL);
	LSTRCPY(realPath, path);
	len = P_PATH_MAX;
	CHKLOG(rc, PANSIFileSystemGetRealPathImpl(self, realPath, &len));

	if ((*path != '\0') && (chdir(realPath) != 0))
		return ESR_NO_MATCH_ERROR;
	return ESR_SUCCESS;
CLEANUP:
	return rc;
}