ReactOS  0.4.10-dev-358-g66981e1
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

Definition at line 40 of file io.c.

#define NDEBUG

Definition at line 36 of file io.c.

Typedef Documentation

Function Documentation

int fs_changed ( void  )

Definition at line 482 of file io.c.

483 {
484  return ! !changes || did_change;
485 }
static CHANGE * changes
Definition: io.c:49
static int did_change
Definition: io.c:51
int fs_close ( int  write)

Definition at line 462 of file io.c.

Referenced by VfatChkdsk().

463 {
464  CHANGE *next;
465  int changed;
466 
467  changed = ! !changes;
468  if (write)
469  fs_flush();
470  else
471  while (changes) {
472  next = changes->next;
473  free(changes->data);
474  free(changes);
475  changes = next;
476  }
477  if (close(fd) < 0)
478  pdie("closing filesystem");
479  return changed || did_change;
480 }
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:416
#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
static void fs_flush ( void  )
static

Definition at line 416 of file io.c.

Referenced by fs_close().

417 {
418 #ifdef __REACTOS__
419 
420  CHANGE *this;
421  int old_write_immed = (FsCheckFlags & FSCHECK_IMMEDIATE_WRITE);
422 
423  /* Disable writes to the list now */
425 
426  while (changes) {
427  this = changes;
428  changes = changes->next;
429 
430  fs_write(this->pos, this->size, this->data);
431 
432  free(this->data);
433  free(this);
434  }
435 
436  /* Restore values */
437  if (!old_write_immed) FsCheckFlags ^= FSCHECK_IMMEDIATE_WRITE;
438 
439 #else
440  CHANGE *this;
441  int size;
442 
443  while (changes) {
444  this = changes;
445  changes = changes->next;
446  if (lseek(fd, this->pos, 0) != this->pos)
447  fprintf(stderr,
448  "Seek to %lld failed: %s\n Did not write %d bytes.\n",
449  (long long)this->pos, strerror(errno), this->size);
450  else if ((size = write(fd, this->data, this->size)) < 0)
451  fprintf(stderr, "Writing %d bytes at %lld failed: %s\n", this->size,
452  (long long)this->pos, strerror(errno));
453  else if (size != this->size)
454  fprintf(stderr, "Wrote %d bytes instead of %d bytes at %lld."
455  "\n", size, this->size, (long long)this->pos);
456  free(this->data);
457  free(this);
458  }
459 #endif
460 }
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:351
#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
void fs_open ( char path,
int  rw 
)

Definition at line 163 of file io.c.

Referenced by VfatChkdsk().

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 }
#define rw
Definition: rosglue.h:38
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
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
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 289 of file io.c.

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

290 {
291  CHANGE *walk;
292  int got;
293 
294 #ifdef __REACTOS__
295  const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size;
296  const off_t seekpos_aligned = pos - (pos % 512);
297  const size_t seek_delta = (size_t)(pos - seekpos_aligned);
298 #if DBG
299  const size_t readsize = (size_t)(pos - seekpos_aligned) + readsize_aligned;
300 #endif
301  char* tmpBuf = alloc(readsize_aligned);
302  if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",pos);
303  if ((got = read(fd, tmpBuf, readsize_aligned)) < 0) pdie("Read %d bytes at %lld",size,pos);
304  assert(got >= size);
305  got = size;
306  assert(seek_delta + size <= readsize);
307  memcpy(data, tmpBuf+seek_delta, size);
308  free(tmpBuf);
309 #else
310  if (lseek(fd, pos, 0) != pos)
311  pdie("Seek to %lld", (long long)pos);
312  if ((got = read(fd, data, size)) < 0)
313  pdie("Read %d bytes at %lld", size, (long long)pos);
314 #endif
315  if (got != size)
316  die("Got %d bytes instead of %d at %lld", got, size, (long long)pos);
317  for (walk = changes; walk; walk = walk->next) {
318  if (walk->pos < pos + size && walk->pos + walk->size > pos) {
319  if (walk->pos < pos)
320  memcpy(data, (char *)walk->data + pos - walk->pos,
321  min(size, walk->size - pos + walk->pos));
322  else
323  memcpy((char *)data + walk->pos - pos, walk->data,
324  min(walk->size, size + pos - walk->pos));
325  }
326  }
327 }
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
#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
int fs_test ( off_t  pos,
int  size 
)

Definition at line 329 of file io.c.

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

330 {
331  void *scratch;
332  int okay;
333 
334 #ifdef __REACTOS__
335  const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size; // TMN:
336  const off_t seekpos_aligned = pos - (pos % 512); // TMN:
337  scratch = alloc(readsize_aligned);
338  if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",pos);
339  okay = read(fd, scratch, readsize_aligned) == (int)readsize_aligned;
340  free(scratch);
341 #else
342  if (lseek(fd, pos, 0) != pos)
343  pdie("Seek to %lld", (long long)pos);
344  scratch = alloc(size);
345  okay = read(fd, scratch, size) == size;
346  free(scratch);
347 #endif
348  return okay;
349 }
#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:29
void fs_write ( off_t  pos,
int  size,
void data 
)

Definition at line 351 of file io.c.

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().

