ReactOS  0.4.13-dev-73-gcfe54aa
io.c File Reference
#include "vfatlib.h"
#include <debug.h>
Include dependency graph for io.c:

Go to the source code of this file.

Classes

struct  _change
 

Macros

#define NDEBUG
 
#define FSCTL_IS_VOLUME_DIRTY   CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 30, METHOD_BUFFERED, FILE_ANY_ACCESS)
 

Typedefs

typedef struct _change CHANGE
 

Functions

void fs_open (char *path, int rw)
 
void fs_read (off_t pos, int size, void *data)
 
int fs_test (off_t pos, int size)
 
void fs_write (off_t pos, int size, void *data)
 
static void fs_flush (void)
 
int fs_close (int write)
 
int fs_changed (void)
 

Variables

static CHANGEchanges
 
static CHANGElast
 
static int fd
 
static int did_change = 0
 

Macro Definition Documentation

◆ FSCTL_IS_VOLUME_DIRTY

Definition at line 40 of file io.c.

◆ NDEBUG

#define NDEBUG

Definition at line 36 of file io.c.

Typedef Documentation

◆ CHANGE

Function Documentation

◆ fs_changed()

int fs_changed ( void  )

Definition at line 475 of file io.c.

476 {
477  return ! !changes || did_change;
478 }
static CHANGE * changes
Definition: io.c:49
static int did_change
Definition: io.c:51

◆ fs_close()

int fs_close ( int  write)

Definition at line 455 of file io.c.

456 {
457  CHANGE *next;
458  int changed;
459 
460  changed = ! !changes;
461  if (write)
462  fs_flush();
463  else
464  while (changes) {
465  next = changes->next;
466  free(changes->data);
467  free(changes);
468  changes = next;
469  }
470  if (close(fd) < 0)
471  pdie("closing filesystem");
472  return changed || did_change;
473 }
static CHANGE * changes
Definition: io.c:49
#define free
Definition: debug_ros.c:5
void * data
Definition: io.c:43
static int fd
Definition: io.c:51
void pdie(const char *msg,...)
Definition: common.c:74
static int did_change
Definition: io.c:51
#define write
Definition: acwin.h:73
static void fs_flush(void)
Definition: io.c:409
#define close
Definition: acwin.h:74
static unsigned __int64 next
Definition: rand_nt.c:6
struct _change * next
Definition: io.c:46
Definition: io.c:42

Referenced by VfatChkdsk().

◆ fs_flush()

static void fs_flush ( void  )
static

Definition at line 409 of file io.c.

410 {
411 #ifdef __REACTOS__
412 
413  CHANGE *this;
414  int old_write_immed = (FsCheckFlags & FSCHECK_IMMEDIATE_WRITE);
415 
416  /* Disable writes to the list now */
418 
419  while (changes) {
420  this = changes;
421  changes = changes->next;
422 
423  fs_write(this->pos, this->size, this->data);
424 
425  free(this->data);
426  free(this);
427  }
428 
429  /* Restore values */
430  if (!old_write_immed) FsCheckFlags ^= FSCHECK_IMMEDIATE_WRITE;
431 
432 #else
433  CHANGE *this;
434  int size;
435 
436  while (changes) {
437  this = changes;
438  changes = changes->next;
439  if (lseek(fd, this->pos, 0) != this->pos)
440  fprintf(stderr,
441  "Seek to %lld failed: %s\n Did not write %d bytes.\n",
442  (long long)this->pos, strerror(errno), this->size);
443  else if ((size = write(fd, this->data, this->size)) < 0)
444  fprintf(stderr, "Writing %d bytes at %lld failed: %s\n", this->size,
445  (long long)this->pos, strerror(errno));
446  else if (size != this->size)
447  fprintf(stderr, "Wrote %d bytes instead of %d bytes at %lld."
448  "\n", size, this->size, (long long)this->pos);
449  free(this->data);
450  free(this);
451  }
452 #endif
453 }
static CHANGE * changes
Definition: io.c:49
#define free
Definition: debug_ros.c:5
static int fd
Definition: io.c:51
_Check_return_opt_ _CRTIMP long __cdecl lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
int errno
const char * strerror(int err)
Definition: compat_str.c:23
void fs_write(off_t pos, int size, void *data)
Definition: io.c:344
#define write
Definition: acwin.h:73
ULONG FsCheckFlags
Definition: vfatlib.c:44
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
GLsizeiptr size
Definition: glext.h:5919
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define FSCHECK_IMMEDIATE_WRITE
Definition: rosglue.h:28
FILE * stderr
struct _change * next
Definition: io.c:46
Definition: io.c:42

