Windows APIs File Processing Copyright © 2016 Curt Hill.

Slides:



Advertisements
Similar presentations
Lectures on File Management
Advertisements

Secure Operating Systems Lesson 3: OS Structures.
Web siteWeb site ExamplesExamples 1 Mode of Operation Protected mode  4 GB  32-bit address  Windows, Linux Real-address mode  1 MB space  20-bit address.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
2: OS Structures 1 Jerry Breecher OPERATING SYSTEMS STRUCTURES.
WinAPI i System Plików 1. Volume Management 2. Disk Management 3. Directory Management 4. File Management Autor: Tomasz Jurkiewicz.
1 JMH Associates © 2004, All rights reserved Chapter 15 Asynchronous Input/Output.
1 JMH Associates © 2004, All rights reserved Chapter 1 Getting Started with Win32/64.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS8: File System 8.5. Windows File and Directory.
Introduction (Processes and Files)
Win32 Programming Lesson 7: Kernel Objects. Abstract  Many of the concepts we’ll look at today won’t make complete sense until you use them  However,
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
FILE HANDLING IN C++.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS2: Operating System Principles 2.4. The Windows.
Win32 Programming Lesson 17: Memory Mapped Files (Finally, cool stuff again, all this work is getting tedious!)
File Systems cs550 Operating Systems David Monismith.
System Programming Course introduction Getting Started …
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Copyright © Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Scala File I/O. Types of files There are two kinds of files: Text files, and binary files Of course, it’s not that simple… Text files Text files are “human.
Copyright © 2004 – 2006 – Curt Hill Linked Lists Very Powerful Data Structure.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Window Threads Chapter 7 Windows Thread Management.
CS212: Object Oriented Analysis and Design
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
File Management Mario Tayah and Jim Fawcett
More important details More fun Part 3
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Windows Programming Lecture 09.
Chapter 7 Text Input/Output Objectives
CSE451 I/O Systems and the Full I/O Path Autumn 2002
Windows Concurrency Concepts and APIs
File Management.
Secure Coding Rules for C++ Copyright © Curt Hill
Files I/O, Streams, I/O Redirection, Reading with fscanf
Predefined Dialog Boxes
Simplifying Flow of Control
Lecture 5A File processing Richard Gesick.
Concepts From Alice Switching to Java Copyright © Curt Hill.
Using files Taken from notes by Dr. Neil Moore
Files in Windows API David Halbig Lopez.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
files Dr. Bhargavi Goswami Department of Computer Science
Waiting and Synchronization
Arrays in Java What, why and how Copyright Curt Hill.
CS703 - Advanced Operating Systems
File Input and Output.
Topics Input and Output Streams More Detailed Error Testing
Cmdlets “Command-lets”
Input - Output.
Copyright © Curt Hill Page Management In memory and on disk Copyright © Curt Hill.
Console A presentation by Inti Vincenzo Pizzoni.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Windows APIs Some odds and ends Copyright © 1997 – 2016 Curt Hill.
OPERATING SYSTEMS STRUCTURES
Mario Tayah and Jim Fawcett CSE775 – Distributed Objects Spring 2007
The CreateFile Function
Professor Jodi Neely-Ritz University of Florida
Varying Character Lengths
Presentation transcript:

Windows APIs File Processing Copyright © 2016 Curt Hill

Introduction In previous presentations we have seen some general Windows API items Today we consider file processing Recall two needed functions, of which we will still have need: GetLastError CloseHandle Copyright © 2016 Curt Hill

MicroSoft Files In DOS 1 MicroSoft used a control block approach The file was represented in memory by a struct This control block had different things at different times File names Disk file pointers This struct was in user memory Copyright © 2016 Curt Hill

Control Block Issues Since the control block is owned by the user it is subject to mishandling The user program may change it while the file is open accidentally or intentionally to achieve some unintended purpose This puts an undue error checking burden on the OS Or causes aborts Copyright © 2016 Curt Hill

Next In about DOS 2 MicroSoft introduces a different set of APIs These were handle based The control block is now in system memory and no longer accessible to the user The handle is a long void * pointer Whether it is a subscript or pointer is now irrelevant Range checking become easier Very difficult for user to mess with Copyright © 2016 Curt Hill

Handles The only thing we will be holding on to is a handle We will obtain this handle with CreateFile This will also open it and set direction We will do input with ReadFile and output with WriteFile CloseHandle will close the file Copyright © 2016 Curt Hill

Generalization One of the goals of an OS is to make the file interface common over many different kind of devices CreateFile does this, so it can access: Files Pipes Directories Volumes Console Network Thus it can get complicated Copyright © 2016 Curt Hill

Signature HANDLE WINAPI CreateFile( _In_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes, _In_opt_ HANDLE hTemplateFile ); Copyright © 2016 Curt Hill

File Specification First parameter is a null terminated string to indicate the file to open Either forward slashes or backslashes may be used May include directory and volume specification The string is usually ASCII Use FileCreateW for UniCode Copyright © 2016 Curt Hill

Access The second parameter is the direction of the file There are two constants: GENERIC_READ GENERIC_WRITE These may be bit stringed Ored If a zero is supplied then the program can neither read nor write, but only examine some of the attributes of the file The attributes must be consistent with the sharing mode Copyright © 2016 Curt Hill

