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

5
Last change on this file since b6f66d9 was b6f66d9, checked in by Sebastian Huber <sebastian.huber@…>, on 11/21/19 at 07:06:28

untar: Unify untar support

Update #3823.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/**
2 * @file
3 *
4 * @brief RTEMS Load Tarfs
5 * @ingroup IMFS
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2010.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/imfs.h>
22#include <rtems/untar.h>
23
24#include <string.h>
25
26int rtems_tarfs_load(
27  const char *mountpoint,
28  uint8_t    *tar_image,
29  size_t      tar_size
30)
31{
32  rtems_filesystem_eval_path_context_t  eval_ctx;
33  int                                   eval_flags;
34  rtems_filesystem_location_info_t     *loc;
35  bool                                  is_imfs;
36  char                                  buf[ 156 + UNTAR_FILE_NAME_SIZE ];
37  size_t                                len;
38  Untar_HeaderContext                   ctx;
39  unsigned long                         ptr;
40
41  len = strlen( mountpoint );
42  if ( len >= sizeof( buf ) - UNTAR_FILE_NAME_SIZE - 2 ) {
43    return -1;
44  }
45
46  eval_flags = RTEMS_FS_FOLLOW_LINK;
47  loc = rtems_filesystem_eval_path_start( &eval_ctx, mountpoint, eval_flags );
48  is_imfs = IMFS_is_imfs_instance( loc );
49  rtems_filesystem_eval_path_cleanup( &eval_ctx );
50  if ( !is_imfs ) {
51    return -1;
52  }
53
54  ctx.printer = NULL;
55  ctx.file_path = memcpy( buf, mountpoint, len );
56
57  if ( len > 0 && ctx.file_path[ len - 1 ] != '/') {
58    ctx.file_path[ len ] = '/';
59    ctx.file_name = ctx.file_path + len + 1;
60  } else {
61    ctx.file_name = ctx.file_path + len;
62  }
63
64  ptr = 0;
65
66  while ( ptr + 512 <= tar_size ) {
67    int retval;
68
69    retval = Untar_ProcessHeader( &ctx, (const char *) &tar_image[ ptr ] );
70    if ( retval != UNTAR_SUCCESSFUL ) {
71      return -1;
72    }
73
74    ptr += 512;
75
76    if ( ctx.linkflag == REGTYPE ) {
77      retval = IMFS_make_linearfile(
78        ctx.file_path,
79        ctx.mode,
80        &tar_image[ ptr ],
81        ctx.file_size
82      );
83      if ( retval != 0 ) {
84        return -1;
85      }
86
87      ptr += 512 * ctx.nblocks;
88    }
89  }
90
91  return 0;
92}
Note: See TracBrowser for help on using the repository browser.