Referenced by fs_close().

◆ fs_open()

void fs_open ( char path,
int  rw 
)

Definition at line 163 of file io.c.

164 {
165  if ((fd = open(path, rw ? O_RDWR : O_RDONLY)) < 0) {
166  perror("open");
167  exit(6);
168  }
169  changes = last = NULL;
170  did_change = 0;
171 }
static CHANGE * changes
Definition: io.c:49
#define open
Definition: acwin.h:71
static int fd
Definition: io.c:51
static int did_change
Definition: io.c:51
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
smooth NULL
Definition: ftsmooth.c:416
int rw
static CHANGE * last
Definition: io.c:49
#define O_RDWR
Definition: fcntl.h:36
Definition: services.c:325
void exit(int exitcode)
Definition: _exit.c:33
#define O_RDONLY
Definition: acwin.h:82

Referenced by VfatChkdsk().

◆ fs_read()

void fs_read ( off_t  pos,
int  size,
void data 
)

Read data from the partition, accounting for any pending updates that are queued for writing.

Parameters
[in]posByte offset, relative to the beginning of the partition, at which to read
[in]sizeNumber of bytes to read
[out]dataWhere to put the data read

Definition at line 282 of file io.c.

283 {
284  CHANGE *walk;
285  int got;
286 
287 #ifdef __REACTOS__
288  const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size;
289  const off_t seekpos_aligned = pos - (pos % 512);
290  const size_t seek_delta = (size_t)(pos - seekpos_aligned);
291 #if DBG
292  const size_t readsize = (size_t)(pos - seekpos_aligned) + readsize_aligned;
293 #endif
294  char* tmpBuf = alloc(readsize_aligned);
295  if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",pos);
296  if ((got = read(fd, tmpBuf, readsize_aligned)) < 0) pdie("Read %d bytes at %lld",size,pos);
297  assert(got >= size);
298  got = size;
299  assert(seek_delta + size <= readsize);
300  memcpy(data, tmpBuf+seek_delta, size);
301  free(tmpBuf);
302 #else
303  if (lseek(fd, pos, 0) != pos)
304  pdie("Seek to %lld", (long long)pos);
305  if ((got = read(fd, data, size)) < 0)
306  pdie("Read %d bytes at %lld", size, (long long)pos);
307 #endif
308  if (got != size)
309  die("Got %d bytes instead of %d at %lld", got, size, (long long)pos);
310  for (walk = changes; walk; walk = walk->next) {
311  if (walk->pos < pos + size && walk->pos + walk->size > pos) {
312  if (walk->pos < pos)
313  memcpy(data, (char *)walk->data + pos - walk->pos,
314  min(size, walk->size - pos + walk->pos));
315  else
316  memcpy((char *)data + walk->pos - pos, walk->data,
317  min(walk->size, size + pos - walk->pos));
318  }
319  }
320 }
static CHANGE * changes
Definition: io.c:49
#define free
Definition: debug_ros.c:5
__kernel_off_t off_t
Definition: linux.h:201
void * data
Definition: io.c:43
#define assert(x)
Definition: debug.h:53
static int fd
Definition: io.c:51
_Check_return_opt_ _CRTIMP long __cdecl lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
void pdie(const char *msg,...)
Definition: common.c:74
off_t pos
Definition: io.c:44
__kernel_size_t size_t
Definition: linux.h:237
GLsizeiptr size
Definition: glext.h:5919
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define min(a, b)
Definition: monoChain.cc:55
#define alloc
Definition: rosglue.h:13
int size
Definition: io.c:45
#define die(str)
Definition: mkdosfs.c:347
struct _change * next
Definition: io.c:46
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
Definition: io.c:42