352 {
353  CHANGE *new;
354  int did;
355 
356 #ifdef __REACTOS__
357  assert(interactive || rw);
358 
360  void *scratch;
361  const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size;
362  const off_t seekpos_aligned = pos - (pos % 512);
363  const size_t seek_delta = (size_t)(pos - seekpos_aligned);
364  BOOLEAN use_read = (seek_delta != 0) || ((readsize_aligned-size) != 0);
365 
366  /* Aloc temp buffer if write is not aligned */
367  if (use_read)
368  scratch = alloc(readsize_aligned);
369  else
370  scratch = data;
371 
372  did_change = 1;
373  if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",seekpos_aligned);
374 
375  if (use_read)
376  {
377  /* Read aligned data */
378  if (read(fd, scratch, readsize_aligned) < 0) pdie("Read %d bytes at %lld",size,pos);
379 
380  /* Patch data in memory */
381  memcpy((char *)scratch + seek_delta, data, size);
382  }
383 
384  /* Write it back */
385  if ((did = write(fd, scratch, readsize_aligned)) == (int)readsize_aligned)
386  {
387  if (use_read) free(scratch);
388  return;
389  }
390  if (did < 0) pdie("Write %d bytes at %lld", size, pos);
391  die("Wrote %d bytes instead of %d at %lld", did, size, pos);
392  }
393 #else
394  if (write_immed) {
395  did_change = 1;
396  if (lseek(fd, pos, 0) != pos)
397  pdie("Seek to %lld", (long long)pos);
398  if ((did = write(fd, data, size)) == size)
399  return;
400  if (did < 0)
401  pdie("Write %d bytes at %lld", size, (long long)pos);
402  die("Wrote %d bytes instead of %d at %lld", did, size, (long long)pos);
403  }
404 #endif
405  new = alloc(sizeof(CHANGE));
406  new->pos = pos;
407  memcpy(new->data = alloc(new->size = size), data, size);
408  new->next = NULL;
409  if (last)
410  last->next = new;
411  else
412  changes = new;
413  last = new;
414 }
#define rw
Definition: rosglue.h:38
static CHANGE * changes
Definition: io.c:49
#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
smooth NULL
Definition: ftsmooth.c:416
__kernel_size_t size_t
Definition: linux.h:237
unsigned char BOOLEAN
GLsizeiptr size
Definition: glext.h:5919
int write_immed
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#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

Variable Documentation

CHANGE* changes
static
int did_change = 0
static

Definition at line 51 of file io.c.

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

int fd
static

Definition at line 51 of file io.c.

Referenced by __rpc_nconf2fd(), _dup(), _fstat64(), LocaleTest::_money_put_X_bug(), _open_osfhandle(), _pipe(), _RunRemoteTest(), _sopen(), _tiffCloseProc(), _tiffisCloseProc(), _tiffisReadProc(), _tiffisSeekProc(), _tiffisSizeProc(), _tiffosCloseProc(), _tiffosSeekProc(), _tiffosSizeProc(), _tiffosWriteProc(), _tiffReadProc(), _tiffSeekProc(), _tiffSizeProc(), _tiffStreamOpen(), _tiffWriteProc(), _tutime(), _wfreopen(), _wfsopen(), _wsopen(), add_protocol(), AddWallpapersFromDirectory(), adns__fdevents(), adns__tcp_tryconnect(), adns_beforeselect(), aexpand(), cff_fd_select_get(), check_for_files(), clnt_dg_call(), clnt_dg_create(), clnt_tli_create(), clnt_vc_create(), codeview_snarf_linetab2(), compressStream(), Control_DoWindow(), count_blocks(), create_io_inherit_block(), do_import(), do_searchW(), DriverEnumProc(), exists_path(), ffileread(), file_get_Size(), flush_output_buffer(), flush_output_resources(), FormatTagEnumProc(), FsVolumeInfo(), FTPGetOneF(), FTPPutOneF(), generate_random(), GetExpandedNameA(), getkeyserv_handle(), getnetid(), CWineTest::GetNextFile(), getpublicandprivatekey(), GetSocketNagleAlgorithm(), gzdopen(), ICreateTypeInfo2_fnAddFuncDesc(), import_certs_from_path(), init_logger_addr(), InitSReadlineInfo(), install_from_unix_file(), InternetCheckConnectionW(), InternetFindNextFileA(), load_licence(), local_rpcb(), LZOpenFileA(), main(), makefd_xprt(), mkstemps(), mpg123_open_fd(), msvcrt_alloc_fd(), msvcrt_init_fp(), NetBTAstatRemote(), NetBTCall(), NetBTInternalFindName(), OleCreateFontIndirect(), open_stream(), open_typelib(), parseAndPrintFile(), poll_isset(), printercache_load_blob(), printercache_save_blob(), pstcache_enumerate(), pstcache_init(), pstcache_load_bitmap(), pstcache_save_bitmap(), pstcache_touch_bitmap(), rd_open_file(), read_importlib(), rpc_broadcast_exp(), rpc_call(), run_ex(), run_open_osfhandle(), rw(), save_licence(), SearchScreenSavers(), setled(), svc_dg_create(), svc_getreq_common(), svc_getreqset(), svc_vc_create(), 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_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(), TIFFSetFileno(), TIFFWarningExt(), tmpfile(), TruncBatchLog(), uncompressStream(), WaitForRemoteInput(), WaitForRemoteOutput(), WaitResponse(), wexpand(), and wpp_default_lookup().

CHANGE * last
static

Definition at line 49 of file io.c.