static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) { u_char *p, *dir; size_t len; ngx_http_dav_copy_ctx_t *copy; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, "http copy dir: \"%s\"", path->data); copy = ctx->data; len = copy->path.len + path->len; dir = ngx_alloc(len + 1, ctx->log); if (dir == NULL) { return NGX_ABORT; } p = ngx_cpymem(dir, copy->path.data, copy->path.len); (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, "http copy dir to: \"%s\"", dir); if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, dir); } ngx_free(dir); return NGX_OK; }
int ngx_create_path(ngx_file_t *file, ngx_path_t *path) { int i, pos; ngx_err_t err; pos = path->name.len; for (i = 0; i < 3; i++) { if (path->level[i] == 0) { break; } pos += path->level[i] + 1; file->name.data[pos] = '\0'; ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0, "temp file: \"%s\"", file->name.data); if (ngx_create_dir(file->name.data) == NGX_FILE_ERROR) { err = ngx_errno; if (err != NGX_EEXIST) { ngx_log_error(NGX_LOG_CRIT, file->log, err, ngx_create_dir_n " \"%s\" failed", file->name.data); return NGX_ERROR; } } file->name.data[pos] = '/'; } return NGX_OK; }
ngx_err_t ngx_create_full_path(u_char *dir, ngx_uint_t access) { u_char *p, ch; ngx_err_t err; for (p = dir + 1; *p; p++) { ch = *p; if (ch != '/') { continue; } *p = '\0'; if (ngx_create_dir(dir, access) == NGX_FILE_ERROR) { err = ngx_errno; if (err != NGX_EEXIST) { return err; } } *p = '/'; } return 0; }
static ngx_int_t ngx_http_groonga_mkdir_p(ngx_log_t *log, const char *dir_name) { char sub_path[PATH_MAX]; size_t i, dir_name_length; dir_name_length = strlen(dir_name); sub_path[0] = dir_name[0]; for (i = 1; i < dir_name_length + 1; i++) { if (dir_name[i] == '/' || dir_name[i] == '\0') { struct stat stat_buffer; sub_path[i] = '\0'; if (stat(sub_path, &stat_buffer) == -1) { if (ngx_create_dir(sub_path, 0700) == -1) { ngx_log_error(NGX_LOG_EMERG, log, 0, "failed to create directory: %s (%s): %s", sub_path, dir_name, strerror(errno)); return NGX_ERROR; } } } sub_path[i] = dir_name[i]; } return NGX_OK; }
static void ngx_pipe_create_subdirs(char *filename, ngx_cycle_t *cycle) { ngx_file_info_t stat_buf; char dirname[1024]; char *p; for (p = filename; (p = strchr(p, '/')); p++) { if (p == filename) { continue; // Don't bother with the root directory } ngx_memcpy(dirname, filename, p - filename); dirname[p-filename] = '\0'; if (ngx_file_info(dirname, &stat_buf) < 0) { if (errno != ENOENT) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "stat [%s] failed", dirname); exit(2); } else { if ((ngx_create_dir(dirname, NGX_PIPE_DIR_ACCESS) < 0) && (errno != EEXIST)) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "mkdir [%s] failed", dirname); exit(2); } } } } }
static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf) { u_char *p; size_t root; ngx_str_t path; if (r->headers_in.content_length_n > 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "MKCOL with body is unsupported"); return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; } if (r->uri.data[r->uri.len - 1] != '/') { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "MKCOL can create a collection only"); return NGX_HTTP_CONFLICT; } p = ngx_http_map_uri_to_path(r, &path, &root, 0); if (p == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } *(p - 1) = '\0'; r->uri.len--; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http mkcol path: \"%s\"", path.data); if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) != NGX_FILE_ERROR) { if (ngx_http_dav_location(r, path.data) != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } return NGX_HTTP_CREATED; } return ngx_http_dav_error(r->connection->log, ngx_errno, NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data); }
ngx_err_t ngx_create_full_path(u_char *dir, ngx_uint_t access) { u_char *p, ch; ngx_err_t err; err = 0; #if (NGX_WIN32) //deleted by fangpeng #else p = dir + 1; #endif for ( /* void */ ; *p; p++) { ch = *p; if (ch != '/') { continue; } *p = '\0'; if (ngx_create_dir(dir, access) == NGX_FILE_ERROR) { err = ngx_errno; switch (err) { case NGX_EEXIST: err = 0; case NGX_EACCES: break; default: return err; } } *p = '/'; } return err; }
static ngx_int_t ngx_http_fsutil_create_dir_if_not_found( ngx_log_t *log, char *path ) { ngx_file_info_t fi; ngx_int_t rc; if ( ngx_file_info( path, &fi ) == NGX_FILE_ERROR ) { rc = ngx_create_dir( path, 0700 ); if ( 0 != rc ) { ngx_log_error(NGX_LOG_ERR, log, 0, "creating dir failed:%s", path); /* TODO elaborate error */ return rc; } } else { if ( ! ngx_is_dir( &fi ) ) { return NGX_ENOTDIR; } /* * if ( ngx_is_dir( &fi ) ) { * return NGX_OK; * } * else if ( ngx_is_file( &fi ) || ngx_is_link( &fi ) ) { * ngx_log_error(NGX_LOG_ERR, log, 0, * "Normal file or link as path element:%s", path); * [> TODO elaborate error <] * return NGX_ENOTDIR; * } * else { * [> TODO elaborate error <] * return NGX_ENOTDIR; * } */ } return NGX_OK; }
static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r) { u_char *p, *host, *last, ch; size_t len, root; ngx_err_t err; ngx_int_t rc, depth; ngx_uint_t overwrite, slash, dir, flags; ngx_str_t path, uri, duri, args; ngx_tree_ctx_t tree; ngx_copy_file_t cf; ngx_file_info_t fi; ngx_table_elt_t *dest, *over; ngx_ext_rename_file_t ext; ngx_http_dav_copy_ctx_t copy; ngx_http_dav_loc_conf_t *dlcf; if (r->headers_in.content_length_n > 0) { return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; } dest = r->headers_in.destination; if (dest == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "client sent no \"Destination\" header"); return NGX_HTTP_BAD_REQUEST; } p = dest->value.data; /* there is always '\0' even after empty header value */ if (p[0] == '/') { last = p + dest->value.len; goto destination_done; } len = r->headers_in.server.len; if (len == 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "client sent no \"Host\" header"); return NGX_HTTP_BAD_REQUEST; } #if (NGX_HTTP_SSL) if (r->connection->ssl) { if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1) != 0) { goto invalid_destination; } host = dest->value.data + sizeof("https://") - 1; } else #endif { if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1) != 0) { goto invalid_destination; } host = dest->value.data + sizeof("http://") - 1; } if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"Destination\" URI \"%V\" is handled by " "different repository than the source URI", &dest->value); return NGX_HTTP_BAD_REQUEST; } last = dest->value.data + dest->value.len; for (p = host + len; p < last; p++) { if (*p == '/') { goto destination_done; } } invalid_destination: ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "client sent invalid \"Destination\" header: \"%V\"", &dest->value); return NGX_HTTP_BAD_REQUEST; destination_done: duri.len = last - p; duri.data = p; flags = NGX_HTTP_LOG_UNSAFE; if (ngx_http_parse_unsafe_uri(r, &duri, &args, &flags) != NGX_OK) { goto invalid_destination; } if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/') || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/')) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "both URI \"%V\" and \"Destination\" URI \"%V\" " "should be either collections or non-collections", &r->uri, &dest->value); return NGX_HTTP_CONFLICT; } depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { if (r->method == NGX_HTTP_COPY) { if (depth != 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"Depth\" header must be 0 or infinity"); return NGX_HTTP_BAD_REQUEST; } } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"Depth\" header must be infinity"); return NGX_HTTP_BAD_REQUEST; } } over = r->headers_in.overwrite; if (over) { if (over->value.len == 1) { ch = over->value.data[0]; if (ch == 'T' || ch == 't') { overwrite = 1; goto overwrite_done; } if (ch == 'F' || ch == 'f') { overwrite = 0; goto overwrite_done; } } ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "client sent invalid \"Overwrite\" header: \"%V\"", &over->value); return NGX_HTTP_BAD_REQUEST; } overwrite = 1; overwrite_done: ngx_http_map_uri_to_path(r, &path, &root, 0); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http copy from: \"%s\"", path.data); uri = r->uri; r->uri = duri; ngx_http_map_uri_to_path(r, ©.path, &root, 0); r->uri = uri; copy.path.len--; /* omit "\0" */ if (copy.path.data[copy.path.len - 1] == '/') { slash = 1; copy.path.len--; copy.path.data[copy.path.len] = '\0'; } else { slash = 0; } ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http copy to: \"%s\"", copy.path.data); if (ngx_link_info(copy.path.data, &fi) == NGX_FILE_ERROR) { err = ngx_errno; if (err != NGX_ENOENT) { return ngx_http_dav_error(r->connection->log, err, NGX_HTTP_NOT_FOUND, ngx_link_info_n, copy.path.data); } /* destination does not exist */ overwrite = 0; dir = 0; } else { /* destination exists */ if (ngx_is_dir(&fi) && !slash) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"%V\" could not be %Ved to collection \"%V\"", &r->uri, &r->method_name, &dest->value); return NGX_HTTP_CONFLICT; } if (!overwrite) { ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST, "\"%s\" could not be created", copy.path.data); return NGX_HTTP_PRECONDITION_FAILED; } dir = ngx_is_dir(&fi); } if (ngx_link_info(path.data, &fi) == NGX_FILE_ERROR) { return ngx_http_dav_error(r->connection->log, ngx_errno, NGX_HTTP_NOT_FOUND, ngx_link_info_n, path.data); } if (ngx_is_dir(&fi)) { if (r->uri.data[r->uri.len - 1] != '/') { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"%V\" is collection", &r->uri); return NGX_HTTP_BAD_REQUEST; } if (overwrite) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http delete: \"%s\"", copy.path.data); rc = ngx_http_dav_delete_path(r, ©.path, dir); if (rc != NGX_OK) { return rc; } } } if (ngx_is_dir(&fi)) { path.len -= 2; /* omit "/\0" */ if (r->method == NGX_HTTP_MOVE) { if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { return NGX_HTTP_CREATED; } } if (ngx_create_dir(copy.path.data, ngx_file_access(&fi)) == NGX_FILE_ERROR) { return ngx_http_dav_error(r->connection->log, ngx_errno, NGX_HTTP_NOT_FOUND, ngx_create_dir_n, copy.path.data); } copy.len = path.len; tree.init_handler = NULL; tree.file_handler = ngx_http_dav_copy_tree_file; tree.pre_tree_handler = ngx_http_dav_copy_dir; tree.post_tree_handler = ngx_http_dav_copy_dir_time; tree.spec_handler = ngx_http_dav_noop; tree.data = © tree.alloc = 0; tree.log = r->connection->log; if (ngx_walk_tree(&tree, &path) == NGX_OK) { if (r->method == NGX_HTTP_MOVE) { rc = ngx_http_dav_delete_path(r, &path, 1); if (rc != NGX_OK) { return rc; } } return NGX_HTTP_CREATED; } } else { if (r->method == NGX_HTTP_MOVE) { dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); ext.access = 0; ext.path_access = dlcf->access; ext.time = -1; ext.create_path = 1; ext.delete_file = 0; ext.log = r->connection->log; if (ngx_ext_rename_file(&path, ©.path, &ext) == NGX_OK) { return NGX_HTTP_NO_CONTENT; } return NGX_HTTP_INTERNAL_SERVER_ERROR; } dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); cf.size = ngx_file_size(&fi); cf.buf_size = 0; cf.access = dlcf->access; cf.time = ngx_file_mtime(&fi); cf.log = r->connection->log; if (ngx_copy_file(path.data, copy.path.data, &cf) == NGX_OK) { return NGX_HTTP_NO_CONTENT; } } return NGX_HTTP_INTERNAL_SERVER_ERROR; }
ngx_int_t ngx_create_paths(ngx_cycle_t *cycle, ngx_uid_t user) { ngx_err_t err; ngx_uint_t i; ngx_path_t **path; path = cycle->paths.elts; for (i = 0; i < cycle->paths.nelts; i++) { if (ngx_create_dir(path[i]->name.data, 0700) == NGX_FILE_ERROR) { err = ngx_errno; if (err != NGX_EEXIST) { ngx_log_error(NGX_LOG_EMERG, cycle->log, err, ngx_create_dir_n " \"%s\" failed", path[i]->name.data); return NGX_ERROR; } } if (user == (ngx_uid_t) NGX_CONF_UNSET_UINT) { continue; } #if !(NGX_WIN32) { ngx_file_info_t fi; if (ngx_file_info((const char *) path[i]->name.data, &fi) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, ngx_file_info_n " \"%s\" failed", path[i]->name.data); return NGX_ERROR; } if (fi.st_uid != user) { if (chown((const char *) path[i]->name.data, user, -1) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "chown(\"%s\", %d) failed", path[i]->name.data, user); return NGX_ERROR; } } if ((fi.st_mode & (S_IRUSR|S_IWUSR|S_IXUSR)) != (S_IRUSR|S_IWUSR|S_IXUSR)) { fi.st_mode |= (S_IRUSR|S_IWUSR|S_IXUSR); if (chmod((const char *) path[i]->name.data, fi.st_mode) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "chmod() \"%s\" failed", path[i]->name.data); return NGX_ERROR; } } } #endif } return NGX_OK; }
static ngx_int_t ngx_rtmp_dash_ensure_directory(ngx_rtmp_session_t *s) { size_t len; ngx_file_info_t fi; ngx_rtmp_dash_ctx_t *ctx; ngx_rtmp_dash_app_conf_t *dacf; static u_char path[NGX_MAX_PATH + 1]; dacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_dash_module); *ngx_snprintf(path, sizeof(path) - 1, "%V", &dacf->path) = 0; if (ngx_file_info(path, &fi) == NGX_FILE_ERROR) { if (ngx_errno != NGX_ENOENT) { ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno, "dash: " ngx_file_info_n " failed on '%V'", &dacf->path); return NGX_ERROR; } /* ENOENT */ if (ngx_create_dir(path, NGX_RTMP_DASH_DIR_ACCESS) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno, "dash: " ngx_create_dir_n " failed on '%V'", &dacf->path); return NGX_ERROR; } ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "dash: directory '%V' created", &dacf->path); } else { if (!ngx_is_dir(&fi)) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "dash: '%V' exists and is not a directory", &dacf->path); return NGX_ERROR; } ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "dash: directory '%V' exists", &dacf->path); } if (!dacf->nested) { return NGX_OK; } ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_dash_module); len = dacf->path.len; if (dacf->path.data[len - 1] == '/') { len--; } *ngx_snprintf(path, sizeof(path) - 1, "%*s/%V", len, dacf->path.data, &ctx->name) = 0; if (ngx_file_info(path, &fi) != NGX_FILE_ERROR) { if (ngx_is_dir(&fi)) { ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "dash: directory '%s' exists", path); return NGX_OK; } ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "dash: '%s' exists and is not a directory", path); return NGX_ERROR; } if (ngx_errno != NGX_ENOENT) { ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno, "dash: " ngx_file_info_n " failed on '%s'", path); return NGX_ERROR; } /* NGX_ENOENT */ if (ngx_create_dir(path, NGX_RTMP_DASH_DIR_ACCESS) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno, "dash: " ngx_create_dir_n " failed on '%s'", path); return NGX_ERROR; } ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "dash: directory '%s' created", path); return NGX_OK; }
static ngx_int_t ngx_http_store_plusplus_add_path(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out) { ngx_str_t value; size_t len; ngx_int_t rc; ngx_buf_t *b; u_char *p, *path_name; ngx_file_info_t fi; ngx_err_t err; rc = ngx_http_arg(r, (u_char *) STORE_VALUE_STR, sizeof(STORE_VALUE_STR) - 1, &value); if (rc != NGX_OK) { return rc; } b = ngx_create_temp_buf(r->pool, 1024); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (value.data[0] != '/') { b->last = ngx_sprintf(b->last, "ERROR: must be an absolute path." CRLF); rc = NGX_OK; goto done; } if (value.data[value.len - 1] == '/') { value.len--; } // check whether the store path is existent. len = value.len + 1 + sizeof(NGX_STORE_PLUSPLUS_TMP_PATH) - 1; path_name = ngx_pcalloc(r->pool, len + 1); if (path_name == NULL) { return NGX_ERROR; } p = ngx_cpymem(path_name, value.data, value.len); *p = '\0'; rc = ngx_file_info(path_name, &fi); if (rc == NGX_FILE_ERROR) { err = ngx_errno; b->last = ngx_strerror(err, b->last, b->end - b->last); *b->last++ = CR; *b->last++ = LF; rc = NGX_OK; goto done; } // create temp_path in this store path p = ngx_cpymem(path_name, value.data, value.len); *p++ = '/'; p = ngx_cpymem(p, NGX_STORE_PLUSPLUS_TMP_PATH, sizeof(NGX_STORE_PLUSPLUS_TMP_PATH) - 1); *p++ = '\0'; if (ngx_create_dir(path_name, ngx_dir_access(NGX_FILE_OWNER_ACCESS)) == NGX_FILE_ERROR) { err = ngx_errno; if (err != NGX_EEXIST) { b->last = ngx_strerror(err, b->last, b->end - b->last); *b->last++ = CR; *b->last++ = LF; rc = NGX_OK; goto done; } } rc = ngx_conhash_add_node(conhash, value.data, value.len, NULL); if (rc == NGX_OK) { b->last = ngx_sprintf(b->last, "Add node successfully!" CRLF); } if (rc == NGX_DECLINED) { b->last = ngx_sprintf(b->last, "The node already exists!" CRLF); rc = NGX_OK; } if (rc == NGX_AGAIN) { b->last = ngx_sprintf(b->last, "The conhash space is not enough!" CRLF); rc = NGX_OK; } done: b->last_buf = 1; out->buf = b; return rc; }