Share Mode Third parameter is how to share the file with other threads and processes This is another bit flag item Flags: 0 – no sharing FILE_SHARE_READ FILE_SHARE_WRITE FILE_SHARE_DELETE Two processes may share a file for reading if they both have FILE_SHARE_READ Copyright © 2016 Curt Hill

Security Attributes The fourth parameter is a pointer We have seen this one in CreateProcess and CreateThread Perhaps it is time to talk about it This is a long pointer to a struct If it is NULL, the security attributes cannot be inherited by a child thread or process Copyright © 2016 Curt Hill

Security Attributes typedef struct _SECURITY_ATTRIBUTES { DWORD nLength; // Size LPVOID lpSecurityDescriptor; BOOL bInheritHandle; }; The security descriptor has its own set of API functions: InitializeSecurityDescriptor GetSecurityDescriptor SetSecurityDescriptor Among others Copyright © 2016 Curt Hill

Creation Flags The fifth parameter is a DWORD of bit flags but they may not be combined These are usually only used in writes They include CREATE_ALWAYS CREATE_NEW OPEN_ALWAYS OPEN_EXISTING TRUNCATE_EXISTING Copyright © 2016 Curt Hill

Creation and Direction Not all combinations of Creation flags and Access flags make sense Open flags and Read flags should pair as do Create or Truncate with Write Some odd combinations do exist Always use GetLastError to determine if things went well Copyright © 2016 Curt Hill

Attributes Each OS may apply file attributes For Windows these include things like: Hidden System Read-only You may specify these with the sixth parameter This only applies when writing Ignored in reading Copyright © 2016 Curt Hill

Attribute Flags Another bit flag value that can OR together several of the following constants FILE_ATTRIBUTE_NORMAL (May only be used alone.) FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE_ARCHIVE FILE_ATTRIBUTE_ENCRYPTED FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE_SYSTEM FILE_ATTRIBUTE_TEMPORARY Not every file system supports all Copyright © 2016 Curt Hill

Other Attribute Flags There are other flags which are not applied to the file on disk but its processing FILE_FLAG_NO_BUFFERING FILE_FLAG_WRITE_THROUGH FILE_FLAG_RANDOM_ACCESS FILE_FLAG_SEQUENTIAL_SCAN Allows cache optimization FILE_FLAG_OVERLAPPED Overlap I/O with file processing There are several others including security attributes Consider buffering and caching Copyright © 2016 Curt Hill

Last Parameter Handle to template file This supplies attributes instead of using the previous parameters This may be NULL Copyright © 2016 Curt Hill

Return Values CreateFile returns a handle in either case Success: a valid handle Failure: INVALID_HANDLE_VALUE Use GetLastError if it fails Copyright © 2016 Curt Hill

I/O The only point of CreateFile is to obtain a file handle This file handle will be used to input or output data This is performed by these two functions ReadFile WriteFile We next consider them Copyright © 2016 Curt Hill

ReadFile Signature BOOL WINAPI ReadFile( _In_ HANDLE hFile, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead, _Inout_opt_ LPOVERLAPPED lpOverlapped ); Copyright © 2016 Curt Hill

ReadFile We pass the handle, a buffer and the number of bytes we want read It returns as the result of success or failure in the function result It changes the buffer We may optionally pass a pointer that gives us the number of bytes read If this is NULL we do not know how many bytes returned In general we want this, the buffer is not null terminated Copyright © 2016 Curt Hill

ReadFile Again There is a lot of processing that is not done by this ReadFile does not care about whitespace, line boundaries, conversion of types or even whether the file is text or binary If we ask for 500 bytes it will give us 500 bytes ReadFile will happily split a line, word or binary data in two to make this happen Only EOF or an error will return fewer Copyright © 2016 Curt Hill

ReadFile Process Often we will try to read in the entire file or a large chunk We will then process lines internally Contrast this approach with using fstreams and getline Copyright © 2016 Curt Hill

Size If CreateFile does not ask for read or write ability all that can be obtained is attributes of the file These are also available with read or write access One of these is: DWORD WINAPI GetFileSize( _In_ HANDLE hFile, _Out_opt_ LPDWORD lpFileSizeHigh ); Copyright © 2016 Curt Hill

Other Attributes You may also obtain other pieces of information from an existing file GetFileType GetFileTime Among others You may also iterate through the files in a directory with FindFirstFile FindNextFile We will not cover these unless some need arises Copyright © 2016 Curt Hill

WriteFile Signature BOOL WINAPI WriteFile( _In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD  lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped ); Copyright © 2016 Curt Hill

Buffers The area to read or write is a void * pointer This could be a character string an array of integers or doubles an array of objects No conversion of any sort is performed Just take the memory and read into it or write from it Copyright © 2016 Curt Hill

Other Functions Windows provides functions to do most of the things that can be done through the command line MoveFile takes two strings: Existing name and a new name Does rename as well CopyFileEx takes two strings and BOOL Boolean is to fail if target exists DeleteFile take a string These return a Boolean for success Copyright © 2016 Curt Hill

Finally When we are done reading or writing a CloseHandle is applied This is when an output file is finalized so should not be missed Copyright © 2016 Curt Hill