123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include "libbb.h"
- #include "bb_archive.h"
- struct fileblock {
- struct fileblock *next;
- char data[TAR_BLOCK_SIZE];
- };
- static void writeheader(const char *path, struct stat *sb, int type)
- {
- struct tar_header_t header;
- int i, sum;
- memset(&header, 0, TAR_BLOCK_SIZE);
- strcpy(header.name, path);
- sprintf(header.mode, "%o", sb->st_mode & 0777);
-
- sprintf(header.uid, "%o", sb->st_uid & 07777777);
- sprintf(header.gid, "%o", sb->st_gid & 07777777);
- sprintf(header.size, "%o", (unsigned)sb->st_size);
- sprintf(header.mtime, "%llo", sb->st_mtime & 077777777777LL);
- header.typeflag = type;
- strcpy(header.magic, "ustar ");
-
- header.chksum[7] = ' ';
- sum = ' ' * 7;
- for (i = 0; i < TAR_BLOCK_SIZE; i++)
- sum += ((unsigned char*)&header)[i];
- sprintf(header.chksum, "%06o", sum);
- xwrite(STDOUT_FILENO, &header, TAR_BLOCK_SIZE);
- }
- static void archivefile(const char *path)
- {
- struct fileblock *start, *cur;
- struct fileblock **prev = &start;
- int fd, r;
- unsigned size = 0;
- struct stat s;
-
- fd = xopen(path, O_RDONLY);
- do {
- cur = xzalloc(sizeof(*cur));
- *prev = cur;
- prev = &cur->next;
- r = full_read(fd, cur->data, TAR_BLOCK_SIZE);
- if (r > 0)
- size += r;
- } while (r == TAR_BLOCK_SIZE);
-
- fstat(fd, &s);
- close(fd);
- s.st_size = size;
- writeheader(path, &s, '0');
-
- for (cur = start; (int)size > 0; size -= TAR_BLOCK_SIZE) {
- xwrite(STDOUT_FILENO, cur->data, TAR_BLOCK_SIZE);
- start = cur;
- cur = cur->next;
- free(start);
- }
- }
- static void archivejoin(const char *sub, const char *name)
- {
- char path[sizeof(long long)*3 + sizeof("/cmdline")];
- sprintf(path, "%s/%s", sub, name);
- archivefile(path);
- }
- int smemcap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int smemcap_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
- {
- DIR *d;
- struct dirent *de;
- xchdir("/proc");
- d = xopendir(".");
- archivefile("meminfo");
- archivefile("version");
- while ((de = readdir(d)) != NULL) {
- if (isdigit(de->d_name[0])) {
- struct stat s;
- memset(&s, 0, sizeof(s));
- s.st_mode = 0555;
- writeheader(de->d_name, &s, '5');
- archivejoin(de->d_name, "smaps");
- archivejoin(de->d_name, "cmdline");
- archivejoin(de->d_name, "stat");
- }
- }
- if (ENABLE_FEATURE_CLEAN_UP)
- closedir(d);
- return EXIT_SUCCESS;
- }
|