file.h 4.32 KB
Newer Older
okuji's avatar
okuji committed
1
/*
2
 *  GRUB  --  GRand Unified Bootloader
3
 *  Copyright (C) 2002,2007  Free Software Foundation, Inc.
okuji's avatar
okuji committed
4
 *
5
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 3 of the License, or
okuji's avatar
okuji committed
8 9
 *  (at your option) any later version.
 *
10
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
11 12 13 14 15
 *  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
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
17 18
 */

19 20
#ifndef GRUB_FILE_HEADER
#define GRUB_FILE_HEADER	1
okuji's avatar
okuji committed
21

22 23 24 25
#include <grub/types.h>
#include <grub/err.h>
#include <grub/device.h>
#include <grub/fs.h>
26
#include <grub/disk.h>
okuji's avatar
okuji committed
27 28

/* File description.  */
29
struct grub_file
okuji's avatar
okuji committed
30
{
31 32 33
  /* File name.  */
  char *name;

okuji's avatar
okuji committed
34
  /* The underlying device.  */
35
  grub_device_t device;
okuji's avatar
okuji committed
36 37

  /* The underlying filesystem.  */
38
  grub_fs_t fs;
okuji's avatar
okuji committed
39 40

  /* The current offset.  */
41
  grub_off_t offset;
42 43 44 45 46 47
  grub_off_t progress_offset;

  /* Progress info. */
  grub_uint64_t last_progress_time;
  grub_off_t last_progress_offset;
  grub_uint64_t estimated_speed;
okuji's avatar
okuji committed
48 49

  /* The file size.  */
50
  grub_off_t size;
okuji's avatar
okuji committed
51

52 53
  /* If file is not easily seekable. Should be set by underlying layer.  */
  int not_easily_seekable;
54

okuji's avatar
okuji committed
55 56 57 58
  /* Filesystem-specific data.  */
  void *data;

  /* This is called when a sector is read. Used only for a disk device.  */
59 60 61 62
  grub_disk_read_hook_t read_hook;

  /* Caller-specific data passed to the read hook.  */
  void *read_hook_data;
okuji's avatar
okuji committed
63
};
64
typedef struct grub_file *grub_file_t;
okuji's avatar
okuji committed
65

66 67
extern grub_disk_read_hook_t EXPORT_VAR(grub_file_progress_hook);

68 69 70
/* Filters with lower ID are executed first.  */
typedef enum grub_file_filter_id
  {
71
    GRUB_FILE_FILTER_PUBKEY,
72
    GRUB_FILE_FILTER_GZIO,
73
    GRUB_FILE_FILTER_XZIO,
74
    GRUB_FILE_FILTER_LZOPIO,
75 76
    GRUB_FILE_FILTER_MAX,
    GRUB_FILE_FILTER_COMPRESSION_FIRST = GRUB_FILE_FILTER_GZIO,
77
    GRUB_FILE_FILTER_COMPRESSION_LAST = GRUB_FILE_FILTER_LZOPIO,
78 79
  } grub_file_filter_id_t;

80
typedef grub_file_t (*grub_file_filter_t) (grub_file_t in, const char *filename);
81 82 83 84 85 86 87 88 89

extern grub_file_filter_t EXPORT_VAR(grub_file_filters_all)[GRUB_FILE_FILTER_MAX];
extern grub_file_filter_t EXPORT_VAR(grub_file_filters_enabled)[GRUB_FILE_FILTER_MAX];

static inline void
grub_file_filter_register (grub_file_filter_id_t id, grub_file_filter_t filter)
{
  grub_file_filters_all[id] = filter;
  grub_file_filters_enabled[id] = filter;
90
}
91 92 93 94 95 96

static inline void
grub_file_filter_unregister (grub_file_filter_id_t id)
{
  grub_file_filters_all[id] = 0;
  grub_file_filters_enabled[id] = 0;
97
}
98 99 100 101 102

static inline void
grub_file_filter_disable (grub_file_filter_id_t id)
{
  grub_file_filters_enabled[id] = 0;
103
}
104 105 106 107 108 109 110 111 112

static inline void
grub_file_filter_disable_compression (void)
{
  grub_file_filter_id_t id;

  for (id = GRUB_FILE_FILTER_COMPRESSION_FIRST;
       id <= GRUB_FILE_FILTER_COMPRESSION_LAST; id++)
    grub_file_filters_enabled[id] = 0;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

static inline void
grub_file_filter_disable_all (void)
{
  grub_file_filter_id_t id;

  for (id = 0;
       id < GRUB_FILE_FILTER_MAX; id++)
    grub_file_filters_enabled[id] = 0;
}

static inline void
grub_file_filter_disable_pubkey (void)
{
  grub_file_filters_enabled[GRUB_FILE_FILTER_PUBKEY] = 0;
}
130

okuji's avatar
okuji committed
131
/* Get a device name from NAME.  */
132
char *EXPORT_FUNC(grub_file_get_device_name) (const char *name);
okuji's avatar
okuji committed
133

134
grub_file_t EXPORT_FUNC(grub_file_open) (const char *name);
135
grub_ssize_t EXPORT_FUNC(grub_file_read) (grub_file_t file, void *buf,
136 137
					  grub_size_t len);
grub_off_t EXPORT_FUNC(grub_file_seek) (grub_file_t file, grub_off_t offset);
138
grub_err_t EXPORT_FUNC(grub_file_close) (grub_file_t file);
okuji's avatar
okuji committed
139

140 141 142
/* Return value of grub_file_size() in case file size is unknown. */
#define GRUB_FILE_SIZE_UNKNOWN	 0xffffffffffffffffULL

143
static inline grub_off_t
144
grub_file_size (const grub_file_t file)
okuji's avatar
okuji committed
145 146 147 148
{
  return file->size;
}

149
static inline grub_off_t
150
grub_file_tell (const grub_file_t file)
okuji's avatar
okuji committed
151 152 153 154
{
  return file->offset;
}

155 156 157
static inline int
grub_file_seekable (const grub_file_t file)
{
158
  return !file->not_easily_seekable;
159 160
}

161 162 163 164 165 166
grub_file_t
grub_file_offset_open (grub_file_t parent, grub_off_t start,
		       grub_off_t size);
void
grub_file_offset_close (grub_file_t file);

167
#endif /* ! GRUB_FILE_HEADER */