Solved

Find first sector of file

Posted on 1997-04-29
6
498 Views
Last Modified: 2008-03-10
How can i find the first sector or cluster of a file? I need a C-routine or a WIN-API (if one exists) which works on all plattforms (Win95, NT) and with all types of HDs)
Thanks for help!
0
Comment
Question by:hugo
6 Comments
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Have a look at the WIn32 SDK sample program FLOPPY (which is on MSDN and (I think) in VC samples directory, or can probably be downloaded from Microsoft.

FLOPPY demonstrates how to access a physical floppy disk using the Microsoft(R) Win32(R) application programming interface (API). It provides an example of how to open a physical disk, read a disk's geometry, perform a low-level format operation, and read and write physical sectors. FLOPPY is very simple; it performs minimal error checking.

I assume you could modify it to access a hard disk and determine where a file lives - anyway, it's worth a look.

If this helps you, let me know and I'll turn my comment into an answer so you can suitably reward me :-)

0
 
LVL 4

Accepted Solution

by:
emmons earned 150 total points
Comment Utility
[Aplogies for the lost formatting. This may be a little difficult to read. I got the examples from the MSDN disk. There are two functions here to check out]

There is a Win32 SDK function that retrieves the first cluster of the specified file or directory. This function is only supported by Windows 95. It will fail if called under MS-DOS. To call this function, the caller must hold a level 3 lock on the drive.

Interrupt 21h Function 440Dh Minor Code 71h
mov ax, 440Dh            ; generic IOCTL
mov bx, CharSet          ; see below
mov ch, 08h              ; device category
mov cl, 71h              ; Get First Cluster
mov dx, seg PathName     ; see below
mov ds, dx
mov dx, offset PathName  
int 21h

jc error

Parameters
CharSet
Character set of PathName. This parameter must be one of these values:
BCS_WANSI (0)      Windows ANSI character set
BCS_OEM (1)      Current OEM character set
BCS_UNICODE (2)      Unicode character set
 
PathName
Address of a null-terminated string containing the path of the file or directory to retrieve the first cluster for.
 
Return Value
Clears the carry flag and sets DX:AX to the first cluster number if successful. Otherwise, the function sets the carry flag and returns either the ERROR_INVALID_FUNCTION or ERROR_ACCESS_DENIED value in AX.
Remarks
The first cluster of a file is the first cluster of the FAT cluster chain describing the data associated with the file. The first cluster of a directory is the first cluster of the FAT cluster chain associated with the directory. It is the cluster that contains the "." and ".." entries. The function finds any file or directory regardless of attribute (system, hidden, or read-only). It does not find volume labels.
If your application is unable to accommodate a 32-bit cluster number, you must check to see if the value returned in the DX register is greater than zero.
if(MAKELONG(regAX,regDX) > 0x0000FFF8)
    b32BitNum = TRUE;
else
    b32BitNum = FALSE;
 
It is the calling application's responsibility to check to see if the returned cluster number is valid.
if((MAKELONG(regAX,regDX) < 2L) || (MAKELONG(regAX,regDX) > maxClus))
    bInvalidNum = TRUE;
else
    bInvalidNum = FALSE;
 
In the preceding example, the maxClus variable is the maximum legal cluster number, as a DWORD type, computed from the drive parameters.
 





Alternatively, you might check out FS_SearchFile, which has a flag( ir_firstclus       ) that returns the first cluster automagically :

FS_SearchFile(
 PIOREQ pir
 )

 
All DOS find first and find next operations are performed through FS_SearchFile. This routine handles the INT 21h functions: 11h, 12h, 4Eh, and 4Fh.
ir_ppath
Supplies a pointer to the canonicalized unicode pathname to search for. This is passed in only for a SEARCH_FIRST operation and is set to NULL on find next requests. Wildcards are allowed on the pathname.
ir_rh
Supplies handle to disk volume or network resource where the pathname is to be searched for.
ir_flags
Supplies the operation to perform.

Search Operation Values:       
 
Value       Meaning

SEARCH_FIRST       Do a FindFirst operation on the path passed in.
SEARCH_NEXT       Do a FindNext operation on the path passed in on the SEARCH_FIRST operation.
ir_attr
Supplies wildcard search attributes. This is supplied only for SEARCH_FIRST requests.

Search Attribute Values: Any combination of the following attributes may be specified, causing those files to be included with normal files in the search.       
 
Value       Meaning

FILE_ATTRIBUTE_HIDDEN       Include hidden file in search.
FILE_ATTRIBUTE_SYSTEM       Include system file in search.
FILE_ATTRIBUTE_DIRECTORY       Include subdirectories in search. The following attribute will only be specified by itself. It will not be combined with any of the other attributes.
FILE_ATTRIBUTE_VOLUMELABEL       Search for volume label only.
ir_uFname
Pointer to the case-preserved filename component in unicode.
ir_upath
Pointer to the unparsed user pathname in unicode.
ir_data
Supplies a pointer to the search info structure that is described below.
ir_user
Supplies user id for this request.
ir_pid
Supplies process id for this request.

7      ir_error       Returns status of the operation ( 0 if no error, errorcode otherwise ).
ir_data       Returns the information about the find in the search info structure. The contents of the supplied structure is the same as the info returned on a path based Int 21h search function. On a successful return the search info structure returns information required for a find next request that is filled in by the FSD. This information is stored in the users address space and is passed back on find next requests.
Search Info Structure:       
 
Value       Meaning

se_key       Search key structure which contains information pertinent to the search operation that is in progress.
Search Key Structure:       
 
Value       Meaning

sk_drive       Drive value. This field is set by the IFS manager and will be overwritten. The FSD should not change this field.
sk_pattern[11]       Returns filename pattern being searched for. This field is set and used by the FSD to continue the search operation. The filename must be stored in the FCB 8.3 format.
sk_attr       Search attributes. This field is set and used by the FSD to continue searches.
sk_localFSD[4]       Returns local FSD continuation fields. These bytes should be used to save the information required to continue the search operation. The typical values saved are those used to locate the directory and the last entry with in the directory.
sk_netFSD[2]       Returns remote FSD continuation fields. These bytes should be used to save the information required to continue the search operation by network FSDs.
sk_ifsmgr[2]       Reserved for use by the IFS manager.
se_attrib       Returns attributes of matched file.
Return Attribute Values: Any combination of the following attributes may be returned.       
 
Value       Meaning

FILE_ATTRIBUTE_READONLY       Read-Only file.
FILE_ATTRIBUTE_HIDDEN       Hidden file.
FILE_ATTRIBUTE_SYSTEM       System file.
FILE_ATTRIBUTE_DIRECTORY       Subdirectory.
FILE_ATTRIBUTE_ARCHIVE       Archive file. This attribute cannot be returned in combination with other attributes.
FILE_ATTRIBUTE_VOLUMELABEL       Volume label.
se_time       Returns DOS file time of matched file. This field is set by the FSD.
se_date       Returns DOS file date of matched file. This field is set by the FSD.
se_size       Returns size of matched file. This field is set by the FSD.
se_name[13]       Returns filename of matched file. This field is set by the FSD to the matching filename found by the search. The filename must be stored as an 8.3 format ASCIIZ string.
ir_firstclus       Returns first cluster of the file. This field is needed only for FCB-based search operations and is updated separately by the IFS. This field is valid only for FAT-based filesystems. A zero value should be returned by non-FAT-based FSDs.

0
 
LVL 7

Expert Comment

by:linda101698
Comment Utility
Comment emailed to Experts Exchange:
Hi emmons!

My question was how to find the first sector of a file.
Your first part of your answer was too low level for me, because i don't
know how i could integrate that assembler stuff in a hole C-programm. Is
his interrupt function also valid under NT?
Your second part was the direct routine FS_SearchFile(). I found nothing
about
that routine and my compiler (Borland 4.5) also.

Please could you give me another little help (short running C-sample for
that interupt)?

Thanks a lot, yours Ralf
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 4

Expert Comment

by:emmons
Comment Utility
hugo == ralf ??
The assembly code will only work for 32 bits (NT and 95).
As for putting assembly into your C code, Borland supports the "asm" command, so you can insert the assembly as a block, surrounded by "asm {" and "}".

As for the second, I didn't realize that you are using Borland, so the File IO interface library is not available to you with the native kit. It is a Microsoft thing. Sorry about that.
0
 

Author Comment

by:hugo
Comment Utility
Hi emmons!
Sorry, i didn't understand it yet.
What means 'the caller must hold a level 3 lock on the drive'?
What means the jc error in the assembler listing? (My compiler says 'unknown symbol')
How i have to fill the registers in c-language?

Please give me another little help!
Thanks Hugo
0
 
LVL 4

Expert Comment

by:emmons
Comment Utility
the
 jc error
line assumes that you have written a routine to handle error conditions. That is not included here, since I have no idea what you will want to do if there is an error condition.

The level 3 lock prevents all processes except the lock owner from reading or writing to the disk. Read operations are blocked, and write operations and new file mappings are either blocked or failed depending on the permissions set in the level 1 lock. This is the most restrictive lock, not only because it prevents all other processes from accessing the disk but also because the lock owner is limited in what it can do. You need to have this level of locking to assure that nothing will move during the course of your routine. To obtain a level 3 lock, an application must make three calls to the lock volume function ¾ first to obtain a level 1 lock, then a level 2 lock, and finally a level 3 lock. After obtaining the level 3 lock, the application can safely access the volume directly. To release the lock on a volume, an application must call the appropriate unlock volume function the same number of times that the corresponding lock volume function was called. Each call to the unlock volume function decrements the lock level. To get a level three lock on a drive, use the following interrupt...
; [Windows 95 only.]
; Locks the logical volume.  
; Interrupt 21h Function 440Dh Minor Code 71h
mov ax, 440Dh        ; generic IOCTL
mov bh, LockLevel    ; see below
mov bl, DriveNum     ; see below
mov ch, 08h          ; device category (must be 08h)
mov cl, 4Ah          ; Lock Logical Volume
mov dx, Permissions  ; see below
int 21h
jc error
Parameters
LockLevel
Level of the lock. This parameter must be either 0, 1, 2, or 3.
DriveNum
Drive to lock. This parameter can be 0 for the default drive, 1 for A, 2 for B, and so on.
Permissions
Operations that the system permits while the volume is locked. This parameter is specified only when a level 1 lock is obtained or when a level 0 lock is obtained for the second time for formatting the volume. For other lock levels, this parameter is zero. When a level 1 lock is obtained, bits 0 and 1 of this parameter specify whether the system permits write operations, new file mappings, or both by other processes during a level 1 lock as well as during level 2 and 3 locks. If this parameter specifies that write operations, new file mappings, or both are failed, these operations are failed during level 1, 2, and 3 locks. This parameter has the following form:
Bit      Meaning
0      0 = Write operations are failed (specified when a level 1 lock is obtained).
0      1 = Write operations are allowed (specified when a level 1 lock is obtained).
1      0 = New file mapping are allowed (specified when a level 1 lock is obtained).
1      1 = New file mapping are failed (specified when a level 1 lock is obtained).
2      1 = The volume is locked for formatting (specified when a level 0 lock is obtained for the second time).
 
Return Value
Clears the carry flag if successful. Otherwise, the function sets the carry flag and sets the AX register to an error value.


As to the last question, I would not spend the time filling the registers in C, create a "_asm" block in your C code and then use the above functions in that block.

On the whole, I am not sure that this is a task that you should be undertaking. You will be working at a very low level on the drive, and mistakes could be rather costly in terms of trashing your drive.


0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now