Initial import of Scalos. To decrease size I have
[AROS-Contrib.git] / scalos / common / Fs / FsAbstraction.c
blob58dd752eb72e920eba987ed54141c1492e28371d
1 // FsAbstraction.c.c
2 // $Date$
3 // $Revision$
6 #include <exec/types.h>
7 #include <intuition/classes.h>
8 #include <intuition/classusr.h>
9 #include <utility/hooks.h>
10 #include <dos/dos.h>
11 #include <dos/dostags.h>
12 #include <dos/exall.h>
14 #define __USE_SYSBASE
16 #include <proto/dos.h>
17 #include <proto/exec.h>
18 #include <proto/intuition.h>
19 #include <proto/utility.h>
20 #include <proto/asl.h>
21 #include "debug.h"
22 #include <proto/scalos.h>
24 #include <clib/alib_protos.h>
26 #include <defs.h>
27 #include <scalos/scalos.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdarg.h>
33 #include "int64.h"
34 #include "FsAbstraction.h"
36 //----------------------------------------------------------------------------
38 // local data definitions
40 //----------------------------------------------------------------------------
42 // local functions
44 //----------------------------------------------------------------------------
46 // local data
48 //----------------------------------------------------------------------------
50 BOOL ScalosExamineBegin(T_ExamineData **exd)
52 #ifdef __amigaos4__
53 *exd = NULL;
54 return TRUE;
55 #else //__amigaos4__
56 *exd = AllocDosObject(DOS_FIB, NULL);
57 return (BOOL) (NULL != *exd);
58 #endif //__amigaos4__
61 //----------------------------------------------------------------------------
63 void ScalosExamineEnd(T_ExamineData **exd)
65 #ifdef __amigaos4__
66 if (exd && *exd)
68 FreeDosObject(DOS_EXAMINEDATA, *exd);
69 *exd = NULL;
71 #else //__amigaos4__
72 if (exd && *exd)
74 FreeDosObject(DOS_FIB, *exd);
75 *exd = NULL;
77 #endif //__amigaos4__
80 //----------------------------------------------------------------------------
82 BOOL ScalosExamineLock(BPTR lock, T_ExamineData **exd)
84 #ifdef __amigaos4__
85 *exd = ExamineObjectTags(EX_FileLockInput, lock,
86 TAG_END);
87 return (NULL != *exd);
88 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
89 if (DOSBase->dl_lib.lib_Version >= 51)
90 return Examine64(lock, *exd, NULL);
91 else
92 return Examine(lock, *exd);
93 #else //__amigaos4__
94 return (BOOL) Examine(lock, *exd);
95 #endif //__amigaos4__
98 //----------------------------------------------------------------------------
100 BOOL ScalosExamineFH(BPTR fh, T_ExamineData **exd)
102 #ifdef __amigaos4__
103 *exd = ExamineObjectTags(EX_FileHandleInput, fh,
104 TAG_END);
105 return (NULL != *exd);
106 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
107 if (DOSBase->dl_lib.lib_Version >= 51)
108 return ExamineFH64(fh, *exd, NULL);
109 else
110 return ExamineFH(fh, *exd);
111 #else //__amigaos4__
112 return ExamineFH(fh, *exd);
113 #endif //__amigaos4__
115 //----------------------------------------------------------------------------
117 struct DateStamp *ScalosExamineGetDate(T_ExamineData *exd)
119 #ifdef __amigaos4__
120 return &exd->Date;
121 #else //__amigaos4__
122 return &exd->fib_Date;
123 #endif //__amigaos4__
126 //----------------------------------------------------------------------------
128 ULONG ScalosExamineGetProtection(const T_ExamineData *exd)
130 #ifdef __amigaos4__
131 return exd->Protection;
132 #else //__amigaos4__
133 return (ULONG) exd->fib_Protection;
134 #endif //__amigaos4__
137 //----------------------------------------------------------------------------
139 ULONG64 ScalosExamineGetSize(const T_ExamineData *exd)
141 #ifdef __amigaos4__
142 return exd->FileSize;
143 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
144 if (DOSBase->dl_lib.lib_Version >= 51)
146 ULONG64 Size = (ULONG64) exd->fib_un.fib_un_ext.fib_un_ext_Size64;
148 // workaround for MorphOS 2.1 sign-extension bug:
149 // 64bit file size is negative for files with sizes between 2G and 4G
150 if (0xffffffff == ULONG64_HIGH(Size))
151 Size = MakeU64(ULONG64_LOW(Size));
153 return Size;
155 else
157 return MakeU64(exd->fib_Size);
159 #else //__amigaos4__
160 return MakeU64(exd->fib_Size);
161 #endif //__amigaos4__
164 //----------------------------------------------------------------------------
166 ULONG ScalosExamineGetBlockCount(const T_ExamineData *exd)
168 #ifdef __amigaos4__
169 if (exd->FileSize >= 0)
171 // Real block count is not available, so we calculate an estimation
172 return (511 + exd->FileSize) / 512;
174 else
175 return 1;
176 #elif defined(__MORPHOS__)
177 return exd->fib_NumBlocks;
178 #else //__amigaos4__
179 return (ULONG) exd->fib_NumBlocks;
180 #endif //__amigaos4__
183 //----------------------------------------------------------------------------
185 CONST_STRPTR ScalosExamineGetName(const T_ExamineData *exd)
187 #ifdef __amigaos4__
188 return exd->Name;
189 #else //__amigaos4__
190 return exd->fib_FileName;
191 #endif //__amigaos4__
194 //----------------------------------------------------------------------------
196 CONST_STRPTR ScalosExamineGetComment(const T_ExamineData *exd)
198 #ifdef __amigaos4__
199 return exd->Comment;
200 #else //__amigaos4__
201 return exd->fib_Comment;
202 #endif //__amigaos4__
205 //----------------------------------------------------------------------------
207 LONG ScalosExamineGetDirEntryType(const T_ExamineData *exd)
209 if (NULL == exd)
210 return ST_FILE;
211 #ifdef __amigaos4__
212 if( EXD_IS_FILE(exd) )
213 return ST_FILE;
214 else if ( EXD_IS_DIRECTORY(exd) )
215 return ST_USERDIR;
216 else if (EXD_IS_SOFTLINK(exd))
217 return ST_SOFTLINK;
218 else if (EXD_IS_PIPE(exd))
219 return ST_PIPEFILE;
220 else
221 return ST_FILE;
222 #else //__amigaos4__
223 return exd->fib_DirEntryType;
224 #endif //__amigaos4__
227 //----------------------------------------------------------------------------
229 LONG ScalosExamineGetDirEntryTypeRoot(const T_ExamineData *exd, BPTR fLock)
231 if (NULL == exd)
232 return ST_FILE;
233 #ifdef __amigaos4__
234 if( EXD_IS_FILE(exd) )
235 return ST_FILE;
236 else if ( EXD_IS_DIRECTORY(exd) )
238 // Special handling (expensive!) since AmigaOS4 ExamineObject() has no means to recognize volumes
239 BPTR parentLock = ParentDir(fLock);
240 if (parentLock)
242 UnLock(parentLock);
243 return ST_USERDIR;
245 else
247 return ST_ROOT;
250 else if (EXD_IS_SOFTLINK(exd))
251 return ST_SOFTLINK;
252 else if (EXD_IS_PIPE(exd))
253 return ST_PIPEFILE;
254 else
255 return ST_FILE;
256 #else //__amigaos4__
257 return exd->fib_DirEntryType;
258 #endif //__amigaos4__
261 //----------------------------------------------------------------------------
263 LONG ScalosExamineIsDrawer(const T_ExamineData *exd)
265 #ifdef __amigaos4__
266 return EXD_IS_DIRECTORY(exd) ? TRUE : FALSE;
267 #else //__amigaos4__
268 return exd->fib_DirEntryType > 0;
269 #endif //__amigaos4__
272 //----------------------------------------------------------------------------
274 ULONG ScalosExamineGetDirUID(const T_ExamineData *exd)
276 #ifdef __amigaos4__
277 return exd->OwnerUID;
278 #else //__amigaos4__
279 return exd->fib_OwnerUID;
280 #endif //__amigaos4__
282 //----------------------------------------------------------------------------
284 ULONG ScalosExamineGetDirGID(const T_ExamineData *exd)
286 #ifdef __amigaos4__
287 return exd->OwnerGID;
288 #else //__amigaos4__
289 return exd->fib_OwnerGID;
290 #endif //__amigaos4__
292 //----------------------------------------------------------------------------
294 SLONG64 ScalosSeek(BPTR fh, SLONG64 pos, LONG mode)
296 #ifdef __amigaos4__
297 int64 oldPos = GetFilePosition(fh);
299 if (-1LL != oldPos && !ChangeFilePosition(fh, pos, mode))
301 oldPos = -1LL;
303 return oldPos;
304 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
305 if (DOSBase->dl_lib.lib_Version >= 51)
306 return Seek64(fh, pos, mode);
307 else
308 return Seek(fh, ULONG64_LOW(pos), mode);
309 #else //__amigaos4__
311 #ifdef __GNUC__
312 return (SLONG64) Seek(fh, ULONG64_LOW(pos), mode);
313 #else //__GNUC__
315 SLONG64 res64;
317 SLONG64_HIGH(res64) = 0;
318 SLONG64_LOW(res64) = Seek(fh, ULONG64_LOW(pos), mode);
320 return res64;
321 #endif //__GNUC__
323 #endif //__amigaos4__
326 //----------------------------------------------------------------------------
328 BOOL ScalosExamineDirBegin(BPTR lock, T_ExamineDirHandle *edh)
330 #ifdef __amigaos4__
331 *edh = ObtainDirContextTags(EX_FileLockInput, lock,
332 EX_DataFields, EXF_ALL,
333 TAG_END);
334 return (NULL != *edh);
335 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
336 *edh = AllocDosObject(DOS_FIB, NULL);
337 if (NULL == *edh)
338 return FALSE;
339 if (DOSBase->dl_lib.lib_Version >= 51)
341 if (!Examine64(lock, *edh, NULL))
343 FreeDosObject(DOS_FIB, *edh);
344 *edh = NULL;
347 else
349 if (!Examine(lock, *edh))
351 FreeDosObject(DOS_FIB, *edh);
352 *edh = NULL;
355 d1(KPrintF("%s/%s/%ld: fib=%08lx\n", __FILE__, __FUNC__, __LINE__, *edh));
356 return (NULL != *edh);
357 #else //__amigaos4__
358 *edh = AllocDosObject(DOS_FIB, NULL);
359 if (NULL == *edh)
360 return FALSE;
361 if (!Examine(lock, *edh))
363 FreeDosObject(DOS_FIB, *edh);
364 *edh = NULL;
366 return (BOOL) (NULL != *edh);
367 #endif //__amigaos4__
370 //----------------------------------------------------------------------------
372 void ScalosExamineDirEnd(T_ExamineDirHandle *edh)
374 #ifdef __amigaos4__
375 if (edh && *edh)
377 ReleaseDirContext(*edh);
378 *edh = NULL;
380 #else //__amigaos4__
381 if (edh && *edh)
383 FreeDosObject(DOS_FIB, *edh);
384 *edh = NULL;
386 #endif //__amigaos4__
389 //----------------------------------------------------------------------------
391 BOOL ScalosExamineDir(BPTR lock, T_ExamineDirHandle *edh, T_ExamineData **exd)
393 #ifdef __amigaos4__
394 (void) lock;
395 *exd = ExamineDir(*edh);
396 return (NULL != *exd);
397 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
398 if (DOSBase->dl_lib.lib_Version >= 51)
400 if (ExNext64(lock, *edh, NULL))
402 *exd = *edh;
404 else
406 return FALSE;
409 else
411 if (ExNext(lock, *edh))
413 *exd = *edh;
415 else
417 return FALSE;
420 return TRUE;
421 #else
422 if (ExNext(lock, *edh))
424 *exd = *edh;
426 else
428 return FALSE;
430 return TRUE;
431 #endif //__amigaos4__
434 //----------------------------------------------------------------------------
436 BOOL ScalosReadLink(BPTR srcDirLock, CONST_STRPTR srcName, STRPTR Buffer, size_t BuffLen)
438 #ifdef __amigaos4__
439 BOOL Success = FALSE;
440 BPTR oldDir = CurrentDir(srcDirLock);
441 struct ExamineData *exd = NULL;
442 APTR dirContext= NULL;
444 do {
445 if (ParsePatternNoCase(srcName, Buffer, BuffLen) < 0)
446 break;
448 dirContext = ObtainDirContextTags(EX_FileLockInput, srcDirLock,
449 EX_DataFields, EXF_NAME | EXF_LINK | EXF_TYPE,
450 EX_MatchString, Buffer,
451 TAG_END);
452 if (NULL == dirContext)
453 break;
455 exd = ExamineDir(dirContext);
456 if (NULL == exd)
457 break;
459 if (!EXD_IS_LINK(exd))
460 break;
462 stccpy(Buffer, exd->Link, BuffLen);
463 Success = TRUE;
464 } while (0);
466 if (dirContext)
467 ReleaseDirContext(dirContext);
469 CurrentDir(oldDir);
471 return Success;
472 #else //__amigaos4__
473 BOOL Success = FALSE;
474 struct DevProc *devproc = NULL;
476 do {
477 if (!NameFromLock(srcDirLock, Buffer, BuffLen))
479 break;
482 devproc = GetDeviceProc(Buffer, NULL);
483 if (NULL == devproc)
485 break;
488 if (!ReadLink(devproc->dvp_Port, srcDirLock, (STRPTR) srcName,
489 Buffer, BuffLen))
491 break;
493 Success = TRUE;
494 } while (0);
496 if (devproc)
497 FreeDeviceProc(devproc);
499 return Success;
500 #endif //__amigaos4__
503 //----------------------------------------------------------------------------
505 void ScalosDosPacketExamineDir(struct DosPacket *pkt, BPTR dirLock, T_ExamineDirHandle *edh)
507 #ifdef __amigaos4__
508 pkt->dp_Type = ACTION_EXAMINEDATA_DIR;
509 pkt->dp_Arg1 = MKBADDR(*edh);
511 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
513 if (DOSBase->dl_lib.lib_Version >= 51)
515 pkt->dp_Type = ACTION_EXAMINE_NEXT64;
516 pkt->dp_Arg1 = dirLock;
517 pkt->dp_Arg2 = MKBADDR(*edh);
518 pkt->dp_Arg3 = NULL;
520 else
522 pkt->dp_Type = ACTION_EXAMINE_NEXT;
523 pkt->dp_Arg1 = dirLock;
524 pkt->dp_Arg2 = MKBADDR(*edh);
526 return;
527 #else
528 pkt->dp_Type = ACTION_EXAMINE_NEXT;
529 pkt->dp_Arg1 = (IPTR) dirLock;
530 pkt->dp_Arg2 = (IPTR) MKBADDR(*edh);
531 #endif
534 //----------------------------------------------------------------------------
536 BOOL ScalosDosPacketExamineDirResult(struct StandardPacket *pkt, T_ExamineData **exd,
537 T_ExamineDirHandle *edh)
539 #ifdef __amigaos4__
540 if (pkt->sp_Pkt.dp_Res1)
542 *exd = (T_ExamineData *) pkt->sp_Pkt.dp_Res1;
544 else
545 *exd = NULL;
547 return 0 != pkt->sp_Pkt.dp_Res1;
548 #else
549 *exd = (T_ExamineData *) *edh;
551 return (BOOL) (0 != pkt->sp_Pkt.dp_Res1);
552 #endif
555 //----------------------------------------------------------------------------
557 BOOL ScalosSupportsExamineDir(struct MsgPort *fileSysPort, BPTR dirLock, T_ExamineDirHandle *edh)
559 #ifdef __amigaos4__
560 LONG rc;
562 rc = DoPkt1(fileSysPort,
563 ACTION_EXAMINEDATA_DIR,
564 MKBADDR(edh));
566 d1(KPrintF("%s/%s/%ld: rc=%ld\n", __FILE__, __FUNC__, __LINE__, rc));
567 return (BOOL) (0 != rc);
568 #elif defined(__MORPHOS__) && defined(ACTION_EXAMINE_NEXT64)
569 LONG rc;
571 d1(KPrintF("%s/%s/%ld: fib=%08lx\n", __FILE__, __FUNC__, __LINE__, *edh));
573 rc = DoPkt3(fileSysPort,
574 ACTION_EXAMINE_OBJECT64,
575 dirLock,
576 MKBADDR(*edh),
577 NULL);
579 d1(KPrintF("%s/%s/%ld: rc=%ld\n", __FILE__, __FUNC__, __LINE__, rc));
580 return (BOOL) (0 != rc);
581 #else
582 return FALSE;
583 #endif /* __MORPHOS__ */
586 //----------------------------------------------------------------------------