void read_from(enum eyefi_file __file) { int tries = 0; int ret; int fd; char *file = eyefi_file(__file); int nr_fresh; init_card(); retry: fd = open(file, O_RDONLY); if (fd < 0) open_error(file, fd); fd_flush(fd); // fd_flush() does not appear to be working 100% of the // time. It is not working on my Thinkpad, but works // fine on the same kernel on the Ideapad. Bizarre. // This at least works around it by detecting when we // did and did not actually bring in pages from the // disk. nr_fresh = nr_fresh_pages(fd, EYEFI_BUF_SIZE); if (!nr_fresh) { tries++; debug_printf(2, "fd_flush(%d) was unsuccessful(%d), retrying (%d)...\n", fd, nr_fresh, tries); close(fd); goto retry; } ret = read(fd, eyefi_buf, EYEFI_BUF_SIZE); if ((eyefi_debug_level >= 3) || (eyefi_debug_level >= 2 && (__file == RSPM))) { printf("%s:", eyefi_file_name(__file)); dumpbuf(eyefi_buf, 128); } if (ret < 0) { close(fd); perror("bad read, retrying..."); goto retry; exit(1); } debug_printf(4, "read '%s': bytes: %d\n", file, ret); /* * There was a time when I was carefully recording how each response * looked, and I counted the zeros in each response. I don't care * any more. u8 c; int zeros = 0; int i; for (i=0; i < EYEFI_BUF_SIZE; i++) { c = ((char *)eyefi_buf)[i]; if (c == '\0') { zeros++; continue; } } */ free(file); close(fd); }
int create_control_files(char *mnt) { char *control_dir = eyefi_file_on(RDIR, mnt); int ret = 0; enum eyefi_file file; ret = mkdir(control_dir, 0644); debug_printf(1, "making control directory: '%s', errno: %d\n", control_dir, errno); free(control_dir); if ((ret != 0) && (errno != EEXIST)) { perror("unable to create Eye-Fi control directory"); return errno; } for (file = REQC; file <= RSPM; file++) { ret = zero_file(file, mnt); debug_printf(2, "trying to create control file: '%s', ret: %d\n", eyefi_file_name(file), ret); if (ret) { perror("unable to create control file"); goto out; } } out: return ret; }
char *eyefi_file_on(enum eyefi_file file, char *mnt) { char *filename = eyefi_file_name(file); char *full = malloc(PATHNAME_MAX); if (!full) return NULL; sprintf(&full[0], "%s/EyeFi/%s", mnt, filename); debug_printf(4, "eyefile nr: %d on '%s' is: '%s'\n", file, mnt, &full[0]); return full; }
void write_to(enum eyefi_file __file, void *stuff, int len) { int ret; int wrote; int fd; char *file; if (fake_write) return; init_card(); file = eyefi_file(__file); if (len == -1) len = strlen(stuff); memset(eyefi_buf, 0, EYEFI_BUF_SIZE); memcpy(eyefi_buf, stuff, len); fd = open(file, O_RDWR|O_CREAT, 0600); if (fd < 0 ) open_error(file, fd); if ((eyefi_debug_level >= 3) || (eyefi_debug_level >= 2 && (__file == REQM))) { printf("%s:", eyefi_file_name(__file)); dumpbuf(eyefi_buf, 128); } wrote = write(fd, eyefi_buf, EYEFI_BUF_SIZE); if (wrote < 0) open_error(file, wrote); ret = fd_flush(fd); if (ret < 0) open_error(file, ret); close(fd); debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", wrote, file, len); if (ret < 0) { fprintf(stderr, "error writing to '%s': ", file); perror(""); exit(ret); } free(file); }