source: branches/samba-3.0/source/lib/os2ea.c@ 390

Last change on this file since 390 was 366, checked in by Herwig Bauernfeind, 16 years ago

Better fix for hardcoded \lib directory in 3.0.x

File size: 13.1 KB
Line 
1/* This file contains helper functions for OS/2 - don't try and compile on other platforms */
2
3#ifdef __OS2__
4
5#define INCL_LONGLONG
6#define INCL_DOS
7#define INCL_DOSPROCESS
8#define INCL_DOSPROFILE
9#define INCL_DOSMISC
10#define INCL_DOSMODULEMGR
11#define INCL_DOSERRORS
12//_SMB_H
13#include <os2.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <errno.h>
17#include <types.h>
18
19//YD for tmalloc
20#include <malloc.h>
21
22/* these define the attribute byte as seen by DOS */
23#define aRONLY (1L<<0) /* 0x01 */
24#define aHIDDEN (1L<<1) /* 0x02 */
25#define aSYSTEM (1L<<2) /* 0x04 */
26#define aVOLID (1L<<3) /* 0x08 */
27#define aDIR (1L<<4) /* 0x10 */
28#define aARCH (1L<<5) /* 0x20 */
29
30#ifndef TESTING
31#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
32#define CMD_KI_RDCNT (0x63)
33
34#include "local.h"
35#include "xfile.h"
36#include "pstring.h"
37#include "debug.h"
38#else
39#define DEBUG(a,b) (0)
40#endif
41
42#include <string.h>
43
44#ifndef ENOATTR
45#define ENOATTR 22
46#endif
47
48int os2_ftruncate(int fd, off_t size)
49{
50 // We call there __libc_Back_ioFileSizeSet directly instead of
51 // ftruncate to force it not to zero expanding files to optimize
52 // samba performance when copying files
53 int rc = __libc_Back_ioFileSizeSet(fd, size, 0);
54 if (rc < 0)
55 {
56 errno = -rc;
57 return -1;
58 }
59 return 0;
60}
61
62int os2_isattribute(char *path, unsigned short attr)
63{
64 HDIR hdirFindHandle = HDIR_CREATE;
65 FILEFINDBUF3 FindBuffer = {0}; /* Returned from FindFirst/Next */
66 USHORT dosattr; /* attribute to search for */
67 ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
68 ULONG ulFindCount = 1; /* Look for 1 file at a time */
69 APIRET rc = NO_ERROR; /* Return code */
70 if (attr==aARCH)
71 dosattr=MUST_HAVE_ARCHIVED;
72 else if (attr==aRONLY)
73 dosattr=MUST_HAVE_READONLY;
74 else if (attr==aSYSTEM)
75 dosattr=MUST_HAVE_SYSTEM;
76 else if (attr==aHIDDEN)
77 dosattr=MUST_HAVE_HIDDEN;
78
79 rc = DosFindFirst( path, /* File pattern - all files */
80 &hdirFindHandle, /* Directory search handle */
81 dosattr,
82 &FindBuffer, /* Result buffer */
83 ulResultBufLen, /* Result buffer length */
84 &ulFindCount, /* Number of entries to find */
85 FIL_STANDARD); /* Return Level 1 file info */
86
87 if (rc != NO_ERROR) {
88 rc = DosFindClose(hdirFindHandle); /* Close our directory handle */
89 return 1;
90
91 } else {
92 rc = DosFindClose(hdirFindHandle); /* Close our directory handle */
93 return 0;
94
95 } /* endif */
96
97}
98
99/* Functions below are based on APR random code */
100/* Licensed to the Apache Software Foundation (ASF) under one or more
101 * contributor license agreements. See the NOTICE file distributed with
102 * this work for additional information regarding copyright ownership.
103 * The ASF licenses this file to You under the Apache License, Version 2.0
104 * (the "License"); you may not use this file except in compliance with
105 * the License. You may obtain a copy of the License at
106 *
107 * http://www.apache.org/licenses/LICENSE-2.0
108 *
109 * Unless required by applicable law or agreed to in writing, software
110 * distributed under the License is distributed on an "AS IS" BASIS,
111 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
112 * See the License for the specific language governing permissions and
113 * limitations under the License.
114 */
115
116void os2_randget(char * buffer, int length)
117{
118 UCHAR randbyte();
119 unsigned int idx;
120
121 for (idx=0; idx<length; idx++)
122 buffer[idx] = randbyte();
123
124}
125
126/* A bunch of system information like memory & process stats.
127 * Not highly random but every bit helps....
128 */
129static UCHAR randbyte_sysinfo()
130{
131 UCHAR byte = 0;
132 UCHAR SysVars[100];
133 int b;
134
135 DosQuerySysInfo(1, QSV_FOREGROUND_PROCESS, SysVars, sizeof(SysVars));
136
137 for (b = 0; b < 100; b++) {
138 byte ^= SysVars[b];
139 }
140
141 return byte;
142}
143
144
145
146/* Similar in concept to randbyte_hrtimer() but accesses the CPU's internal
147 * counters which run at the CPU's MHz speed. We get separate
148 * idle / busy / interrupt cycle counts which should provide very good
149 * randomness due to interference of hardware events.
150 * This only works on newer CPUs (at least PPro or K6) and newer OS/2 versions
151 * which is why it's run-time linked.
152 */
153
154static HMODULE hDoscalls = 0;
155#define CMD_KI_RDCNT (0x63)
156
157typedef struct _CPUUTIL {
158 ULONG ulTimeLow; /* Low 32 bits of time stamp */
159 ULONG ulTimeHigh; /* High 32 bits of time stamp */
160 ULONG ulIdleLow; /* Low 32 bits of idle time */
161 ULONG ulIdleHigh; /* High 32 bits of idle time */
162 ULONG ulBusyLow; /* Low 32 bits of busy time */
163 ULONG ulBusyHigh; /* High 32 bits of busy time */
164 ULONG ulIntrLow; /* Low 32 bits of interrupt time */
165 ULONG ulIntrHigh; /* High 32 bits of interrupt time */
166} CPUUTIL;
167
168
169static UCHAR randbyte_perf()
170{
171 UCHAR byte = 0;
172 CPUUTIL util;
173 int c;
174
175#ifndef __INNOTEK_LIBC__
176 if (hDoscalls == 0) {
177 char failed_module[20];
178 ULONG rc;
179
180 rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS",
181 &hDoscalls);
182
183 if (rc == 0) {
184 rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&DosPerfSysCall);
185
186 if (rc) {
187 DosPerfSysCall = NULL;
188 }
189 }
190 }
191
192 if (DosPerfSysCall) {
193 if (DosPerfSysCall(CMD_KI_RDCNT, (ULONG)&util, 0, 0) == 0) {
194 for (c = 0; c < sizeof(util); c++) {
195 byte ^= ((UCHAR *)&util)[c];
196 }
197 }
198 else {
199 DosPerfSysCall = NULL;
200 }
201 }
202#endif
203 return byte;
204}
205
206
207
208UCHAR randbyte()
209{
210 return randbyte_sysinfo() ^ randbyte_perf();
211}
212
213
214void maperrno(int rc)
215{
216 switch (rc)
217 {
218 case ERROR_PATH_NOT_FOUND :
219 case ERROR_FILE_NOT_FOUND : errno = ENOENT; break;
220 case ERROR_INVALID_HANDLE : errno = EBADF; break;
221 case ERROR_ACCESS_DENIED : errno = EACCES; break;
222 case ERROR_BUFFER_OVERFLOW :
223 case ERROR_NOT_ENOUGH_MEMORY : errno = ERANGE; break;
224 case ERROR_INVALID_EA_NAME : errno = ENOATTR; break;
225 case ERROR_INVALID_LEVEL :
226 case ERROR_INVALID_PARAMETER : errno = EINVAL; break;
227 case ERROR_SHARING_VIOLATION : errno = EACCES; break;
228 default : errno = EINVAL;
229 }
230}
231
232ssize_t unigetxattr (const char *path, int file, const char *name, void *value, size_t size)
233{
234 int rc, namelen;
235 EAOP2 eaop2 = {0};
236 PGEA2LIST pgea2list = NULL;
237 PFEA2LIST pfea2list = NULL;
238 char * p;
239
240 if ((!path && !file) || !name)
241 {
242 errno = EINVAL;
243 return -1;
244 }
245 namelen = strlen(name);
246 if (namelen > 0xFF)
247 {
248 errno = EINVAL;
249 return -1;
250 }
251 pgea2list = (PGEA2LIST)calloc(sizeof(GEA2LIST) + namelen + 1, 1);
252 pgea2list->list[0].oNextEntryOffset = 0;
253 pgea2list->list[0].cbName = namelen;
254 strcpy(pgea2list->list[0].szName, name);
255 pgea2list->cbList = sizeof(GEA2LIST) + namelen;
256
257 // max ea is 64kb
258 pfea2list = (PFEA2LIST)calloc(sizeof(FEA2LIST) + 0x10000, 1);
259 pfea2list->cbList = sizeof(FEA2LIST) + 0x10000;
260
261 eaop2.fpGEA2List = pgea2list;
262 eaop2.fpFEA2List = pfea2list;
263 eaop2.oError = 0;
264 do
265 {
266 if (path)
267 {
268 char npath[CCHMAXPATH + 1] = {0};
269 strncpy(npath, path, CCHMAXPATH);
270 for (p = npath; *p; p++)
271 {
272 if (*p == '/') *p = '\\';
273 }
274 rc = DosQueryPathInfo(npath, FIL_QUERYEASFROMLIST, &eaop2, sizeof(eaop2));
275 }
276 else
277 {
278 rc = DosQueryFileInfo( file, FIL_QUERYEASFROMLIST, &eaop2, sizeof(eaop2));
279 }
280 if (rc)
281 {
282 maperrno(rc);
283 rc = -1;
284 break;
285 }
286 if (strnicmp(pfea2list->list[0].szName, name, namelen) || pfea2list->list[0].cbValue == 0)
287 {
288 errno = ENOATTR;
289 rc = -1;
290 break;
291 }
292 rc = pfea2list->list[0].cbValue;
293 if (value)
294 {
295 if (size < rc)
296 {
297 errno = ERANGE;
298 rc = -1;
299 }
300 else
301 {
302 p = pfea2list->list[0].szName + pfea2list->list[0].cbName + 1;
303 memcpy(value, p, rc);
304 }
305 }
306 } while (0);
307 if (pgea2list)
308 {
309 free(pgea2list);
310 }
311 if (pfea2list)
312 {
313 free(pfea2list);
314 }
315 DEBUG(4,("unigetxattr : (%s:%d) %s %d\n", path ? path : "null", file, name, rc));
316 return rc;
317}
318
319ssize_t unilistxattr (const char *path, int file, char *list, size_t size)
320{
321 ssize_t gotsize = 0;
322 unsigned long ulCount = -1;
323 int rc;
324 char * buf, *p = list;
325 PFEA2 pfea;
326 FILESTATUS4 stat = {0};
327 char npath[CCHMAXPATH + 1] = {0};
328 if (!path && !file)
329 {
330 errno = EINVAL;
331 return -1;
332 }
333 if (path)
334 {
335 char * p;
336 strncpy(npath, path, CCHMAXPATH);
337 for (p = npath; *p; p++)
338 {
339 if (*p == '/') *p = '\\';
340 }
341 rc = DosQueryPathInfo(npath, FIL_QUERYEASIZE, &stat, sizeof(stat));
342 }
343 else
344 {
345 rc = DosQueryFileInfo( file, FIL_QUERYEASIZE, &stat, sizeof(stat));
346 }
347 if (rc)
348 {
349 DEBUG(4,("unilistxattr1 : (%s:%d) %d\n", path ? path : "null", file, rc));
350 maperrno(rc);
351 return -1;
352 }
353 if (stat.cbList <= 4)
354 {
355 // NO ea
356 return 0;
357 }
358 //YD DosEnumAttribute doesn't like high-mem buffers, get a low one.
359 buf = (char *)_tmalloc(stat.cbList * 2);
360 rc = DosEnumAttribute(path ? 1 : 0, path ? (PVOID)path : (PVOID)&file, 1, (PVOID)buf, stat.cbList * 2, &ulCount, 1);
361 if (rc)
362 {
363 DEBUG(4,("unilistxattr2 : (%s:%d) %d\n", path ? path : "null", file, rc));
364 maperrno(rc);
365 _tfree(buf);
366 return -1;
367 }
368 if (ulCount > 0)
369 for (pfea = (PFEA2)buf;;pfea = (PFEA2)((char *)pfea + pfea->oNextEntryOffset))
370 {
371 if (pfea->cbName > 0)
372 {
373 gotsize += pfea->cbName + 1;
374 if (p && size >= gotsize)
375 {
376 pfea->szName[pfea->cbName] = 0;
377 strcpy(p, pfea->szName);
378 p += strlen(p) + 1;
379 }
380 }
381 // had to be added to avoid crash in case of broken extended attributes
382 if (pfea->oNextEntryOffset > 0x10000)
383 {
384 DEBUG(0, ("Broken Extended Attributes detected for: %s:%d\n", path ? path : "null", file));
385 // DEBUG(0, ("Broken Extended Attributes detected for: %s:%d, Last EA:%s\n", path ? path : "null", file, pfea->szName));
386 break;
387 }
388 if (!pfea->oNextEntryOffset)
389 {
390 break;
391 }
392 }
393 _tfree(buf);
394 DEBUG(4,("unilistxattr : (%s:%d) %d\n", path ? path : "null", file, gotsize));
395 if (gotsize > size)
396 {
397 errno = ERANGE;
398 return list ? -1 : gotsize;
399 }
400 return gotsize;
401}
402
403int uniremovexattr (const char *path, int file, const char *name)
404{
405 int rc, namelen;
406 EAOP2 eaop2 = {0};
407 PFEA2LIST pfea2list = NULL;
408 char buf[300] = {0};
409
410 if ((!path && !file) || !name)
411 {
412 errno = EINVAL;
413 return -1;
414 }
415
416 namelen = strlen(name);
417 if (namelen > 0xFF)
418 {
419 errno = EINVAL;
420 return -1;
421 }
422
423 pfea2list = (PFEA2LIST)buf;
424 pfea2list->list[0].cbName = namelen;
425 pfea2list->list[0].cbValue = 0;
426 pfea2list->list[0].fEA = 0;
427 strcpy(pfea2list->list[0].szName, name);
428 pfea2list->cbList = sizeof(FEA2LIST) + namelen;
429 eaop2.fpFEA2List = pfea2list;
430
431 if (path)
432 {
433 char npath[CCHMAXPATH + 1] = {0};
434 char * p;
435 strncpy(npath, path, CCHMAXPATH);
436 for (p = npath; *p; p++)
437 {
438 if (*p == '/') *p = '\\';
439 }
440 rc = DosSetPathInfo(npath, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2), DSPI_WRTTHRU);
441 }
442 else
443 {
444 rc = DosSetFileInfo( file, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2));
445 }
446 if (rc)
447 {
448 maperrno(rc);
449 return -1;
450 }
451 return 0;
452}
453
454#ifndef XATTR_CREATE
455#define XATTR_CREATE 1
456#endif
457#ifndef XATTR_REPLACE
458#define XATTR_REPLACE 2
459#endif
460
461int unisetxattr (const char *path, int file, const char *name, const void *value, size_t size, int flags)
462{
463 int rc, namelen, totalsize;
464 EAOP2 eaop2 = {0};
465 PFEA2LIST pfea2list = NULL;
466 char * p;
467
468 if ((!path && !file) || !name || (!value && size))
469 {
470 errno = EINVAL;
471 return -1;
472 }
473 namelen = strlen(name);
474 if (namelen > 0xFF)
475 {
476 errno = EINVAL;
477 return -1;
478 }
479
480 if (flags & (XATTR_CREATE | XATTR_REPLACE))
481 {
482 ssize_t esize = unigetxattr(path, file, name, 0, 0);
483 if (flags & XATTR_CREATE && esize > 0)
484 {
485 errno = EEXIST;
486 return -1;
487 }
488 if (flags & XATTR_REPLACE && esize < 0)
489 {
490 errno = ENOATTR;
491 return -1;
492 }
493 }
494
495 totalsize = sizeof(FEA2LIST) + size + namelen + 1;
496
497 pfea2list = (PFEA2LIST)calloc(totalsize, 1);
498 pfea2list->cbList = totalsize;
499 pfea2list->list[0].oNextEntryOffset = 0;
500 pfea2list->list[0].cbName = namelen;
501 pfea2list->list[0].cbValue = size;
502 strcpy(pfea2list->list[0].szName, name);
503 if (value)
504 {
505 memcpy(pfea2list->list[0].szName + namelen + 1, value, size);
506 }
507 eaop2.fpFEA2List = pfea2list;
508
509 if (path)
510 {
511 char npath[CCHMAXPATH + 1] = {0};
512 char * p;
513 strncpy(npath, path, CCHMAXPATH);
514 for (p = npath; *p; p++)
515 {
516 if (*p == '/') *p = '\\';
517 }
518 rc = DosSetPathInfo(npath, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2), DSPI_WRTTHRU);
519 }
520 else
521 {
522 rc = DosSetFileInfo( file, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2));
523 }
524 free(pfea2list);
525 if (rc)
526 {
527 maperrno(rc);
528 return -1;
529 }
530 return 0;
531}
532
533#endif
Note: See TracBrowser for help on using the repository browser.