Referenced by add_file(), alloc_rootdir_entry(), check_backup_boot(), find_volume_de(), read_boot(), read_fat(), read_fsinfo(), and write_boot_label().

◆ fs_test()

int fs_test ( off_t  pos,
int  size 
)

Definition at line 322 of file io.c.

323 {
324  void *scratch;
325  int okay;
326 
327 #ifdef __REACTOS__
328  const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size; // TMN:
329  const off_t seekpos_aligned = pos - (pos % 512); // TMN:
330  scratch = alloc(readsize_aligned);
331  if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",pos);
332  okay = read(fd, scratch, readsize_aligned) == (int)readsize_aligned;
333  free(scratch);
334 #else
335  if (lseek(fd, pos, 0) != pos)
336  pdie("Seek to %lld", (long long)pos);
337  scratch = alloc(size);
338  okay = read(fd, scratch, size) == size;
339  free(scratch);
340 #endif
341  return okay;
342 }
#define free
Definition: debug_ros.c:5
__kernel_off_t off_t
Definition: linux.h:201
static int fd
Definition: io.c:51
_Check_return_opt_ _CRTIMP long __cdecl lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
void pdie(const char *msg,...)
Definition: common.c:74
GLsizeiptr size
Definition: glext.h:5919
#define alloc
Definition: rosglue.h:13
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31

Referenced by fix_bad(), read_boot(), and test_file().

◆ fs_write()

void fs_write ( off_t  pos,
int  size,
void data 
)

Definition at line 344 of file io.c.

345 {
346  CHANGE *new;
347  int did;
348 
349 #ifdef __REACTOS__
350  assert(interactive || rw);
351 
353  void *scratch;
354  const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size;
355  const off_t seekpos_aligned = pos - (pos % 512);
356  const size_t seek_delta = (size_t)(pos - seekpos_aligned);
357  BOOLEAN use_read = (seek_delta != 0) || ((readsize_aligned-size) != 0);
358 
359  /* Aloc temp buffer if write is not aligned */
360  if (use_read)
361  scratch = alloc(readsize_aligned);
362  else
363  scratch = data;
364 
365  did_change = 1;
366  if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",seekpos_aligned);
367 
368  if (use_read)
369  {
370  /* Read aligned data */
371  if (read(fd, scratch, readsize_aligned) < 0) pdie("Read %d bytes at %lld",size,pos);
372 
373  /* Patch data in memory */
374  memcpy((char *)scratch + seek_delta, data, size);
375  }
376 
377  /* Write it back */
378  if ((did = write(fd, scratch, readsize_aligned)) == (int)readsize_aligned)
379  {
380  if (use_read) free(scratch);
381  return;
382  }
383  if (did < 0) pdie("Write %d bytes at %lld", size, pos);
384  die("Wrote %d bytes instead of %d at %lld", did, size, pos);
385  }
386 #else
387  if (write_immed) {
388  did_change = 1;
389  if (lseek(fd, pos, 0) != pos)
390  pdie("Seek to %lld", (long long)pos);
391  if ((did = write(fd, data, size)) == size)
392  return;
393  if (did < 0)
394  pdie("Write %d bytes at %lld", size, (long long)pos);
395  die("Wrote %d bytes instead of %d at %lld", did, size, (long long)pos);
396  }
397 #endif
398  new = alloc(sizeof(CHANGE));
399  new->pos = pos;
400  memcpy(new->data = alloc(new->size = size), data, size);
401  new->next = NULL;
402  if (last)
403  last->next = new;
404  else
405  changes = new;
406  last = new;
407 }
static CHANGE * changes
Definition: io.c:49
#define new(TYPE, numElems)
Definition: treelist.c:54
#define free
Definition: debug_ros.c:5
__kernel_off_t off_t
Definition: linux.h:201
#define assert(x)
Definition: debug.h:53
static int fd
Definition: io.c:51
_Check_return_opt_ _CRTIMP long __cdecl lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
#define interactive
Definition: rosglue.h:34
void pdie(const char *msg,...)
Definition: common.c:74
static int did_change
Definition: io.c:51
#define write
Definition: acwin.h:73
ULONG FsCheckFlags
Definition: vfatlib.c:44
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
__kernel_size_t size_t
Definition: linux.h:237
GLsizeiptr size
Definition: glext.h:5919
int write_immed
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
int rw
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static CHANGE * last
Definition: io.c:49
#define alloc
Definition: rosglue.h:13
#define FSCHECK_IMMEDIATE_WRITE
Definition: rosglue.h:28
#define die(str)
Definition: mkdosfs.c:347
struct _change * next
Definition: io.c:46
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
Definition: io.c:42

