#include #include #include typedef uint32_t nfs_fh_t; static const char file_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static const size_t file_len = 26; int nfs_read(nfs_fh_t fh, uint64_t offset, uint32_t count, char *buf, uint32_t *nread) { printf("NFS-REQ READ fh=%u off=%llu cnt=%u\n", (unsigned)fh, (unsigned long long)offset, count); if (fh != 7) { printf("NFS-ERR stale handle\n"); return -1; } if (offset >= file_len) { *nread = 0; printf("NFS-REP 0 bytes (EOF)\n"); return 0; } size_t remain = file_len - (size_t)offset; uint32_t copy = count < remain ? count : (uint32_t)remain; memcpy(buf, file_data + (size_t)offset, copy); *nread = copy; printf("NFS-REP %u bytes\n", copy); return 0; } int main(void) { char buf[32]; uint32_t got; nfs_fh_t fh = 7; printf("Client starts using NFS file handle 7\n"); if (nfs_read(fh, 0, 5, buf, &got) == 0) { buf[got] = '\0'; printf("DATA: %s\n", buf); } if (nfs_read(fh, 10, 5, buf, &got) == 0) { buf[got] = '\0'; printf("DATA: %s\n", buf); } printf(">>> simulated server crash and restart <<<\n"); if (nfs_read(fh, 20, 10, buf, &got) == 0) { buf[got] = '\0'; printf("DATA: %s\n", buf); } nfs_read(fh, 30, 4, buf, &got); printf("EOF reached as expected\n"); return 0; }