Ignore:
Timestamp:
Jul 8, 2014, 12:15:12 AM (8 years ago)
Author:
jordan
Message:

(trunk, libT) #4160 'foreign character support' -- merge mike.dld's 4160-03a-file.platch, which introduces tr_sys_file_*() portability wrappers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/file-test.c

    r14314 r14321  
    138138}
    139139
     140static bool
     141validate_permissions (const char   * path,
     142                      unsigned int   permissions)
     143{
     144#ifndef _WIN32
     145
     146  struct stat sb;
     147  return stat (path, &sb) != -1 && (sb.st_mode & 0777) == permissions;
     148
     149#else
     150
     151  /* No UNIX permissions on Windows */
     152  return true;
     153
     154#endif
     155}
     156
    140157static int
    141158test_get_info (void)
     
    143160  char * const test_dir = create_test_dir (__FUNCTION__);
    144161  tr_sys_path_info info;
     162  tr_sys_file_t fd;
    145163  tr_error * err = NULL;
    146164  char * path1, * path2;
     
    163181  check_int_eq (4, info.size);
    164182  check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
     183
     184  /* Good file info (by handle) */
     185  fd = tr_sys_file_open (path1, TR_SYS_FILE_READ, 0, NULL);
     186  clear_path_info (&info);
     187  check (tr_sys_file_get_info (fd, &info, &err));
     188  check (err == NULL);
     189  check_int_eq (TR_SYS_PATH_IS_FILE, info.type);
     190  check_int_eq (4, info.size);
     191  check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
     192  tr_sys_file_close (fd, NULL);
    165193
    166194  tr_sys_path_remove (path1, NULL);
     
    192220      check_int_eq (4, info.size);
    193221      check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
     222
     223      /* Good file info (by handle) */
     224      fd = tr_sys_file_open (path1, TR_SYS_FILE_READ, 0, NULL);
     225      clear_path_info (&info);
     226      check (tr_sys_file_get_info (fd, &info, &err));
     227      check (err == NULL);
     228      check_int_eq (TR_SYS_PATH_IS_FILE, info.type);
     229      check_int_eq (4, info.size);
     230      check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
     231      tr_sys_file_close (fd, NULL);
    194232
    195233      tr_sys_path_remove (path2, NULL);
     
    776814}
    777815
     816static int
     817test_file_open (void)
     818{
     819  char * const test_dir = create_test_dir (__FUNCTION__);
     820  tr_error * err = NULL;
     821  char * path1;
     822  tr_sys_file_t fd;
     823  uint64_t n;
     824  tr_sys_path_info info;
     825
     826  path1 = tr_buildPath (test_dir, "a", NULL);
     827
     828  /* Can't open non-existent file */
     829  check (!tr_sys_path_exists (path1, NULL));
     830  check (tr_sys_file_open (path1, TR_SYS_FILE_READ, 0600, &err) == TR_BAD_SYS_FILE);
     831  check (err != NULL);
     832  check (!tr_sys_path_exists (path1, NULL));
     833  tr_error_clear (&err);
     834  check (tr_sys_file_open (path1, TR_SYS_FILE_WRITE, 0600, &err) == TR_BAD_SYS_FILE);
     835  check (err != NULL);
     836  check (!tr_sys_path_exists (path1, NULL));
     837  tr_error_clear (&err);
     838
     839  /* Can't open directory */
     840  tr_mkdirp (path1, 0777);
     841#ifdef _WIN32
     842  /* This works on *NIX */
     843  check (tr_sys_file_open (path1, TR_SYS_FILE_READ, 0600, &err) == TR_BAD_SYS_FILE);
     844  check (err != NULL);
     845  tr_error_clear (&err);
     846#endif
     847  check (tr_sys_file_open (path1, TR_SYS_FILE_WRITE, 0600, &err) == TR_BAD_SYS_FILE);
     848  check (err != NULL);
     849  tr_error_clear (&err);
     850
     851  tr_sys_path_remove (path1, NULL);
     852
     853  /* Can create non-existent file */
     854  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0640, &err);
     855  check (fd != TR_BAD_SYS_FILE);
     856  check (err == NULL);
     857  tr_sys_file_close (fd, NULL);
     858  check (tr_sys_path_exists (path1, NULL));
     859  check (validate_permissions (path1, 0640));
     860
     861  /* Can open existing file */
     862  check (tr_sys_path_exists (path1, NULL));
     863  fd = tr_sys_file_open (path1, TR_SYS_FILE_READ, 0600, &err);
     864  check (fd != TR_BAD_SYS_FILE);
     865  check (err == NULL);
     866  tr_sys_file_close (fd, NULL);
     867  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE, 0600, &err);
     868  check (fd != TR_BAD_SYS_FILE);
     869  check (err == NULL);
     870  tr_sys_file_close (fd, NULL);
     871
     872  tr_sys_path_remove (path1, NULL);
     873  libtest_create_file_with_string_contents (path1, "test");
     874
     875  /* Can't create new file if it already exists */
     876  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE_NEW, 0640, &err);
     877  check (fd == TR_BAD_SYS_FILE);
     878  check (err != NULL);
     879  tr_error_clear (&err);
     880  tr_sys_path_get_info (path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
     881  check_int_eq (4, info.size);
     882
     883  /* Pointer is at the end of file */
     884  tr_sys_path_get_info (path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
     885  check_int_eq (4, info.size);
     886  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_APPEND, 0600, &err);
     887  check (fd != TR_BAD_SYS_FILE);
     888  check (err == NULL);
     889  tr_sys_file_write (fd, "s", 1, NULL, NULL); /* On *NIX, pointer is positioned on each write but not initially */
     890  tr_sys_file_seek (fd, 0, TR_SEEK_CUR, &n, NULL);
     891  check_int_eq (5, n);
     892  tr_sys_file_close (fd, NULL);
     893
     894  /* File gets truncated */
     895  tr_sys_path_get_info (path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
     896  check_int_eq (5, info.size);
     897  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_TRUNCATE, 0600, &err);
     898  check (fd != TR_BAD_SYS_FILE);
     899  check (err == NULL);
     900  tr_sys_file_get_info (fd, &info, NULL);
     901  check_int_eq (0, info.size);
     902  tr_sys_file_close (fd, NULL);
     903  tr_sys_path_get_info (path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
     904  check_int_eq (0, info.size);
     905
     906  /* TODO: symlink and hardlink tests */
     907
     908  tr_sys_path_remove (path1, NULL);
     909
     910  tr_free (path1);
     911
     912  tr_free (test_dir);
     913  return 0;
     914}
     915
     916static int
     917test_file_read_write_seek (void)
     918{
     919  char * const test_dir = create_test_dir (__FUNCTION__);
     920  tr_error * err = NULL;
     921  char * path1;
     922  tr_sys_file_t fd;
     923  uint64_t n;
     924  char buf[100];
     925
     926  path1 = tr_buildPath (test_dir, "a", NULL);
     927
     928  fd = tr_sys_file_open (path1, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
     929
     930  check (tr_sys_file_seek (fd, 0, TR_SEEK_CUR, &n, &err));
     931  check (err == NULL);
     932  check_int_eq (0, n);
     933
     934  check (tr_sys_file_write (fd, "test", 4, &n, &err));
     935  check (err == NULL);
     936  check_int_eq (4, n);
     937
     938  check (tr_sys_file_seek (fd, 0, TR_SEEK_CUR, &n, &err));
     939  check (err == NULL);
     940  check_int_eq (4, n);
     941
     942  check (tr_sys_file_seek (fd, 0, TR_SEEK_SET, &n, &err));
     943  check (err == NULL);
     944  check_int_eq (0, n);
     945
     946  check (tr_sys_file_read (fd, buf, sizeof (buf), &n, &err));
     947  check (err == NULL);
     948  check_int_eq (4, n);
     949
     950  check_int_eq (0, memcmp (buf, "test", 4));
     951
     952  check (tr_sys_file_seek (fd, -3, TR_SEEK_CUR, &n, &err));
     953  check (err == NULL);
     954  check_int_eq (1, n);
     955
     956  check (tr_sys_file_write (fd, "E", 1, &n, &err));
     957  check (err == NULL);
     958  check_int_eq (1, n);
     959
     960  check (tr_sys_file_seek (fd, -2, TR_SEEK_CUR, &n, &err));
     961  check (err == NULL);
     962  check_int_eq (0, n);
     963
     964  check (tr_sys_file_read (fd, buf, sizeof (buf), &n, &err));
     965  check (err == NULL);
     966  check_int_eq (4, n);
     967
     968  check_int_eq (0, memcmp (buf, "tEst", 4));
     969
     970  check (tr_sys_file_seek (fd, 0, TR_SEEK_END, &n, &err));
     971  check (err == NULL);
     972  check_int_eq (4, n);
     973
     974  check (tr_sys_file_write (fd, " ok", 3, &n, &err));
     975  check (err == NULL);
     976  check_int_eq (3, n);
     977
     978  check (tr_sys_file_seek (fd, 0, TR_SEEK_SET, &n, &err));
     979  check (err == NULL);
     980  check_int_eq (0, n);
     981
     982  check (tr_sys_file_read (fd, buf, sizeof (buf), &n, &err));
     983  check (err == NULL);
     984  check_int_eq (7, n);
     985
     986  check_int_eq (0, memcmp (buf, "tEst ok", 7));
     987
     988  check (tr_sys_file_write_at (fd, "-", 1, 4, &n, &err));
     989  check (err == NULL);
     990  check_int_eq (1, n);
     991
     992  check (tr_sys_file_read_at (fd, buf, 5, 2, &n, &err));
     993  check (err == NULL);
     994  check_int_eq (5, n);
     995
     996  check_int_eq (0, memcmp (buf, "st-ok", 5));
     997
     998  tr_sys_file_close (fd, NULL);
     999
     1000  tr_sys_path_remove (path1, NULL);
     1001
     1002  tr_free (path1);
     1003
     1004  tr_free (test_dir);
     1005  return 0;
     1006}
     1007
     1008static int
     1009test_file_truncate (void)
     1010{
     1011  char * const test_dir = create_test_dir (__FUNCTION__);
     1012  tr_error * err = NULL;
     1013  char * path1;
     1014  tr_sys_file_t fd;
     1015  tr_sys_path_info info;
     1016
     1017  path1 = tr_buildPath (test_dir, "a", NULL);
     1018
     1019  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
     1020
     1021  check (tr_sys_file_truncate (fd, 10, &err));
     1022  check (err == NULL);
     1023  tr_sys_file_get_info (fd, &info, NULL);
     1024  check_int_eq (10, info.size);
     1025
     1026  check (tr_sys_file_truncate (fd, 20, &err));
     1027  check (err == NULL);
     1028  tr_sys_file_get_info (fd, &info, NULL);
     1029  check_int_eq (20, info.size);
     1030
     1031  check (tr_sys_file_truncate (fd, 0, &err));
     1032  check (err == NULL);
     1033  tr_sys_file_get_info (fd, &info, NULL);
     1034  check_int_eq (0, info.size);
     1035
     1036  check (tr_sys_file_truncate (fd, 50, &err));
     1037  check (err == NULL);
     1038
     1039  tr_sys_file_close (fd, NULL);
     1040
     1041  tr_sys_path_get_info (path1, 0, &info, NULL);
     1042  check_int_eq (50, info.size);
     1043
     1044  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
     1045
     1046  check (tr_sys_file_truncate (fd, 25, &err));
     1047  check (err == NULL);
     1048
     1049  tr_sys_file_close (fd, NULL);
     1050
     1051  tr_sys_path_get_info (path1, 0, &info, NULL);
     1052  check_int_eq (25, info.size);
     1053
     1054  tr_sys_path_remove (path1, NULL);
     1055
     1056  tr_free (path1);
     1057
     1058  tr_free (test_dir);
     1059  return 0;
     1060}
     1061
     1062static int
     1063test_file_preallocate (void)
     1064{
     1065  char * const test_dir = create_test_dir (__FUNCTION__);
     1066  tr_error * err = NULL;
     1067  char * path1;
     1068  tr_sys_file_t fd;
     1069  tr_sys_path_info info;
     1070
     1071  path1 = tr_buildPath (test_dir, "a", NULL);
     1072
     1073  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
     1074
     1075  if (tr_sys_file_preallocate (fd, 50, 0, &err))
     1076    {
     1077      check (err == NULL);
     1078      tr_sys_file_get_info (fd, &info, NULL);
     1079      check_int_eq (50, info.size);
     1080    }
     1081  else
     1082    {
     1083      check (err != NULL);
     1084      fprintf (stderr, "WARNING: [%s] unable to preallocate file (full): %s (%d)\n", __FUNCTION__, err->message, err->code);
     1085      tr_error_clear (&err);
     1086    }
     1087
     1088  tr_sys_file_close (fd, NULL);
     1089
     1090  tr_sys_path_remove (path1, NULL);
     1091
     1092  fd = tr_sys_file_open (path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
     1093
     1094  if (tr_sys_file_preallocate (fd, 500 * 1024 * 1024, TR_SYS_FILE_PREALLOC_SPARSE, &err))
     1095    {
     1096      check (err == NULL);
     1097      tr_sys_file_get_info (fd, &info, NULL);
     1098      check_int_eq (500 * 1024 * 1024, info.size);
     1099    }
     1100  else
     1101    {
     1102      check (err != NULL);
     1103      fprintf (stderr, "WARNING: [%s] unable to preallocate file (sparse): %s (%d)\n", __FUNCTION__, err->message, err->code);
     1104      tr_error_clear (&err);
     1105    }
     1106
     1107  tr_sys_file_close (fd, NULL);
     1108
     1109  tr_sys_path_remove (path1, NULL);
     1110
     1111  tr_free (path1);
     1112
     1113  tr_free (test_dir);
     1114  return 0;
     1115}
     1116
     1117static int
     1118test_file_map (void)
     1119{
     1120  char * const test_dir = create_test_dir (__FUNCTION__);
     1121  tr_error * err = NULL;
     1122  char * path1;
     1123  tr_sys_file_t fd;
     1124  char * view;
     1125
     1126  path1 = tr_buildPath (test_dir, "a", NULL);
     1127
     1128  libtest_create_file_with_string_contents (path1, "test");
     1129
     1130  fd = tr_sys_file_open (path1, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, 0600, NULL);
     1131
     1132  view = tr_sys_file_map_for_reading (fd, 0, 4, &err);
     1133  check (view != NULL);
     1134  check (err == NULL);
     1135
     1136  check_int_eq (0, memcmp (view, "test", 4));
     1137
     1138  tr_sys_file_write_at (fd, "E", 1, 1, NULL, NULL);
     1139
     1140  check_int_eq (0, memcmp (view, "tEst", 4));
     1141
     1142  check (tr_sys_file_unmap (view, 4, &err));
     1143  check (err == NULL);
     1144
     1145  tr_sys_file_close (fd, NULL);
     1146
     1147  tr_sys_path_remove (path1, NULL);
     1148
     1149  tr_free (path1);
     1150
     1151  tr_free (test_dir);
     1152  return 0;
     1153}
     1154
    7781155int
    7791156main (void)
     
    7871164      test_path_basename_dirname,
    7881165      test_path_rename,
    789       test_path_remove
     1166      test_path_remove,
     1167      test_file_open,
     1168      test_file_read_write_seek,
     1169      test_file_truncate,
     1170      test_file_preallocate,
     1171      test_file_map
    7901172    };
    7911173  int ret;
Note: See TracChangeset for help on using the changeset viewer.