open, ftruncate, mmap, stat

来源:互联网 发布:域名劫持工具 编辑:程序博客网 时间:2024/05/20 23:03
open
system\core\init\property_service.c
android:
fd = open("/dev/__properties__", O_RDWR | O_CREAT, 0600);


mingw:
    fd = open("c:\\__properties__", O_RDWR | O_CREAT, 0600);
if (fd < 0){
fd = creat("c:\\__properties__", O_RDWR | O_CREAT);
if (fd < 0){
printf("ERROR opening.\n\terror is:%s\n", strerror(errno));
return -1;

chmod("c:\\__properties__", 0600);
}

autually mingw can create with open.

c:\MinGW-4.7.1\include\fcntl.h
#define O_CREAT        0x0100  /* create and open file */
d:/linux/linuxkernel/WORKING_DIRECTORY/android-omap-20111108-gingerbread/dalvik/vm/analysis/DexPrepare.c:87:0: warning: "O_CREAT" redefined [enabled by default]
d:/linux/linuxkernel/WORKING_DIRECTORY/android-omap-20111108-gingerbread/bionic/libc/kernel/common/asm-generic/fcntl.h:22:0: note: this is the location of the previous definition
if O_CREAT is not correct(0x0100), open(cacheFileName, O_CREAT|O_RDWR, 0600) might not take effect.



ftruncate
mingw
c:\mingw-4.6.1\include\unistd.h
__CRT_INLINE int ftruncate(int __fd, off_t __length)
{
  return _chsize (__fd, __length);
}




mmap
system\core\init\property_service.c
android:
    data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);


mingw:
nt-base.h:
#if !defined(mmap)
#  define mmap(address,length,protection,access,file,offset) \
  NTMapMemory(address,length,protection,access,file,offset)
#endif
nt-base.c:

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
+  N T M a p M e m o r y                                                      %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Mmap() emulates the Unix method of the same name.
%
%  The format of the NTMapMemory method is:
%
%    MagickExport void *NTMapMemory(char *address,size_t length,int protection,
%      int access,int file,MagickOffsetType offset)
% doesn't support PROT_NONE on windows
*/
//MagickExport void *NTMapMemory(char *address,size_t length,int protection,
 void *NTMapMemory(char *address,size_t length,int protection,
  int flags,int file,MagickOffsetType offset)
{
  DWORD
    access_mode,
    high_length,
    high_offset,
    low_length,
    low_offset,
    protection_mode;


  HANDLE
    file_handle,
    map_handle;


  void
    *map;


  (void) address;
  access_mode=0;
  file_handle=INVALID_HANDLE_VALUE;
  /*dwMaximumSizeLow [in]
   *The low-order DWORD of the maximum size of the file mapping object.
   *If this parameter and dwMaximumSizeHigh are 0 (zero), 
   * the maximum size of the file mapping object is equal to the current size of the file that hFile identifies.
   * As sometimes the length is smaller than file size, which would cause error if the offset is larger than length,
   * we take 0 as default */
//  low_length=(DWORD) (length & 0xFFFFFFFFUL);
  low_length=0;
  //The high-order DWORD of the maximum size of the file mapping object. normally it should be 0, unless 
  //the file size is extremely big.
  //refer to MemoryMappedFile::map in https://raw.github.com/zimbatm/deb-mongodb/master/util/mmap_win.cpp  
//  high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
  high_length=0;
  map_handle=INVALID_HANDLE_VALUE;
  map=(void *) NULL;
  low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
//  high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
  high_offset=0;
  protection_mode=0;
  if (protection & PROT_WRITE)
    {
      access_mode=FILE_MAP_WRITE;
      if (!(flags & MAP_PRIVATE))
        protection_mode=PAGE_READWRITE;
      else
        {
          access_mode=FILE_MAP_COPY;
          protection_mode=PAGE_WRITECOPY;
        }
    }
  else
    if (protection & PROT_READ)
      {
        access_mode=FILE_MAP_READ;
        protection_mode=PAGE_READONLY;
      }
  if ((file == -1) && (flags & MAP_ANONYMOUS)){
    file_handle=INVALID_HANDLE_VALUE;
/*If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size 
* for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. 
* In this scenario, CreateFileMapping creates a file mapping object of a specified size 
* that is backed by the system paging file instead of by a file in the file system.*/
low_length=(DWORD) (length & 0xFFFFFFFFUL);
  }else
    file_handle=(HANDLE) _get_osfhandle(file);
  map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
    low_length,0);
  if (map_handle)
    {
      map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
        length);
      CloseHandle(map_handle);
    }
  if (map == (void *) NULL){
char errormsg[500];
sprintf(errormsg, "ERROR CreateFileMapping.\n\terror is:%s\n", strerror(errno));
printf("ERROR CreateFileMapping.\n\terror is:%s\n", strerror(errno));
    return((void *) MAP_FAILED);
  }
  return((void *) ((char *) map));
} //void *NTMapMemory(char *address,size_t length,int protection,


stat:

make sure the program is using the definition of struct stat in c:\mingw-4.7.1\include\sys\stat.h. u can try to rename stat.h to statm.h, and include it.

原创粉丝点击