3 * Copyright (C) 2002, 2004 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/filesystems/cdfs/misc.c
23 * PURPOSE: CDROM (ISO 9660) filesystem driver
24 * PROGRAMMER: Eric Kohl
28 /* INCLUDES *****************************************************************/
35 /* FUNCTIONS ****************************************************************/
38 * FUNCTION: Used with IRP to set them to TopLevelIrp field
40 * Irp = The IRP to set
41 * RETURNS: TRUE if top level was null, else FALSE
47 BOOLEAN ReturnCode
= FALSE
;
49 DPRINT("CdfsIsIrpTopLevel()\n");
51 if (IoGetTopLevelIrp() == NULL
)
53 IoSetTopLevelIrp(Irp
);
62 * FUNCTION: Allocate and fill a CDFS_IRP_CONTEXT struct in order to use it for IRP
64 * DeviceObject = Used to fill in struct
65 * Irp = The IRP that need IRP_CONTEXT struct
66 * RETURNS: NULL or PCDFS_IRP_CONTEXT
69 CdfsAllocateIrpContext(
70 PDEVICE_OBJECT DeviceObject
,
73 PCDFS_IRP_CONTEXT IrpContext
;
75 DPRINT("CdfsAllocateIrpContext()\n");
77 IrpContext
= (PCDFS_IRP_CONTEXT
)ExAllocateFromNPagedLookasideList(&CdfsGlobalData
->IrpContextLookasideList
);
78 if (IrpContext
== NULL
)
81 RtlZeroMemory(IrpContext
, sizeof(CDFS_IRP_CONTEXT
));
83 // IrpContext->Identifier.Type = NTFS_TYPE_IRP_CONTEST;
84 // IrpContext->Identifier.Size = sizeof(NTFS_IRP_CONTEXT);
85 IrpContext
->Irp
= Irp
;
86 IrpContext
->DeviceObject
= DeviceObject
;
87 IrpContext
->Stack
= IoGetCurrentIrpStackLocation(Irp
);
88 IrpContext
->MajorFunction
= IrpContext
->Stack
->MajorFunction
;
89 IrpContext
->MinorFunction
= IrpContext
->Stack
->MinorFunction
;
90 IrpContext
->FileObject
= IrpContext
->Stack
->FileObject
;
91 IrpContext
->IsTopLevel
= (IoGetTopLevelIrp() == Irp
);
92 IrpContext
->PriorityBoost
= IO_NO_INCREMENT
;
93 IrpContext
->Flags
= IRPCONTEXT_COMPLETE
;
95 if (IrpContext
->MajorFunction
== IRP_MJ_FILE_SYSTEM_CONTROL
||
96 IrpContext
->MajorFunction
== IRP_MJ_DEVICE_CONTROL
||
97 IrpContext
->MajorFunction
== IRP_MJ_SHUTDOWN
||
98 (IrpContext
->MajorFunction
!= IRP_MJ_CLEANUP
&&
99 IrpContext
->MajorFunction
!= IRP_MJ_CLOSE
&&
100 IoIsOperationSynchronous(Irp
)))
102 IrpContext
->Flags
|= IRPCONTEXT_CANWAIT
;
110 CdfsSwapString(PWCHAR Out
,
114 PUCHAR t
= (PUCHAR
)Out
;
117 for (i
= 0; i
< Count
; i
+= 2)
121 if (t
[i
+1] == 0 && t
[i
] == ';')
124 if ((i
>2)&&(t
[i
-2] == '.'))
135 CdfsDateTimeToSystemTime(PFCB Fcb
,
136 PLARGE_INTEGER SystemTime
)
138 TIME_FIELDS TimeFields
;
139 LARGE_INTEGER LocalTime
;
141 TimeFields
.Milliseconds
= 0;
142 TimeFields
.Second
= Fcb
->Entry
.Second
;
143 TimeFields
.Minute
= Fcb
->Entry
.Minute
;
144 TimeFields
.Hour
= Fcb
->Entry
.Hour
;
146 TimeFields
.Day
= Fcb
->Entry
.Day
;
147 TimeFields
.Month
= Fcb
->Entry
.Month
;
148 TimeFields
.Year
= Fcb
->Entry
.Year
+ 1900;
150 RtlTimeFieldsToTime(&TimeFields
,
152 ExLocalTimeToSystemTime(&LocalTime
, SystemTime
);
157 CdfsFileFlagsToAttributes(PFCB Fcb
,
158 PULONG FileAttributes
)
160 /* FIXME: Fix attributes */
162 *FileAttributes
= // FILE_ATTRIBUTE_READONLY |
163 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_HIDDEN
) ? FILE_ATTRIBUTE_HIDDEN
: 0) |
164 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_DIRECTORY
) ? FILE_ATTRIBUTE_DIRECTORY
: 0) |
165 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_SYSTEM
) ? FILE_ATTRIBUTE_SYSTEM
: 0) |
166 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_READONLY
) ? FILE_ATTRIBUTE_READONLY
: 0);
170 CdfsIsNameLegalDOS8Dot3(IN UNICODE_STRING FileName
175 CHAR DbcsNameBuffer
[12];
177 /* 8dot3 filename is max 12 length */
178 if (FileName
.Length
/ sizeof(WCHAR
) > 12)
183 ASSERT(FileName
.Length
>= sizeof(WCHAR
));
184 for (i
= 0; i
< FileName
.Length
/ sizeof(WCHAR
) ; i
++)
186 /* Don't allow spaces in FileName */
187 if (FileName
.Buffer
[i
] == L
' ')
191 /* If FileName is finishing with a dot, remove it */
192 if (FileName
.Buffer
[FileName
.Length
/ sizeof(WCHAR
) - 1] == '.')
194 FileName
.Length
-= sizeof(WCHAR
);
197 /* Finally, convert the string to call the FsRtl function */
198 RtlInitEmptyAnsiString(&DbcsName
, DbcsNameBuffer
, sizeof(DbcsNameBuffer
));
199 if (!NT_SUCCESS(RtlUnicodeStringToCountedOemString(&DbcsName
,
206 return FsRtlIsFatDbcsLegal(DbcsName
, FALSE
, FALSE
, FALSE
);
210 CdfsShortNameCacheGet
212 PLARGE_INTEGER StreamOffset
,
213 PUNICODE_STRING LongName
,
214 PUNICODE_STRING ShortName
)
217 PCDFS_SHORT_NAME ShortNameEntry
;
218 GENERATE_NAME_CONTEXT Context
= { 0 };
220 DPRINT("CdfsShortNameCacheGet(%I64d,%wZ)\n", StreamOffset
->QuadPart
, LongName
);
222 /* Get the name list resource */
223 ExAcquireResourceExclusiveLite(&DirectoryFcb
->NameListResource
, TRUE
);
225 /* Try to find the name in our cache */
226 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
227 Entry
!= &DirectoryFcb
->ShortNameList
;
228 Entry
= Entry
->Flink
)
230 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
231 if (ShortNameEntry
->StreamOffset
.QuadPart
== StreamOffset
->QuadPart
)
234 RtlCopyUnicodeString(ShortName
, &ShortNameEntry
->Name
);
235 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
236 DPRINT("Yield short name %wZ from cache\n", ShortName
);
242 if (!CdfsIsNameLegalDOS8Dot3(*LongName
))
244 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
248 /* copy short name */
249 RtlUpcaseUnicodeString
255 DPRINT("Initial Guess %wZ\n", ShortName
);
257 /* Make it unique by scanning the cache and bumping */
258 /* Note that incrementing the ambiguous name is enough, since we add new
259 * entries at the tail. We'll scan over all collisions. */
260 /* XXX could perform better. */
261 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
262 Entry
!= &DirectoryFcb
->ShortNameList
;
263 Entry
= Entry
->Flink
)
265 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
266 if (RtlCompareUnicodeString
268 &ShortNameEntry
->Name
,
269 TRUE
) == 0) /* Match */
271 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
272 DPRINT("Collide; try %wZ\n", ShortName
);
276 /* We've scanned over all entries and now have a unique one. Cache it. */
277 ShortNameEntry
= ExAllocatePoolWithTag(PagedPool
,
278 sizeof(CDFS_SHORT_NAME
),
279 CDFS_SHORT_NAME_TAG
);
282 /* We couldn't cache it, but we can return it. We run the risk of
283 * generating a non-unique name later. */
284 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
285 DPRINT1("Couldn't cache potentially clashing 8.3 name %wZ\n", ShortName
);
289 ShortNameEntry
->StreamOffset
= *StreamOffset
;
290 RtlInitEmptyUnicodeString(&ShortNameEntry
->Name
,
291 ShortNameEntry
->NameBuffer
,
292 sizeof(ShortNameEntry
->NameBuffer
));
293 RtlCopyUnicodeString(&ShortNameEntry
->Name
, ShortName
);
294 InsertTailList(&DirectoryFcb
->ShortNameList
, &ShortNameEntry
->Entry
);
295 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
297 DPRINT("Returning short name %wZ for long name %wZ\n", ShortName
, LongName
);