aboutsummaryrefslogtreecommitdiff
path: root/progressbar.c
blob: 8c5900a3e39d5a2a6c2bb3144d7ca47adff2f041 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/stat.h>


struct filtered_dir_entries {
    struct dirent ** entries;
    size_t entries_num;
};


static int search_dir(const char * const dir,
                      int (*filter_fn)(const struct dirent *),
                      struct filtered_dir_entries * const entries)
{
    struct dirent **names;
    int n;

    n = scandir(dir, &names, filter_fn, alphasort);
    if (n < 0) {
        return 1;
    }

    entries->entries = names;
    entries->entries_num = n;

    return 0;
}

static int dirent_filter_only_numeric(const struct dirent * ent)
{
    char * end_ptr;

    errno = 0;
    strtoul(ent->d_name, &end_ptr, 10);
    return (errno != 0 || *end_ptr == '\0');
}

static int search_procfs_fd(const char * const dir, const char * const subdir,
                              struct filtered_dir_entries * const entries)
{
    char buf[BUFSIZ];

    snprintf(buf, sizeof buf, "%s/%s/fd", dir ,subdir);
    return search_dir(buf, dirent_filter_only_numeric, entries);
}

static ssize_t realpath_procfs_fd(const char * const dir, const char * const pid,
                                  const char * const fd, char * dest, size_t siz)
{
    char buf[BUFSIZ];

    snprintf(buf, sizeof buf, "%s/%s/fd/%s", dir, pid, fd);
    return readlink(buf, dest, siz);
}

static void free_filtered_dir_entries(struct filtered_dir_entries * const entries)
{
    if (!entries->entries) {
        return;
    }
    for (size_t i = 0; i < entries->entries_num; ++i) {
        free(entries->entries[i]);
    }
    free(entries->entries);
    entries->entries = NULL;
    entries->entries_num = 0;
}

int main(int argc, char **argv)
{
    struct filtered_dir_entries proc_pid_entries = {};
    struct filtered_dir_entries proc_fd_entries = {};
    ssize_t realpath_used;
    size_t target_filepath_len;
    char file_realpath[BUFSIZ] = {};
    char pid[32];
    char fd[32];
    bool found_target = false;

    if (argc != 2) {
        fprintf(stderr, "usage: %s [FILE]\n", (argc > 0 ? argv[0] : "progressbar"));
        exit(EXIT_FAILURE);
    }
    target_filepath_len = strlen(argv[1]);

    if (search_dir("/proc", dirent_filter_only_numeric, &proc_pid_entries)) {
        exit(EXIT_FAILURE);
    }
    for (size_t i = 0; i < proc_pid_entries.entries_num; ++i) {
        if (search_procfs_fd("/proc", proc_pid_entries.entries[i]->d_name,
                             &proc_fd_entries)) {
            continue;
        }

        for (size_t j = 0; j < proc_fd_entries.entries_num; ++j) {
            realpath_used = realpath_procfs_fd("/proc",
                                proc_pid_entries.entries[i]->d_name,
                                proc_fd_entries.entries[j]->d_name,
                                &file_realpath[0], sizeof file_realpath);
            if (realpath_used <= 0) {
                continue;
            }
            file_realpath[realpath_used] = '\0';
            if (realpath_used == target_filepath_len) {
                if (!strncmp(argv[1], file_realpath, realpath_used)) {
                    found_target = true;
                    if (snprintf(pid, sizeof pid, "%s", proc_pid_entries.entries[i]->d_name) <= 0) {
                        found_target = false;
                    }
                    if (snprintf(fd, sizeof fd, "%s", proc_fd_entries.entries[j]->d_name) <= 0) {
                        found_target = false;
                    }
                    break;
                }
            }
        }
        free_filtered_dir_entries(&proc_fd_entries);

        if (found_target) {
            break;
        }
    }
    free_filtered_dir_entries(&proc_pid_entries);

    if (!found_target) {
        fprintf(stderr, "%s: file '%s' not found in /proc\n", argv[0], argv[1]);
        exit(EXIT_FAILURE);
    }

    printf("PID: %s, FD: %s, FILE: '%.*s'\n", pid, fd, (int)realpath_used, file_realpath);

    char proc_fd_path[BUFSIZ];
    if (snprintf(proc_fd_path, sizeof proc_fd_path, "%s/%s/fd/%s", "/proc", pid, fd) <= 0) {
        exit(EXIT_FAILURE);
    }

    int proc_fd_fd = open(proc_fd_path, 0);
    if (proc_fd_fd < 0) {
        perror("open proc_fd");
        exit(EXIT_FAILURE);
    }

    struct stat buf = {};
    if (fstat(proc_fd_fd, &buf)) {
        perror("stat proc_fd");
        exit(EXIT_FAILURE);
    }
    close(proc_fd_fd);

    printf("Total size: %lld\n", (long long) buf.st_size);

    char proc_fdinfo_path[BUFSIZ];
    if (snprintf(proc_fdinfo_path, sizeof proc_fdinfo_path, "%s/%s/fdinfo/%s", "/proc", pid, fd) <= 0) {
        exit(EXIT_FAILURE);
    }

    int proc_fdinfo_fd = open(proc_fdinfo_path, 0);
    if (proc_fdinfo_fd < 0) {
        perror("open proc_fdinfo");
        exit(EXIT_FAILURE);
    }

    ssize_t nread;
    char proc_fdinfo[BUFSIZ];
    nread = read(proc_fdinfo_fd, &proc_fdinfo[0], sizeof proc_fdinfo);
    if (nread <= 0) {
        perror("read proc_fdinfo");
        exit(EXIT_FAILURE);
    }
    proc_fdinfo[nread - 1] = '\0';
    close(proc_fdinfo_fd);

    static const char needle[] = "pos:\t";
    char * pospos = strstr(&proc_fdinfo[0], &needle[0]);
    if (!pospos) {
        exit(EXIT_FAILURE);
    }
    pospos += (sizeof(needle) - 1) / sizeof(needle[0]);

    unsigned long long int current_position = strtoull(pospos, NULL, 10);
    printf("Current position: %llu\n", current_position);
}