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: services/fs/cdfs/misc.c
23 * PURPOSE: CDROM (ISO 9660) filesystem driver
24 * PROGRAMMER: Eric Kohl
28 /* INCLUDES *****************************************************************/
35 /* FUNCTIONS ****************************************************************/
38 CdfsSwapString(PWCHAR Out
,
42 PUCHAR t
= (PUCHAR
)Out
;
45 for (i
= 0; i
< Count
; i
+= 2)
49 if (t
[i
+1] == 0 && t
[i
] == ';')
52 if ((i
>2)&&(t
[i
-2] == '.'))
63 CdfsDateTimeToSystemTime(PFCB Fcb
,
64 PLARGE_INTEGER SystemTime
)
66 TIME_FIELDS TimeFields
;
67 LARGE_INTEGER LocalTime
;
69 TimeFields
.Milliseconds
= 0;
70 TimeFields
.Second
= Fcb
->Entry
.Second
;
71 TimeFields
.Minute
= Fcb
->Entry
.Minute
;
72 TimeFields
.Hour
= Fcb
->Entry
.Hour
;
74 TimeFields
.Day
= Fcb
->Entry
.Day
;
75 TimeFields
.Month
= Fcb
->Entry
.Month
;
76 TimeFields
.Year
= Fcb
->Entry
.Year
+ 1900;
78 RtlTimeFieldsToTime(&TimeFields
,
80 ExLocalTimeToSystemTime(&LocalTime
, SystemTime
);
85 CdfsFileFlagsToAttributes(PFCB Fcb
,
86 PULONG FileAttributes
)
88 /* FIXME: Fix attributes */
90 *FileAttributes
= // FILE_ATTRIBUTE_READONLY |
91 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_HIDDEN
) ? FILE_ATTRIBUTE_HIDDEN
: 0) |
92 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_DIRECTORY
) ? FILE_ATTRIBUTE_DIRECTORY
: 0) |
93 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_SYSTEM
) ? FILE_ATTRIBUTE_SYSTEM
: 0) |
94 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_READONLY
) ? FILE_ATTRIBUTE_READONLY
: 0);
98 CdfsIsNameLegalDOS8Dot3(IN UNICODE_STRING FileName
103 CHAR DbcsNameBuffer
[12];
105 /* 8dot3 filename is max 12 length */
106 if (FileName
.Length
/ sizeof(WCHAR
) > 12)
111 ASSERT(FileName
.Length
>= sizeof(WCHAR
));
112 for (i
= 0; i
< FileName
.Length
/ sizeof(WCHAR
) ; i
++)
114 /* Don't allow spaces in FileName */
115 if (FileName
.Buffer
[i
] == L
' ')
119 /* If FileName is finishing with a dot, remove it */
120 if (FileName
.Buffer
[FileName
.Length
/ sizeof(WCHAR
) - 1] == '.')
122 FileName
.Length
-= sizeof(WCHAR
);
125 /* Finally, convert the string to call the FsRtl function */
126 RtlInitEmptyAnsiString(&DbcsName
, DbcsNameBuffer
, sizeof(DbcsNameBuffer
));
127 if (!NT_SUCCESS(RtlUnicodeStringToCountedOemString(&DbcsName
,
134 return FsRtlIsFatDbcsLegal(DbcsName
, FALSE
, FALSE
, FALSE
);
138 CdfsShortNameCacheGet
140 PLARGE_INTEGER StreamOffset
,
141 PUNICODE_STRING LongName
,
142 PUNICODE_STRING ShortName
)
145 PCDFS_SHORT_NAME ShortNameEntry
;
146 GENERATE_NAME_CONTEXT Context
= { 0 };
148 DPRINT("CdfsShortNameCacheGet(%I64d,%wZ)\n", StreamOffset
->QuadPart
, LongName
);
150 /* Get the name list resource */
151 ExAcquireResourceExclusiveLite(&DirectoryFcb
->NameListResource
, TRUE
);
153 /* Try to find the name in our cache */
154 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
155 Entry
!= &DirectoryFcb
->ShortNameList
;
156 Entry
= Entry
->Flink
)
158 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
159 if (ShortNameEntry
->StreamOffset
.QuadPart
== StreamOffset
->QuadPart
)
162 RtlCopyUnicodeString(ShortName
, &ShortNameEntry
->Name
);
163 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
164 DPRINT("Yield short name %wZ from cache\n", ShortName
);
170 if (!CdfsIsNameLegalDOS8Dot3(*LongName
))
172 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
176 /* copy short name */
177 RtlUpcaseUnicodeString
183 DPRINT("Initial Guess %wZ\n", ShortName
);
185 /* Make it unique by scanning the cache and bumping */
186 /* Note that incrementing the ambiguous name is enough, since we add new
187 * entries at the tail. We'll scan over all collisions. */
188 /* XXX could perform better. */
189 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
190 Entry
!= &DirectoryFcb
->ShortNameList
;
191 Entry
= Entry
->Flink
)
193 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
194 if (RtlCompareUnicodeString
196 &ShortNameEntry
->Name
,
197 TRUE
) == 0) /* Match */
199 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
200 DPRINT("Collide; try %wZ\n", ShortName
);
204 /* We've scanned over all entries and now have a unique one. Cache it. */
205 ShortNameEntry
= ExAllocatePoolWithTag(PagedPool
,
206 sizeof(CDFS_SHORT_NAME
),
207 CDFS_SHORT_NAME_TAG
);
210 /* We couldn't cache it, but we can return it. We run the risk of
211 * generating a non-unique name later. */
212 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
213 DPRINT1("Couldn't cache potentially clashing 8.3 name %wZ\n", ShortName
);
217 ShortNameEntry
->StreamOffset
= *StreamOffset
;
218 RtlInitEmptyUnicodeString(&ShortNameEntry
->Name
,
219 ShortNameEntry
->NameBuffer
,
220 sizeof(ShortNameEntry
->NameBuffer
));
221 RtlCopyUnicodeString(&ShortNameEntry
->Name
, ShortName
);
222 InsertTailList(&DirectoryFcb
->ShortNameList
, &ShortNameEntry
->Entry
);
223 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
225 DPRINT("Returning short name %wZ for long name %wZ\n", ShortName
, LongName
);