cat.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /*
  18. A a stripped down version of unix's "cat".
  19. Doesn't deal with any flags for now, will just attempt to read the whole file.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "hdfspp/hdfs_ext.h"
  25. #include "uriparser2/uriparser2.h"
  26. #include "common/util_c.h"
  27. #define SCHEME "hdfs"
  28. #define BUF_SIZE 1048576 //1 MB
  29. static char input_buffer[BUF_SIZE];
  30. int main(int argc, char** argv) {
  31. char error_text[1024];
  32. if (argc != 2) {
  33. fprintf(stderr, "usage: cat [hdfs://[<hostname>:<port>]]/<path-to-file>\n");
  34. return 1;
  35. }
  36. URI * uri = NULL;
  37. const char * uri_path = argv[1];
  38. //Separate check for scheme is required, otherwise uriparser2.h library causes memory issues under valgrind
  39. const char * scheme_end = strstr(uri_path, "://");
  40. if (scheme_end) {
  41. if (strncmp(uri_path, SCHEME, strlen(SCHEME)) != 0) {
  42. fprintf(stderr, "Scheme %.*s:// is not supported.\n", (int) (scheme_end - uri_path), uri_path);
  43. return 1;
  44. } else {
  45. uri = uri_parse(uri_path);
  46. }
  47. }
  48. if (!uri) {
  49. fprintf(stderr, "Malformed URI: %s\n", uri_path);
  50. return 1;
  51. }
  52. struct hdfsBuilder* builder = hdfsNewBuilder();
  53. if (uri->host)
  54. hdfsBuilderSetNameNode(builder, uri->host);
  55. if (uri->port != 0)
  56. hdfsBuilderSetNameNodePort(builder, uri->port);
  57. hdfsFS fs = hdfsBuilderConnect(builder);
  58. if (fs == NULL) {
  59. hdfsGetLastError(error_text, sizeof(error_text));
  60. const char * host = uri->host ? uri->host : "<default>";
  61. int port = uri->port;
  62. if (port == 0)
  63. port = 8020;
  64. fprintf(stderr, "Unable to connect to %s:%d, hdfsConnect returned null.\n%s\n",
  65. host, port, error_text);
  66. return 1;
  67. }
  68. hdfsFile file = hdfsOpenFile(fs, uri->path, 0, 0, 0, 0);
  69. if (NULL == file) {
  70. hdfsGetLastError(error_text, sizeof(error_text));
  71. fprintf(stderr, "Unable to open file %s: %s\n", uri->path, error_text );
  72. hdfsDisconnect(fs);
  73. hdfsFreeBuilder(builder);
  74. return 1;
  75. }
  76. ssize_t read_bytes_count = 0;
  77. ssize_t last_read_bytes = 0;
  78. while (0 < (last_read_bytes =
  79. hdfsPread(fs, file, read_bytes_count, input_buffer, sizeof(input_buffer)))) {
  80. fwrite(input_buffer, last_read_bytes, 1, stdout);
  81. read_bytes_count += last_read_bytes;
  82. }
  83. int res = 0;
  84. res = hdfsCloseFile(fs, file);
  85. if (0 != res) {
  86. hdfsGetLastError(error_text, sizeof(error_text));
  87. fprintf(stderr, "Error closing file: %s\n", error_text);
  88. hdfsDisconnect(fs);
  89. hdfsFreeBuilder(builder);
  90. return 1;
  91. }
  92. res = hdfsDisconnect(fs);
  93. if (0 != res) {
  94. hdfsGetLastError(error_text, sizeof(error_text));
  95. fprintf(stderr, "Error disconnecting filesystem: %s", error_text);
  96. hdfsFreeBuilder(builder);
  97. return 1;
  98. }
  99. hdfsFreeBuilder(builder);
  100. free(uri);
  101. // Clean up static data and prevent valgrind memory leaks
  102. ShutdownProtobufLibrary_C();
  103. return 0;
  104. }