File: map.c

package info (click to toggle)
xdelta 0.14-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 684 kB
  • ctags: 682
  • sloc: ansic: 5,668; sh: 1,740; makefile: 62
file content (110 lines) | stat: -rw-r--r-- 2,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* -*-Mode: C;-*-
 * XDELTA - RCS replacement and delta generator
 * Copyright (C) 1997  Josh MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: map.c 1.7 Sun, 12 Oct 1997 20:29:34 -0700 jmacd $
 */

#include "xdelta.h"

#ifndef _WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#endif

#ifdef BUFSIZE
#undef BUFSIZE
#endif

#define BUFSIZE (1<<13)

MappedFile*
map_file (const gchar *filename)
{
  MappedFile *map;
#ifdef _WIN32
  FILE* it = fopen (filename, FOPEN_READ_ARG);
  int n_read = 0, t_read = 0;
  char* n_seg = g_new (char, BUFSIZ);
  int n_seg_len = BUFSIZ;

  if (!it)
    return NULL;

  for (;;)
    {
      /* Windows seems to adjust some read buffer size at
       * the first read so that if you ask for a larger amount
       * later you don't get it.  now by my reading of a UNIX
       * man page, that's wrong.  As a result, I read a fixed
       * size even though I've expanded the buffer to be much
       * larger. */
      n_read = fread (n_seg + t_read, 1, BUFSIZ, it);

      if (n_read < 0)
	return NULL;

      t_read += n_read;

      if (n_read < BUFSIZ)
	break;

      if (t_read == n_seg_len)
	{
	  n_seg_len *= 2;
	  n_seg = g_realloc (n_seg, n_seg_len);
	}
    }

  map = g_new (MappedFile, 1);

  map->seg = n_seg;
  map->len = t_read;

  return map;
#else
  struct stat sbuf;
  int fd;

  if (stat (filename, &sbuf))
    {
      perror ("stat");
      return FALSE;
    }

  if ((fd = open (filename, O_RDONLY)) < 0)
    {
      perror ("open");
      return FALSE;
    }

  map = g_new (MappedFile, 1);

  map->len = sbuf.st_size;
  map->seg = mmap (0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);

  if (!map->seg)
    {
      g_free (map);
      return NULL;
    }

  return map;
#endif
}