int npc_puts (Npcfid *fid, char *buf) { int n, count = strlen (buf), done = 0; while (done < count) { n = npc_write(fid, buf + done, count - done); if (n < 0) { done = -1; break; } done += n; } return done; }
int npc_put(Npcfid *root, char *path, void *buf, u32 count) { int n, done = 0; Npcfid *fid; if (!(fid = npc_open_bypath (root, path, O_WRONLY))) return -1; while (done < count) { n = npc_write(fid, buf + done, count - done); if (n < 0) { done = -1; break; } done += n; } if (npc_clunk (fid) < 0) done = -1; return done; }
int npc_setxattr (Npcfid *root, char *path, char *name, char *val, size_t size, int flags) { Npcfid *fid = NULL; int n, tot = 0; int ret = -1; if (!(fid = npc_walk (root, path))) goto done; if (npc_xattrcreate (fid, name, size, flags) < 0) goto done; for (tot = 0; tot < size; tot += n) { n = npc_write(fid, val + tot, size - tot); if (n < 0) goto done; } ret = 0; done: if (fid) ret = npc_clunk(fid); return ret; }
int main(int argc, char **argv) { int i, n, off; int c, port, dotu; char *addr, *s; char *path; Npuser *user; Npcfsys *fs; Npcfid *fid; char buf[512]; port = 564; // npc_chatty = 1; #ifdef _WIN32 init(); user = np_default_users->uname2user(np_default_users, "nobody"); #else user = np_default_users->uid2user(np_default_users, geteuid()); if (!user) { fprintf(stderr, "cannot retrieve user %d\n", geteuid()); exit(1); } #endif dotu = 1; while ((c = getopt(argc, argv, "dp:u:U")) != -1) { switch (c) { case 'd': npc_chatty = 1; break; case 'p': port = strtol(optarg, &s, 10); if (*s != '\0') usage(); break; case 'u': user = np_default_users->uname2user(np_default_users, optarg); break; case 'U': dotu = 0; break; default: usage(); } } if (argc - optind < 2) usage(); addr = argv[optind]; path = argv[optind+1]; fs = npc_netmount(npc_netaddr(addr, port), dotu, user, port, NULL, NULL); fid = npc_open(fs, path, Owrite); if (!fid) { fid = npc_create(fs, path, 0666, Owrite); if (!fid) { fprintf(stderr, "error creating\n"); exit(1); } } off = 0; while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) { i = npc_write(fid, (u8*) buf, n, off); if (i != n) { fprintf(stderr, "error writing\n"); exit(1); } off += n; } npc_close(fid); npc_umount(fs); exit(0); }
int main(int argc, char **argv) { int sfd, i, n, off; int c, port; char *addr, *s; char *uname, *path; Npuser *user; Npcfsys *fs; Npcfid *fid; struct sockaddr_in saddr; struct hostent *hostinfo; char buf[512]; port = 564; // npc_chatty = 1; user = np_uid2user(getuid()); if (!user) { fprintf(stderr, "cannot retrieve user %d\n", getuid()); exit(1); } uname = user->uname; while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': npc_chatty = 1; break; case 'p': port = strtol(optarg, &s, 10); if (*s != '\0') usage(); break; case 'u': uname = optarg; break; default: usage(); } } if (argc - optind < 2) usage(); addr = argv[optind]; path = argv[optind+1]; sfd = socket(PF_INET, SOCK_STREAM, 0); if (sfd < 0) { perror("socket"); exit(1); } hostinfo = gethostbyname (addr); if (!hostinfo) { perror("gethostbyname"); exit(1); } saddr.sin_family = AF_INET; saddr.sin_port = htons(port); saddr.sin_addr = *(struct in_addr *) hostinfo->h_addr; if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { perror("connect"); exit(1); } fs = npc_mount(sfd, NULL, "lucho"); fid = npc_open(fs, path, Owrite); if (!fid) { fid = npc_create(fs, path, 0666, Owrite); if (!fid) { fprintf(stderr, "error creating\n"); exit(1); } } off = 0; while ((n = read(0, buf, sizeof(buf))) > 0) { i = npc_write(fid, (u8*) buf, n, off); if (i != n) { fprintf(stderr, "error writing\n"); exit(1); } off += n; } npc_close(fid); npc_umount(fs); exit(0); }