]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/wimfile.c
added Spanish (Latinoamérica) translation (#1865)
[Ventoy.git] / wimboot / wimboot-2.7.3 / src / wimfile.c
1 /*
2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 /**
21 * @file
22 *
23 * WIM virtual files
24 *
25 */
26
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <wchar.h>
32 #include "wimboot.h"
33 #include "vdisk.h"
34 #include "wim.h"
35 #include "wimfile.h"
36
37 /** A WIM virtual file */
38 struct wim_file {
39 /** Underlying virtual file */
40 struct vdisk_file *file;
41 /** WIM header */
42 struct wim_header header;
43 /** Resource */
44 struct wim_resource_header resource;
45 };
46
47 /** Maximum number of WIM virtual files */
48 #define WIM_MAX_FILES 8
49
50 /** WIM virtual files */
51 static struct wim_file wim_files[WIM_MAX_FILES];
52
53 /**
54 * Read from WIM virtual file
55 *
56 * @v file Virtual file
57 * @v data Data buffer
58 * @v offset Offset
59 * @v len Length
60 */
61 static void wim_read_file ( struct vdisk_file *file, void *data,
62 size_t offset, size_t len ) {
63 struct wim_file *wfile = file->opaque;
64 int rc;
65
66 /* Read from resource */
67 if ( ( rc = wim_read ( wfile->file, &wfile->header, &wfile->resource,
68 data, offset, len ) ) != 0 ) {
69 die ( "Could not read from WIM virtual file\n" );
70 }
71 }
72
73 /**
74 * Add WIM virtual file
75 *
76 * @v file Underlying virtual file
77 * @v index Image index, or 0 to use boot image
78 * @v path Path to file within WIM
79 * @v wname New virtual file name
80 * @ret file Virtual file, or NULL if not found
81 */
82 struct vdisk_file * wim_add_file ( struct vdisk_file *file, unsigned int index,
83 const wchar_t *path, const wchar_t *wname ) {
84 static unsigned int wim_file_idx = 0;
85 struct wim_resource_header meta;
86 struct wim_file *wfile;
87 char name[ VDISK_NAME_LEN + 1 /* NUL */ ];
88 unsigned int i;
89 int rc;
90
91 /* Sanity check */
92 if ( wim_file_idx >= WIM_MAX_FILES )
93 die ( "Too many WIM files\n" );
94 wfile = &wim_files[wim_file_idx];
95
96 /* Construct ASCII file name */
97 snprintf ( name, sizeof ( name ), "%ls", wname );
98
99 /* Skip files already added explicitly */
100 for ( i = 0 ; i < VDISK_MAX_FILES ; i++ ) {
101 if ( strcasecmp ( name, vdisk_files[i].name ) == 0 )
102 return NULL;
103 }
104
105 /* Get WIM header */
106 if ( ( rc = wim_header ( file, &wfile->header ) ) != 0 )
107 return NULL;
108
109 /* Get image metadata */
110 if ( ( rc = wim_metadata ( file, &wfile->header, index, &meta ) ) != 0 )
111 return NULL;
112
113 /* Get file resource */
114 if ( ( rc = wim_file ( file, &wfile->header, &meta, path,
115 &wfile->resource ) ) != 0 )
116 return NULL;
117
118 /* Add virtual file */
119 wim_file_idx++;
120 wfile->file = file;
121 return vdisk_add_file ( name, wfile, wfile->resource.len,
122 wim_read_file );
123 }
124
125 /**
126 * Add WIM virtual files
127 *
128 * @v file Underlying virtual file
129 * @v index Image index, or 0 to use boot image
130 * @v paths List of paths to files within WIM
131 */
132 void wim_add_files ( struct vdisk_file *file, unsigned int index,
133 const wchar_t **paths ) {
134 const wchar_t **path;
135 const wchar_t *wname;
136 const wchar_t *tmp;
137
138 /* Add any existent files within the list */
139 for ( path = paths ; *path ; path++ ) {
140
141 /* Construct file name */
142 wname = *path;
143 for ( tmp = wname ; *tmp ; tmp++ ) {
144 if ( *tmp == L'\\' )
145 wname = ( tmp + 1 );
146 }
147
148 /* Add virtual file, if existent */
149 wim_add_file ( file, index, *path, wname );
150 }
151 }