source: rtems/cpukit/libfs/src/imfs/imfs_load_tar.c @ ae55da72

4.115
Last change on this file since ae55da72 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2010.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 */
9
10#if HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14/*
15 * This file implements the "mount" procedure for tar-based IMFS
16 * extensions.  The TAR is not actually mounted under the IMFS.
17 * Directories from the TAR file are created as usual in the IMFS.
18 * File entries are created as IMFS_LINEAR_FILE nodes with their nods
19 * pointing to addresses in the TAR image.
20 */
21
22#include "imfs.h"
23
24#include <sys/stat.h>
25#include <string.h>
26#include <tar.h>
27
28#include <rtems/untar.h>
29
30/*
31 * TAR file format:
32 *
33 *   Offset   Length   Contents
34 *     0    100 bytes  File name ('\0' terminated, 99 maxmum length)
35 *   100      8 bytes  File mode (in octal ascii)
36 *   108      8 bytes  User ID (in octal ascii)
37 *   116      8 bytes  Group ID (in octal ascii)
38 *   124     12 bytes  File size (s) (in octal ascii)
39 *   136     12 bytes  Modify time (in octal ascii)
40 *   148      8 bytes  Header checksum (in octal ascii)
41 *   156      1 bytes  Link flag
42 *   157    100 bytes  Linkname ('\0' terminated, 99 maxmum length)
43 *   257      8 bytes  Magic PAX ("ustar\0" + 2 bytes padding)
44 *   257      8 bytes  Magic GNU tar ("ustar  \0")
45 *   265     32 bytes  User name ('\0' terminated, 31 maxmum length)
46 *   297     32 bytes  Group name ('\0' terminated, 31 maxmum length)
47 *   329      8 bytes  Major device ID (in octal ascii)
48 *   337      8 bytes  Minor device ID (in octal ascii)
49 *   345    167 bytes  Padding
50 *   512   (s+p)bytes  File contents (s+p) := (((s) + 511) & ~511),
51 *                     round up to 512 bytes
52 *
53 *   Checksum:
54 *   int i, sum;
55 *   char* header = tar_header_pointer;
56 *   sum = 0;
57 *   for(i = 0; i < 512; i++)
58 *       sum += 0xFF & header[i];
59 */
60
61#define MAX_NAME_FIELD_SIZE      99
62
63#define MIN(a,b)   ((a)>(b)?(b):(a))
64
65/*
66 * rtems_tarfs_load
67 *
68 * Here we create the mountpoint directory and load the tarfs at
69 * that node.  Once the IMFS has been mounted, we work through the
70 * tar image and perform as follows:
71 *  - For directories, simply call mkdir().  The IMFS creates nodes as
72 *    needed.
73 *  - For files, we make our own calls to IMFS eval_for_make and
74 *    create_node.
75 */
76int rtems_tarfs_load(
77  const char *mountpoint,
78  uint8_t *tar_image,
79  size_t tar_size
80)
81{
82   const char                       *hdr_ptr;
83   char                             filename[100];
84   char                             full_filename[256];
85   int                              hdr_chksum;
86   unsigned char                    linkflag;
87   unsigned long                    file_size;
88   unsigned long                    file_mode;
89   int                              offset;
90   unsigned long                    nblocks;
91   IMFS_jnode_t                    *node;
92   int rv = 0;
93   int eval_flags = RTEMS_FS_FOLLOW_LINK;
94   rtems_filesystem_eval_path_context_t ctx;
95   rtems_filesystem_location_info_t rootloc;
96   rtems_filesystem_location_info_t *currentloc =
97     rtems_filesystem_eval_path_start( &ctx, mountpoint, eval_flags );
98
99   rtems_filesystem_eval_path_extract_currentloc( &ctx, &rootloc );
100   rtems_filesystem_eval_path_set_flags(
101     &ctx,
102     RTEMS_FS_MAKE | RTEMS_FS_EXCLUSIVE
103   );
104   if (rootloc.ops != &IMFS_ops && rootloc.ops != &fifoIMFS_ops) {
105     rv = -1;
106   }
107
108   /*
109    * Create an IMFS node structure pointing to tar image memory.
110    */
111   offset = 0;
112   while ( rv == 0 ) {
113    if (offset + 512 > tar_size)
114      break;
115
116    /*
117     * Read a header.
118     */
119    hdr_ptr = (char *) &tar_image[offset];
120    offset += 512;
121    if (strncmp(&hdr_ptr[257], "ustar", 5))
122      break;
123
124    strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE);
125    filename[MAX_NAME_FIELD_SIZE] = '\0';
126
127    linkflag   = hdr_ptr[156];
128    file_mode  = _rtems_octal2ulong(&hdr_ptr[100], 8);
129    file_size  = _rtems_octal2ulong(&hdr_ptr[124], 12);
130    hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8);
131
132    if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum)
133      break;
134
135    /*
136     * Generate an IMFS node depending on the file type.
137     * - For directories, just create directories as usual.  IMFS
138     *   will take care of the rest.
139     * - For files, create a file node with special tarfs properties.
140     */
141    if (linkflag == DIRTYPE) {
142      strcpy(full_filename, mountpoint);
143      if (full_filename[strlen(full_filename)-1] != '/')
144        strcat(full_filename, "/");
145      strcat(full_filename, filename);
146      mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO);
147    }
148    /*
149     * Create a LINEAR_FILE node
150     */
151    else if (linkflag == REGTYPE) {
152      rtems_filesystem_location_free( currentloc );
153      rtems_filesystem_location_clone( currentloc, &rootloc );
154      rtems_filesystem_eval_path_set_path(
155        &ctx,
156        filename,
157        strlen( filename )
158      );
159      rtems_filesystem_eval_path_continue( &ctx );
160
161      if ( !rtems_filesystem_location_is_null( currentloc ) ) {
162        node = IMFS_create_node(
163          currentloc,
164          IMFS_LINEAR_FILE,
165          rtems_filesystem_eval_path_get_token( &ctx ),
166          rtems_filesystem_eval_path_get_tokenlen( &ctx ),
167          (file_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) | S_IFREG,
168          NULL
169        );
170        node->info.linearfile.size   = file_size;
171        node->info.linearfile.direct = &tar_image[offset];
172      }
173
174      nblocks = (((file_size) + 511) & ~511) / 512;
175      offset += 512 * nblocks;
176    }
177  }
178
179  rtems_filesystem_location_free( &rootloc );
180  rtems_filesystem_eval_path_cleanup( &ctx );
181
182  return rv;
183}
184
Note: See TracBrowser for help on using the repository browser.