Referenced by add_file(), alloc_rootdir_entry(), auto_rename(), check_backup_boot(), check_fat_state_bit(), clear_lfn_slots(), fs_flush(), lfn_add_slot(), lfn_fix_checksum(), lfn_get(), lfn_remove(), read_fat(), read_fsinfo(), reclaim_file(), rename_file(), set_fat(), update_free(), write_boot_label(), and write_volume_label().

Variable Documentation

◆ changes

◆ did_change

int did_change = 0
static

Definition at line 51 of file io.c.

Referenced by fs_changed(), fs_close(), fs_open(), and fs_write().

◆ fd

int fd
static

Definition at line 51 of file io.c.

Referenced by __file_size(), __is_regular_file(), __rpc_fd2sockinfo(), __rpc_nconf2fd(), __rpc_sockisbound(), __rpcgettp(), _chsize(), _chsize_s(), _close(), _commit(), _dup(), _eof(), _fcons(), _fdopen(), _filelength(), _filelengthi64(), _findfirst64i32(), _findnext64i32(), _fstat(), _fstat64(), _fstati64(), _futime(), _get_osfhandle(), _isatty(), _locking(), _lseek(), _lseeki64(), LocaleTest::_money_put_X_bug(), _open_osfhandle(), _pipe(), _read(), _RunRemoteTest(), _setmode(), _sopen(), _sopen_s(), _tell(), _telli64(), _tiffCloseProc(), _tiffDummyMapProc(), _tiffDummyUnmapProc(), _tiffisCloseProc(), _tiffisReadProc(), _tiffisSeekProc(), _tiffisSizeProc(), _tiffMapProc(), _tiffosCloseProc(), _tiffosSeekProc(), _tiffosSizeProc(), _tiffosWriteProc(), _TIFFPrettyPrintField(), _TIFFprintAscii(), _TIFFprintAsciiBounded(), _TIFFprintAsciiTag(), _TIFFPrintField(), _TIFFPrintFieldInfo(), _tiffReadProc(), _tiffSeekProc(), _tiffSizeProc(), _tiffStreamOpen(), _tiffUnmapProc(), _tiffWriteProc(), _tutime(), _wfdopen(), _wfreopen(), _wfsopen(), _write(), _wsopen(), _wsopen_s(), add_protocol(), AddWallpapersFromDirectory(), adns__fdevents(), adns__setnonblock(), adns__tcp_tryconnect(), adns_beforeselect(), adns_processexceptional(), adns_processreadable(), adns_processwriteable(), aexpand(), alloc_pioinfo_block(), applySavedFileAttrToOutputFile(), BZ2_bzdopen(), bzopen_or_bzdopen(), calc_crc32(), cff_fd_select_get(), check_for_files(), clnt_dg_create(), clnt_tli_create(), clnt_vc_create(), codeview_snarf_linetab2(), compat_fdopen(), compressStream(), Control_DoWindow(), count_blocks(), create_io_inherit_block(), CRTDLL__fstat(), do_import(), do_searchW(), DriverEnumProc(), epoll_ctl(), exists_path(), fallback_lseek(), fallback_read(), fd_event(), ffileread(), file_get_Size(), FileSize(), flush_output_buffer(), flush_output_resources(), FormatTagEnumProc(), fs_close(), fs_flush(), fs_open(), fs_read(), fs_test(), fs_write(), FsVolumeInfo(), FTPGetOneF(), FTPPutOneF(), ftruncate_growable(), generate_random(), get_ioinfo(), get_ioinfo_alloc(), get_ioinfo_alloc_fd(), get_ioinfo_nolock(), GetExpandedNameA(), getkeyserv_handle(), getnetid(), CWineTest::GetNextFile(), getpublicandprivatekey(), GetSocketLinger(), GetSocketNagleAlgorithm(), gz_open(), gzdopen(), ICreateTypeInfo2_fnAddFuncDesc(), import_certs_from_file(), import_certs_from_path(), init_logger_addr(), InitSReadlineInfo(), install_from_unix_file(), InternetCheckConnectionW(), InternetFindNextFileA(), llseek(), load_licence(), local_rpcb(), LZClose(), LZOpenFileA(), LZRead(), LZSeek(), main(), makefd_xprt(), mkstemps(), mpg123_open_fd(), msft_read_guid(), msvcrt_alloc_fd(), msvcrt_free_fd(), msvcrt_init_fp(), NetBTAstatRemote(), NetBTCall(), NetBTInternalFindName(), NetBTNameWaitLoop(), NetBTSendNameQuery(), NetBTSessionReq(), NetBTWaitForNameResponse(), nonblock(), OleCreateFontIndirect(), open_stream(), open_typelib(), parseAndPrintFile(), poll_isset(), posix_lseek(), posix_read(), PredictorPrintDir(), printercache_load_blob(), printercache_save_blob(), pstcache_enumerate(), pstcache_init(), pstcache_load_bitmap(), pstcache_save_bitmap(), pstcache_touch_bitmap(), rd_close_file(), rd_lock_file(), rd_lseek_file(), rd_open_file(), rd_read_file(), rd_write_file(), rdpdr_abort_io(), read_header(), read_i(), read_importlib(), read_msft_importlib(), read_vc(), readblock(), rpc_broadcast_exp(), rpc_call(), run_ex(), run_open_osfhandle(), save_licence(), SearchScreenSavers(), SelectSetAdd(), SelectSetRemove(), setled(), SetSocketLinger(), SetSocketNagleAlgorithm(), sock_recv(), sock_send(), subprocess(), svc_dg_create(), svc_fd_create(), svc_getreq_common(), svc_getreqset(), svc_tli_create(), svc_vc_create(), switch_to_acf(), test__creat(), test__open_osfhandle(), test_AddRefHfont(), test_bitmap_font_metrics(), test_chsize(), test_FDICopy(), test_FDIIsCabinet(), test_fdopen(), test_file_inherit(), test_file_inherit_child(), test_file_inherit_child_no(), test_fileops(), Test_FindFirstFileA(), Test_FindFirstFileW(), test_get_osfhandle(), test_height(), test_height_selection_vdmx(), test_ifont_size(), test_IsEqual(), test_MoveFileA(), test_overwrite(), test_persistent_state(), test_pipes_child(), test_readmode(), test_ReleaseHfont(), test_setmode(), test_stat(), test_stdin(), test_VarFormat(), test_write_flush_size(), TIFFClose(), TIFFErrorExt(), TIFFFdOpen(), TIFFOpen(), TIFFOpenW(), TIFFPrintDirectory(), TIFFSetFileno(), TIFFWarningExt(), tlb_lseek(), tlb_read(), tmpfile(), TRIO_ARGS3(), TRIO_VARGS3(), TruncBatchLog(), udf_lseek64(), uncompressStream(), unintr_read(), unintr_write(), valid_offset(), WaitForRemoteInput(), WaitForRemoteOutput(), WaitResponse(), wexpand(), wpp_default_lookup(), WriteTable(), xmlCtxtReadFd(), xmlReadFd(), and xsltSaveResultToFd().

◆ last

CHANGE * last
static

Definition at line 49 of file io.c.

Referenced by fs_open(), and fs_write().