File: udfls.c

package info (click to toggle)
libudfread 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 268 kB
  • sloc: ansic: 2,076; makefile: 3
file content (112 lines) | stat: -rw-r--r-- 3,211 bytes parent folder | download | duplicates (2)
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
111
112
/*
 * This file is part of libudfread
 * Copyright (C) 2014-2015 VLC authors and VideoLAN
 *
 * Authors: Petri Hintukainen <phintuka@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>

#include "udfread.h"

static int _lsdir_at(UDFDIR *dir, const char *path, int depth)
{
    struct udfread_dirent dirent;

    while (udfread_readdir(dir, &dirent)) {
        if (!strcmp(dirent.d_name, ".") || !strcmp(dirent.d_name, "..")) continue;

        if (dirent.d_type == UDF_DT_DIR) {
            UDFDIR *child;
            char *next_dir;

            printf("\t\t %s%s\n", path, dirent.d_name);

            next_dir = (char*)malloc(strlen(path) + strlen(dirent.d_name) + 2);
            if (!next_dir) {
                fprintf(stderr, "out of memory\n");
                continue;
            }
            sprintf(next_dir, "%s%s/",  path, dirent.d_name);

            child = udfread_opendir_at(dir, dirent.d_name);
            if (!child) {
                free(next_dir);
                fprintf(stderr, "error opening directory %s\n", dirent.d_name);
                continue;
            }

            _lsdir_at(child, next_dir, depth + 1);
            udfread_closedir(child);

            free(next_dir);
        } else {
            UDFFILE *fp;

            fp = udfread_file_openat(dir, dirent.d_name);
            if (!fp) {
                fprintf(stderr, "error opening file '%s%s'\n", path, dirent.d_name);
                continue;
            }
            printf("%16" PRId64 " %s%s\n",  udfread_file_size(fp), path, dirent.d_name);
            udfread_file_close(fp);
        }
    }

    return 0;
}

int main(int argc, const char *argv[])
{
    udfread *udf;
    UDFDIR *root;

    if (argc < 2) {
        fprintf(stderr, "usage: udfls <path>\n"
                "    path   path to UDF filesystem image (raw device or image file)\n");
        return -1;
    }

    udf = udfread_init();
    if (!udf) {
        fprintf(stderr, "udfread_init() failed\n");
        return -1;
    }
    if (udfread_open(udf, argv[1]) < 0) {
        fprintf(stderr, "udfread_open(%s) failed\n", argv[1]);
        udfread_close(udf);
        return -1;
    }

    printf("Volume ID: %s\n", udfread_get_volume_id(udf));

    root  = udfread_opendir(udf, "/");
    if (!root) {
        fprintf(stderr, "error opening root directory\n");
    } else {
        _lsdir_at(root, "/", 0);
        udfread_closedir(root);
    }

    udfread_close(udf);

    return 1;
}