Skip to content

Instantly share code, notes, and snippets.

@Mygod
Created November 11, 2016 02:45
Show Gist options
  • Save Mygod/664ab8b02fb984440ff3887a3ffb9815 to your computer and use it in GitHub Desktop.
Save Mygod/664ab8b02fb984440ff3887a3ffb9815 to your computer and use it in GitHub Desktop.
Detect errors in your device.
/**
* Detect errors in your device.
*
* Usage: sudo ./lserrs <page size in bytes> <device name>
*/
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <memory>
#include <sstream>
using namespace std;
int main(int argc, char **argv) {
istringstream is(argv[1]);
size_t pageSize;
is >> pageSize;
auto buffer = make_unique<char[]>(pageSize);
auto fd = open(argv[2], O_RDONLY);
auto size = lseek(fd, 0, SEEK_END);
auto err = false;
if (size % pageSize) {
cout << "Wrong page size!" << endl;
goto fail;
}
lseek(fd, 0, SEEK_SET);
cout << hex;
for (__off_t i = 0; i < size; i += pageSize) {
if (err) lseek(fd, i, SEEK_SET);
if (read(fd, buffer.get(), pageSize) != pageSize) {
if (err) continue;
cout << i << '-';
err = true;
} else if (err) {
cout << i << endl;
err = false;
}
}
if (err) cout << size << endl;
fail:
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment