dpl_status_t dpltest_upload_file(dpl_ctx_t *ctx, char *path, char *blob_buf, int blob_size, int buffered, int block_size) { dpl_status_t ret, ret2; dpl_canned_acl_t canned_acl = DPL_CANNED_ACL_PRIVATE; dpl_dict_t *metadata = NULL; dpl_vfile_t *vfile = NULL; int retries = 0; dpl_vfile_flag_t flags = 0u; dpl_sysmd_t sysmd; int remain, buf_size, off; memset(&sysmd, 0, sizeof (sysmd)); sysmd.mask = DPL_SYSMD_MASK_CANNED_ACL; sysmd.canned_acl = canned_acl; flags = DPL_VFILE_FLAG_CREAT; //flags |= DPL_VFILE_FLAG_POST; if (!buffered) { flags |= DPL_VFILE_FLAG_ONESHOT; block_size = blob_size; } retry: if (retries >= 3) { fprintf(stderr, "too many retries: %s (%d)\n", dpl_status_str(ret), ret); ret = DPL_FAILURE; goto end; } retries++; ret2 = dpl_openwrite(ctx, path, DPL_FTYPE_REG, flags, NULL, metadata, &sysmd, blob_size, NULL, &vfile); if (DPL_SUCCESS != ret2) { if (DPL_ENOENT == ret2) { ret = DPL_ENOENT; } goto retry; } remain = blob_size; off = 0; while (remain > 0) { buf_size = MIN(remain, block_size); ret = dpl_write(vfile, blob_buf + off, buf_size); if (DPL_SUCCESS != ret) { fprintf(stderr, "write failed\n"); goto retry; } off += buf_size; remain -= buf_size; } ret = dpl_close(vfile); if (DPL_SUCCESS != ret) { fprintf(stderr, "close failed %s (%d)\n", dpl_status_str(ret), ret); goto retry; } vfile = NULL; ret = DPL_SUCCESS; end: if (NULL != metadata) dpl_dict_free(metadata); if (NULL != vfile) dpl_close(vfile); return ret; }
int cmd_put(int argc, char **argv) { int ret; char opt; dpl_canned_acl_t canned_acl = DPL_CANNED_ACL_PRIVATE; int Aflag = 0; int i; int fd = -1; dpl_dict_t *metadata = NULL; char *local_file = NULL; char *remote_file = NULL; dpl_vfile_t *vfile = NULL; size_t block_size = 64*1024; ssize_t cc; struct stat st; int kflag = 0; char *buf; var_set("status", "1", VAR_CMD_SET, NULL); optind = 0; while ((opt = getopt(argc, argv, usage_getoptstr(put_usage))) != -1) switch (opt) { case 'k': kflag = 1; break ; case 'm': metadata = dpl_parse_metadata(optarg); if (NULL == metadata) { fprintf(stderr, "error parsing metadata\n"); return SHELL_CONT; } break ; case 'a': canned_acl = dpl_canned_acl(optarg); if (-1 == canned_acl) { fprintf(stderr, "bad canned acl '%s'\n", optarg); return SHELL_CONT; } break ; case 'A': Aflag = 1; break ; case '?': default: usage_help(&put_cmd); return SHELL_CONT; } argc -= optind; argv += optind; if (1 == Aflag) { for (i = 0;i < DPL_N_CANNED_ACL;i++) printf("%s\n", dpl_canned_acl_str(i)); return SHELL_CONT; } if (2 == argc) { char *p, *p2; local_file = argv[0]; remote_file = argv[1]; p = index(remote_file, ':'); if (NULL != p) { p++; if (!strcmp(p, "")) { p2 = rindex(local_file, '/'); if (NULL != p2) { p2++; strcat(remote_file, p2); } else { strcat(remote_file, local_file); } } } } else if (1 == argc) { local_file = argv[0]; remote_file = rindex(local_file, '/'); if (NULL != remote_file) remote_file++; else remote_file = local_file; } else { usage_help(&put_cmd); return SHELL_CONT; } fd = open(local_file, O_RDONLY); if (-1 == fd) { perror("open"); goto end; } buf = alloca(block_size); ret = fstat(fd, &st); if (-1 == ret) { perror("fstat"); return SHELL_CONT; } ret = dpl_openwrite(ctx, remote_file, DPL_VFILE_FLAG_CREAT | (1 == kflag ? DPL_VFILE_FLAG_ENCRYPT : DPL_VFILE_FLAG_MD5), metadata, canned_acl, st.st_size, &vfile); if (DPL_SUCCESS != ret) { fprintf(stderr, "status: %s (%d)\n", dpl_status_str(ret), ret); return SHELL_CONT; } while (1) { cc = read(fd, buf, block_size); if (-1 == cc) { perror("read"); return -1; } if (0 == cc) { break ; } ret = dpl_write(vfile, buf, cc); if (DPL_SUCCESS != ret) { fprintf(stderr, "write failed\n"); return SHELL_CONT; } if (1 == hash) { fprintf(stderr, "#"); fflush(stderr); } } ret = dpl_close(vfile); if (DPL_SUCCESS != ret) { fprintf(stderr, "close failed %s (%d)\n", dpl_status_str(ret), ret); return SHELL_CONT; } vfile = NULL; var_set("status", "0", VAR_CMD_SET, NULL); end: if (-1 != fd) close(fd); if (NULL != metadata) dpl_dict_free(metadata); if (NULL != vfile) dpl_close(vfile); return SHELL_CONT; }