The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2009-2012 Broadcom Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | #include <stdio.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/statfs.h> |
| 24 | #include <sys/vfs.h> |
| 25 | #include <unistd.h> |
| 26 | #include <dirent.h> |
| 27 | #include <time.h> |
| 28 | #include <limits.h> |
| 29 | #include "gki.h" |
| 30 | #include "bta_fs_co.h" |
| 31 | #include "bta_fs_ci.h" |
| 32 | #include <inttypes.h> |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 33 | #include "bt_utils.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 34 | |
| 35 | #ifndef AID_SYSTEM |
| 36 | #define AID_SYSTEM 1000 |
| 37 | #define AID_BLUETOOTH 1002 |
| 38 | #define AID_SDCARD_RW 1015 |
| 39 | #define AID_MISC 9998 |
| 40 | #endif |
| 41 | |
| 42 | #define FAT_FS 0x4d44 |
| 43 | const unsigned short BT_UID= AID_BLUETOOTH; |
| 44 | const unsigned short BT_GID= AID_BLUETOOTH; |
| 45 | |
| 46 | /* enable additional debugging traces that should be compiled out by default! */ |
| 47 | #ifndef BTA_FS_DEBUG |
| 48 | #define BTA_FS_DEBUG TRUE |
| 49 | #define LOG_TAG "BTA_FS_CO" |
| 50 | #define LOGI(format, ...) fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__) |
| 51 | #endif |
| 52 | |
| 53 | #if (defined BTA_PBS_INCLUDED) && (BTA_PBS_INCLUDED == TRUE) |
| 54 | extern const tBTA_PBS_CFG bta_pbs_cfg; |
| 55 | #endif |
| 56 | |
| 57 | |
| 58 | |
| 59 | static int del_path (const char *path) |
| 60 | { |
| 61 | DIR *dir; |
| 62 | struct dirent *de; |
| 63 | int ret = 0; |
| 64 | char nameBuffer[PATH_MAX] = {0}; |
| 65 | struct stat statBuffer; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 66 | BTIF_TRACE_DEBUG("in del_path for path:%s", path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 67 | dir = opendir(path); |
| 68 | |
| 69 | if (dir == NULL) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 70 | BTIF_TRACE_DEBUG("opendir failed on path:%s", path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | char *filenameOffset; |
| 75 | |
| 76 | strncpy(nameBuffer, path, PATH_MAX - 1); |
| 77 | strcat(nameBuffer, "/"); |
| 78 | int nameLen = strlen(nameBuffer); |
| 79 | filenameOffset = nameBuffer + nameLen; |
| 80 | |
| 81 | for (;;) { |
| 82 | de = readdir(dir); |
| 83 | |
| 84 | if (de == NULL) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 85 | BTIF_TRACE_DEBUG("readdir failed for path:%s", path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 86 | //ret = -1; |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | if (0 == strcmp(de->d_name, ".") || 0 == strcmp(de->d_name, "..")) |
| 91 | continue; |
| 92 | |
| 93 | if((int)strlen(de->d_name) > PATH_MAX - nameLen) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 94 | BTIF_TRACE_DEBUG("d_name len:%d is too big", strlen(de->d_name)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 95 | ret = -1; |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | strcpy(filenameOffset, de->d_name); |
| 100 | |
| 101 | ret = lstat (nameBuffer, &statBuffer); |
| 102 | |
| 103 | if (ret != 0) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 104 | BTIF_TRACE_DEBUG("lstat failed for path:%s", nameBuffer); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 105 | break; |
| 106 | } |
| 107 | |
| 108 | if(S_ISDIR(statBuffer.st_mode)) { |
| 109 | |
| 110 | ret = del_path(nameBuffer); |
| 111 | if(ret != 0) |
| 112 | break; |
| 113 | } else { |
| 114 | ret = unlink(nameBuffer); |
| 115 | if (ret != 0) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 116 | BTIF_TRACE_DEBUG("unlink failed for path:%s", nameBuffer); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | closedir(dir); |
| 123 | if(ret == 0) { |
| 124 | ret = rmdir(path); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 125 | BTIF_TRACE_DEBUG("rmdir return:%d for path:%s", ret, path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return ret; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | inline int getAccess(int accType, struct stat *buffer, char *p_path) |
| 133 | { |
| 134 | |
| 135 | struct statfs fsbuffer; |
| 136 | int idType; |
| 137 | |
| 138 | if(! buffer) |
| 139 | return BTA_FS_CO_FAIL; |
| 140 | |
| 141 | //idType= (buffer->st_uid== BT_UID) ? 1 : (buffer->st_uid== BT_GID) ? 2 : 3; |
| 142 | if(buffer->st_uid == BT_UID) |
| 143 | idType = 1; |
| 144 | else if(buffer->st_gid == BT_GID || |
| 145 | buffer->st_gid == AID_SYSTEM || |
| 146 | buffer->st_gid == AID_MISC || |
| 147 | buffer->st_gid == AID_SDCARD_RW) |
| 148 | idType = 2; |
| 149 | else idType = 3; |
| 150 | |
| 151 | if(statfs(p_path, &fsbuffer)==0) |
| 152 | { |
| 153 | if(fsbuffer.f_type == FAT_FS) |
| 154 | return BTA_FS_CO_OK; |
| 155 | } |
| 156 | else { |
| 157 | return BTA_FS_CO_FAIL; |
| 158 | } |
| 159 | |
| 160 | switch(accType) { |
| 161 | case 4: |
| 162 | if(idType== 1) { //Id is User Id |
| 163 | if(buffer-> st_mode & S_IRUSR) |
| 164 | return BTA_FS_CO_OK; |
| 165 | } |
| 166 | else if(idType==2) { //Id is Group Id |
| 167 | if(buffer-> st_mode & S_IRGRP) |
| 168 | return BTA_FS_CO_OK; |
| 169 | } |
| 170 | else { //Id is Others |
| 171 | if(buffer-> st_mode & S_IROTH) |
| 172 | return BTA_FS_CO_OK; |
| 173 | } |
| 174 | break; |
| 175 | |
| 176 | case 6: |
| 177 | if(idType== 1) { //Id is User Id |
| 178 | if((buffer-> st_mode & S_IRUSR) && (buffer-> st_mode & S_IWUSR)) |
| 179 | return BTA_FS_CO_OK; |
| 180 | } |
| 181 | else if(idType==2) { //Id is Group Id |
| 182 | if((buffer-> st_mode & S_IRGRP) && (buffer-> st_mode & S_IWGRP)) |
| 183 | return BTA_FS_CO_OK; |
| 184 | } |
| 185 | else { //Id is Others |
| 186 | if((buffer-> st_mode & S_IROTH) && (buffer-> st_mode & S_IWOTH)) |
| 187 | return BTA_FS_CO_OK; |
| 188 | } |
| 189 | break; |
| 190 | |
| 191 | default: |
| 192 | return BTA_FS_CO_OK; |
| 193 | } |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 194 | BTIF_TRACE_DEBUG("*************FTP- Access Failed **********"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 195 | return BTA_FS_CO_EACCES; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /***************************************************************************** |
| 200 | ** Function Declarations |
| 201 | *****************************************************************************/ |
| 202 | |
| 203 | /******************************************************************************* |
| 204 | ** |
| 205 | ** Function bta_fs_convert_oflags |
| 206 | ** |
| 207 | ** Description This function converts the open flags from BTA into MFS. |
| 208 | ** |
| 209 | ** Returns BTA FS status value. |
| 210 | ** |
| 211 | *******************************************************************************/ |
| 212 | int bta_fs_convert_bta_oflags(int bta_oflags) |
| 213 | { |
| 214 | int oflags = 0; /* Initially read only */ |
| 215 | |
| 216 | /* Only one of these can be set: Read Only, Read/Write, or Write Only */ |
| 217 | if (bta_oflags & BTA_FS_O_RDWR) |
| 218 | oflags |= O_RDWR; |
| 219 | else if (bta_oflags & BTA_FS_O_WRONLY) |
| 220 | oflags |= O_WRONLY; |
| 221 | |
| 222 | /* OR in any other flags that are set by BTA */ |
| 223 | if (bta_oflags & BTA_FS_O_CREAT) |
| 224 | oflags |= O_CREAT; |
| 225 | |
| 226 | if (bta_oflags & BTA_FS_O_EXCL) |
| 227 | oflags |= O_EXCL; |
| 228 | |
| 229 | if (bta_oflags & BTA_FS_O_TRUNC) |
| 230 | oflags |= O_TRUNC; |
| 231 | |
| 232 | return (oflags); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | |
| 237 | /******************************************************************************* |
| 238 | ** |
| 239 | ** Function btapp_fs_check_space |
| 240 | ** |
| 241 | ** Description determines access and if there is enough space for given files size on given path |
| 242 | ** |
| 243 | ** Parameters p_path - Fully qualified path and file name. |
| 244 | ** WARNING: file name is stripped off! so it must be present! |
| 245 | ** size - size of file to put (0 if unavailable or not applicable) |
| 246 | ** app_id - in case application specific treatement is required (e.g opp versus ftp) |
| 247 | ** Returns 0 if enough space, otherwise errno failure codes |
| 248 | ** |
| 249 | *******************************************************************************/ |
| 250 | static int btapp_fs_check_space( const char *p_path, const UINT32 size, const UINT8 app_id ) |
| 251 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 252 | unsigned long long max_space; |
| 253 | struct statfs fs_buffer; |
| 254 | int err = 0; |
| 255 | char *p_dir; |
| 256 | char *p_end; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 257 | UNUSED(app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 258 | |
| 259 | if(size==BTA_FS_LEN_UNKNOWN) |
| 260 | return 0; |
| 261 | /* fail silently in case of no memory. write will catch if not enough space */ |
| 262 | |
| 263 | if (NULL != (p_dir = (char *) GKI_getbuf(strlen(p_path) + 1))) |
| 264 | { |
| 265 | strcpy(p_dir, p_path); |
| 266 | if (NULL != (p_end = strrchr(p_dir, '/'))) |
| 267 | { |
| 268 | |
| 269 | *p_end = '\0'; |
| 270 | /* get fs info and calculate available space. if not enough, the fs error EFBIG is returned */ |
| 271 | |
| 272 | if (0 == statfs(p_dir, &fs_buffer)) |
| 273 | { |
| 274 | |
| 275 | max_space = fs_buffer.f_bavail * fs_buffer.f_bsize; |
| 276 | #if (BTA_FS_DEBUG==TRUE) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 277 | BTIF_TRACE_DEBUG("btapp_fs_enough_space(file size: %d): (uint)max_size: %u", size, (UINT32)max_space); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 278 | #endif |
| 279 | if (max_space < size) |
| 280 | err = EFBIG; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | err = errno; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 285 | BTIF_TRACE_WARNING("btapp_fs_enough_space(): statfs() failed with err: %d", err); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | err = ENOENT; |
| 291 | } |
| 292 | GKI_freebuf(p_dir); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | err = ENOMEM; |
| 297 | } |
| 298 | return err; |
| 299 | |
| 300 | } /* btapp_fs_check_access_space() */ |
| 301 | |
| 302 | |
| 303 | /******************************************************************************* |
| 304 | ** |
| 305 | ** Function bta_fs_co_open |
| 306 | ** |
| 307 | ** Description This function is executed by BTA when a file is opened. |
| 308 | ** The phone uses this function to open |
| 309 | ** a file for reading or writing. |
| 310 | ** |
| 311 | ** Parameters p_path - Fully qualified path and file name. |
| 312 | ** oflags - permissions and mode (see constants above) |
| 313 | ** size - size of file to put (0 if unavailable or not applicable) |
| 314 | ** evt - event that must be passed into the call-in function. |
| 315 | ** app_id - application ID specified in the enable functions. |
| 316 | ** It can be used to identify which profile is the caller |
| 317 | ** of the call-out function. |
| 318 | ** |
| 319 | ** Returns void |
| 320 | ** |
| 321 | ** Note: Upon completion of the request, a file descriptor (int), |
| 322 | ** if successful, and an error code (tBTA_FS_CO_STATUS) |
| 323 | ** are returned in the call-in function, bta_fs_ci_open(). |
| 324 | ** |
| 325 | *******************************************************************************/ |
| 326 | |
| 327 | void bta_fs_co_open(const char *p_path, int oflags, UINT32 size, UINT16 evt, |
| 328 | UINT8 app_id) |
| 329 | { |
| 330 | |
| 331 | tBTA_FS_CO_STATUS status; |
| 332 | UINT32 file_size = 0; |
| 333 | struct stat file_stat; |
| 334 | int fd = -1; |
| 335 | int err = 0; |
| 336 | |
| 337 | /* Convert BTA oflags into os specific flags */ |
| 338 | oflags = bta_fs_convert_bta_oflags(oflags); |
| 339 | |
| 340 | /* check available space in case of write access. oflags are in OS format! */ |
| 341 | if (oflags & (O_RDWR|O_WRONLY)) |
| 342 | { |
| 343 | err = btapp_fs_check_space(p_path, size, app_id); |
| 344 | } |
| 345 | |
| 346 | if ( 0==err ) |
| 347 | { |
Sharvil Nanavati | 405b5c9 | 2016-06-17 14:15:46 -0700 | [diff] [blame] | 348 | if ((fd = TEMP_FAILURE_RETRY(open(p_path, oflags | O_NONBLOCK, 0666))) >= 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 349 | { |
| 350 | if (fstat(fd, &file_stat) == 0) |
| 351 | { |
| 352 | file_size = file_stat.st_size; |
| 353 | if (oflags & O_CREAT) |
| 354 | { |
| 355 | fchown(fd, BT_UID, BT_GID); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 356 | BTIF_TRACE_DEBUG("\n ******CHANGED OWNERSHIP SUCCESSFULLY**********"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | else |
| 362 | { |
| 363 | err = errno; |
| 364 | } |
| 365 | } |
| 366 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 367 | BTIF_TRACE_DEBUG("[CO] bta_fs_co_open: handle:%d err:%d, flags:%x, app id:%d", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 368 | fd, err, oflags, app_id); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 369 | BTIF_TRACE_DEBUG("file=%s", p_path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 370 | |
| 371 | /* convert fs error into bta_fs err. erro is set by first call to enough space to a valid value |
| 372 | * and needs only updating in case of error. This reports correct failure to remote obex! */ |
| 373 | |
| 374 | switch (err) |
| 375 | { |
| 376 | |
| 377 | case 0: |
| 378 | status = BTA_FS_CO_OK; |
| 379 | break; |
| 380 | case EACCES: |
| 381 | status = BTA_FS_CO_EACCES; |
| 382 | break; |
| 383 | case EFBIG: /* file to big for available fs space */ |
| 384 | status = BTA_FS_CO_ENOSPACE; |
| 385 | break; |
| 386 | default: |
| 387 | status = BTA_FS_CO_FAIL; |
| 388 | break; |
| 389 | } |
| 390 | bta_fs_ci_open(fd, status, file_size, evt); |
| 391 | } |
| 392 | |
| 393 | /******************************************************************************* |
| 394 | ** |
| 395 | ** Function bta_fs_co_close |
| 396 | ** |
| 397 | ** Description This function is called by BTA when a connection to a |
| 398 | ** client is closed. |
| 399 | ** |
| 400 | ** Parameters fd - file descriptor of file to close. |
| 401 | ** app_id - application ID specified in the enable functions. |
| 402 | ** It can be used to identify which profile is the caller |
| 403 | ** of the call-out function. |
| 404 | ** |
| 405 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 406 | ** [BTA_FS_CO_OK if successful], |
| 407 | ** [BTA_FS_CO_FAIL if failed ] |
| 408 | ** |
| 409 | *******************************************************************************/ |
| 410 | tBTA_FS_CO_STATUS bta_fs_co_close(int fd, UINT8 app_id) |
| 411 | { |
| 412 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
| 413 | int err; |
| 414 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 415 | BTIF_TRACE_DEBUG("[CO] bta_fs_co_close: handle:%d, app id:%d", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 416 | fd, app_id); |
| 417 | if (close (fd) < 0) |
| 418 | { |
| 419 | err = errno; |
| 420 | status = BTA_FS_CO_FAIL; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 421 | BTIF_TRACE_WARNING("[CO] bta_fs_co_close: handle:%d error=%d app_id:%d", fd, err, app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return (status); |
| 425 | } |
| 426 | |
| 427 | /******************************************************************************* |
| 428 | ** |
| 429 | ** Function bta_fs_co_read |
| 430 | ** |
| 431 | ** Description This function is called by BTA to read in data from the |
| 432 | ** previously opened file on the phone. |
| 433 | ** |
| 434 | ** Parameters fd - file descriptor of file to read from. |
| 435 | ** p_buf - buffer to read the data into. |
| 436 | ** nbytes - number of bytes to read into the buffer. |
| 437 | ** evt - event that must be passed into the call-in function. |
| 438 | ** ssn - session sequence number. Ignored, if bta_fs_co_open |
| 439 | ** was not called with BTA_FS_CO_RELIABLE. |
| 440 | ** app_id - application ID specified in the enable functions. |
| 441 | ** It can be used to identify which profile is the caller |
| 442 | ** of the call-out function. |
| 443 | ** |
| 444 | ** Returns void |
| 445 | ** |
| 446 | ** Note: Upon completion of the request, bta_fs_ci_read() is |
| 447 | ** called with the buffer of data, along with the number |
| 448 | ** of bytes read into the buffer, and a status. The |
| 449 | ** call-in function should only be called when ALL requested |
| 450 | ** bytes have been read, the end of file has been detected, |
| 451 | ** or an error has occurred. |
| 452 | ** |
| 453 | *******************************************************************************/ |
| 454 | void bta_fs_co_read(int fd, UINT8 *p_buf, UINT16 nbytes, UINT16 evt, UINT8 ssn, UINT8 app_id) |
| 455 | { |
| 456 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
| 457 | INT32 num_read; |
| 458 | int err; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 459 | UNUSED(ssn); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 460 | |
Sharvil Nanavati | 405b5c9 | 2016-06-17 14:15:46 -0700 | [diff] [blame] | 461 | if ((num_read = TEMP_FAILURE_RETRY(read (fd, p_buf, nbytes))) < 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 462 | { |
| 463 | err = errno; |
| 464 | status = BTA_FS_CO_FAIL; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 465 | BTIF_TRACE_WARNING("[CO] bta_fs_co_read: handle:%d error=%d app_id:%d", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 466 | fd, err, app_id); |
| 467 | } |
| 468 | else if (num_read < nbytes) |
| 469 | status = BTA_FS_CO_EOF; |
| 470 | |
| 471 | bta_fs_ci_read(fd, (UINT16)num_read, status, evt); |
| 472 | } |
| 473 | |
| 474 | /******************************************************************************* |
| 475 | ** |
| 476 | ** Function bta_fs_co_write |
| 477 | ** |
| 478 | ** Description This function is called by io to send file data to the |
| 479 | ** phone. |
| 480 | ** |
| 481 | ** Parameters fd - file descriptor of file to write to. |
| 482 | ** p_buf - buffer to read the data from. |
| 483 | ** nbytes - number of bytes to write out to the file. |
| 484 | ** evt - event that must be passed into the call-in function. |
| 485 | ** ssn - session sequence number. Ignored, if bta_fs_co_open |
| 486 | ** was not called with BTA_FS_CO_RELIABLE. |
| 487 | ** app_id - application ID specified in the enable functions. |
| 488 | ** It can be used to identify which profile is the caller |
| 489 | ** of the call-out function. |
| 490 | ** |
| 491 | ** Returns void |
| 492 | ** |
| 493 | ** Note: Upon completion of the request, bta_fs_ci_write() is |
| 494 | ** called with the file descriptor and the status. The |
| 495 | ** call-in function should only be called when ALL requested |
| 496 | ** bytes have been written, or an error has been detected, |
| 497 | ** |
| 498 | *******************************************************************************/ |
| 499 | void bta_fs_co_write(int fd, const UINT8 *p_buf, UINT16 nbytes, UINT16 evt, |
| 500 | UINT8 ssn, UINT8 app_id) |
| 501 | { |
| 502 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
| 503 | INT32 num_written; |
| 504 | int err=0; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 505 | UNUSED(ssn); |
| 506 | UNUSED(app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 507 | |
Sharvil Nanavati | 405b5c9 | 2016-06-17 14:15:46 -0700 | [diff] [blame] | 508 | if ((num_written = TEMP_FAILURE_RETRY(write (fd, p_buf, nbytes))) < 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 509 | { |
| 510 | err = errno; |
| 511 | status = BTA_FS_CO_FAIL; |
| 512 | } |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 513 | /* BTIF_TRACE_DEBUG("[CO] bta_fs_co_write: handle:%d error=%d, num_written:%d", fd, err, num_written);*/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 514 | |
| 515 | bta_fs_ci_write(fd, status, evt); |
| 516 | } |
| 517 | |
| 518 | /******************************************************************************* |
| 519 | ** |
| 520 | ** Function bta_fs_co_seek |
| 521 | ** |
| 522 | ** Description This function is called by io to move the file pointer |
| 523 | ** of a previously opened file to the specified location for |
| 524 | ** the next read or write operation. |
| 525 | ** |
| 526 | ** Parameters fd - file descriptor of file. |
| 527 | ** offset - Number of bytes from origin. |
| 528 | ** origin - Initial position. |
| 529 | ** |
| 530 | ** Returns void |
| 531 | ** |
| 532 | *******************************************************************************/ |
| 533 | void bta_fs_co_seek (int fd, INT32 offset, INT16 origin, UINT8 app_id) |
| 534 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 535 | UNUSED(app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 536 | lseek(fd, offset, origin); |
| 537 | } |
| 538 | |
| 539 | /******************************************************************************* |
| 540 | ** |
| 541 | ** Function bta_fs_co_access |
| 542 | ** |
| 543 | ** Description This function is called to check the existence of |
| 544 | ** a file or directory, and return whether or not it is a |
| 545 | ** directory or length of the file. |
| 546 | ** |
| 547 | ** Parameters p_path - (input) file or directory to access (fully qualified path). |
| 548 | ** mode - (input) [BTA_FS_ACC_EXIST, BTA_FS_ACC_READ, or BTA_FS_ACC_RDWR] |
| 549 | ** p_is_dir - (output) returns TRUE if p_path specifies a directory. |
| 550 | ** app_id - (input) application ID specified in the enable functions. |
| 551 | ** It can be used to identify which profile is the caller |
| 552 | ** of the call-out function. |
| 553 | ** |
| 554 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 555 | ** [BTA_FS_CO_OK if it exists] |
| 556 | ** [BTA_FS_CO_EACCES if permissions are wrong] |
| 557 | ** [BTA_FS_CO_FAIL if it does not exist] |
| 558 | ** |
| 559 | *******************************************************************************/ |
| 560 | tBTA_FS_CO_STATUS bta_fs_co_access(const char *p_path, int mode, BOOLEAN *p_is_dir, |
| 561 | UINT8 app_id) |
| 562 | { |
| 563 | int err; |
| 564 | int os_mode = 0; |
| 565 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
| 566 | struct stat buffer; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 567 | UNUSED(app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 568 | |
| 569 | #if (TRUE==BTA_FS_DEBUG) |
| 570 | LOGI("***********CHECKING ACCESS TO = %s", p_path); |
| 571 | #endif |
| 572 | |
| 573 | #if (defined BTA_PBS_INCLUDED) && (BTA_PBS_INCLUDED == TRUE) |
| 574 | |
| 575 | if (app_id == UI_PBS_ID) |
| 576 | { |
| 577 | |
| 578 | *p_is_dir = TRUE; |
| 579 | |
| 580 | #if (TRUE==BTA_FS_DEBUG) |
| 581 | LOGI("***********SUPPORTED REPO = %d", bta_pbs_cfg.supported_repositories); |
| 582 | #endif |
| 583 | //Check if SIM contact requested, and if so if it's supported. |
| 584 | //If not, return error! |
| 585 | if (strstr(p_path,"SIM1") && !(bta_pbs_cfg.supported_repositories & 0x2)) { |
| 586 | LOGI("***********RETURNING FAIL!"); |
| 587 | return BTA_FS_CO_FAIL; |
| 588 | } |
| 589 | |
| 590 | #if (TRUE==BTA_FS_DEBUG) |
| 591 | LOGI("***********RETURNING success!"); |
| 592 | #endif |
| 593 | return (status); |
| 594 | } |
| 595 | #endif |
| 596 | |
| 597 | |
| 598 | *p_is_dir = FALSE; |
| 599 | |
| 600 | if (mode == BTA_FS_ACC_RDWR) |
| 601 | os_mode = 6; |
| 602 | else if (mode == BTA_FS_ACC_READ) |
| 603 | os_mode = 4; |
| 604 | |
| 605 | if (stat(p_path, &buffer) == 0) |
| 606 | { |
| 607 | /* Determine if the object is a file or directory */ |
| 608 | if (S_ISDIR(buffer.st_mode)) |
| 609 | *p_is_dir = TRUE; |
| 610 | } |
| 611 | else |
| 612 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 613 | BTIF_TRACE_DEBUG("stat() failed! "); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 614 | return BTA_FS_CO_FAIL; |
| 615 | } |
| 616 | |
| 617 | status=getAccess (os_mode, &buffer, (char*)p_path); |
| 618 | return (status); |
| 619 | } |
| 620 | |
| 621 | /******************************************************************************* |
| 622 | ** |
| 623 | ** Function bta_fs_co_mkdir |
| 624 | ** |
| 625 | ** Description This function is called to create a directory with |
| 626 | ** the pathname given by path. The pathname is a null terminated |
| 627 | ** string. All components of the path must already exist. |
| 628 | ** |
| 629 | ** Parameters p_path - (input) name of directory to create (fully qualified path). |
| 630 | ** app_id - (input) application ID specified in the enable functions. |
| 631 | ** It can be used to identify which profile is the caller |
| 632 | ** of the call-out function. |
| 633 | ** |
| 634 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 635 | ** [BTA_FS_CO_OK if successful] |
| 636 | ** [BTA_FS_CO_FAIL if unsuccessful] |
| 637 | ** |
| 638 | *******************************************************************************/ |
| 639 | tBTA_FS_CO_STATUS bta_fs_co_mkdir(const char *p_path, UINT8 app_id) |
| 640 | { |
| 641 | int err; |
| 642 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
| 643 | |
| 644 | if ((mkdir (p_path, 0666)) != 0) |
| 645 | { |
| 646 | err = errno; |
| 647 | status = BTA_FS_CO_FAIL; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 648 | BTIF_TRACE_WARNING("[CO] bta_fs_co_mkdir: error=%d, path [%s] app_id:%d", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 649 | err, p_path, app_id); |
| 650 | } |
| 651 | return (status); |
| 652 | } |
| 653 | |
| 654 | /******************************************************************************* |
| 655 | ** |
| 656 | ** Function bta_fs_co_rmdir |
| 657 | ** |
| 658 | ** Description This function is called to remove a directory whose |
| 659 | ** name is given by path. The directory must be empty. |
| 660 | ** |
| 661 | ** Parameters p_path - (input) name of directory to remove (fully qualified path). |
| 662 | ** app_id - (input) application ID specified in the enable functions. |
| 663 | ** It can be used to identify which profile is the caller |
| 664 | ** of the call-out function. |
| 665 | ** |
| 666 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 667 | ** [BTA_FS_CO_OK if successful] |
| 668 | ** [BTA_FS_CO_EACCES if read-only] |
| 669 | ** [BTA_FS_CO_ENOTEMPTY if directory is not empty] |
| 670 | ** [BTA_FS_CO_FAIL otherwise] |
| 671 | ** |
| 672 | *******************************************************************************/ |
| 673 | tBTA_FS_CO_STATUS bta_fs_co_rmdir(const char *p_path, UINT8 app_id) |
| 674 | { |
| 675 | int err, path_len; |
| 676 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
| 677 | struct stat buffer; |
| 678 | char *dirName, *tmp = NULL; |
| 679 | |
| 680 | path_len = strlen( p_path )+1; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 681 | BTIF_TRACE_DEBUG( "bta_fs_co_rmdir( app_id: %d ): path_len: %d", app_id, path_len ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 682 | #if (TRUE==BTA_FS_DEBUG) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 683 | BTIF_TRACE_DEBUG( "bta_fs_co_rmdir():path_len: %d, p_path", app_id ); |
| 684 | BTIF_TRACE_DEBUG( p_path ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 685 | #endif |
| 686 | |
| 687 | /* allocate a temp buffer for path with 0 char. make sure not to crash if path is too big! */ |
| 688 | dirName = (char*) calloc(1, path_len+1); |
| 689 | if ( NULL != dirName ) |
| 690 | { |
| 691 | strcpy( dirName, p_path ); |
| 692 | } |
| 693 | else |
| 694 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 695 | BTIF_TRACE_WARNING( "bta_fs_co_rmdir( app_id: %d ) for path_len: %d::out of memory", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 696 | app_id, path_len ); |
| 697 | return BTA_FS_CO_FAIL; |
| 698 | } |
| 699 | |
| 700 | if (NULL!= (tmp = strrchr(dirName, '/'))) |
| 701 | { |
| 702 | *tmp = '\0'; |
| 703 | } |
| 704 | if (stat(dirName, &buffer) == 0) |
| 705 | { |
| 706 | status = getAccess(6, &buffer, dirName); |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | free(dirName); |
| 711 | #if (TRUE==BTA_FS_DEBUG) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 712 | BTIF_TRACE_WARNING( "bta_fs_co_rmdir()::stat(dirName) failed" ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 713 | #endif |
| 714 | return BTA_FS_CO_FAIL; |
| 715 | } |
| 716 | |
| 717 | free(dirName); |
| 718 | if (status != BTA_FS_CO_OK) |
| 719 | { |
| 720 | #if (TRUE==BTA_FS_DEBUG) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 721 | BTIF_TRACE_WARNING( "bta_fs_co_rmdir()::getAccess(dirName) FAILED"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 722 | #endif |
| 723 | return status; |
| 724 | } |
| 725 | |
| 726 | if (stat(p_path, &buffer) == 0) |
| 727 | { |
| 728 | status = getAccess(6, &buffer, (char*)p_path); |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | #if (TRUE==BTA_FS_DEBUG) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 733 | BTIF_TRACE_WARNING( "bta_fs_co_rmdir()::stat(p_path) FAILED"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 734 | #endif |
| 735 | return BTA_FS_CO_FAIL; |
| 736 | } |
| 737 | |
| 738 | if (status != BTA_FS_CO_OK) |
| 739 | { |
| 740 | #if (TRUE==BTA_FS_DEBUG) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 741 | BTIF_TRACE_DEBUG( "bta_fs_co_rmdir()::getAccess(p_path) FAILED"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 742 | #endif |
| 743 | return status; |
| 744 | } |
| 745 | //if ((rmdir (p_path)) != 0) |
| 746 | if (del_path(p_path) != 0) |
| 747 | { |
| 748 | err = errno; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 749 | BTIF_TRACE_WARNING( "bta_fs_co_rmdir():rmdir/del_path FAILED with err: %d", err ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 750 | if (err == EACCES) |
| 751 | status = BTA_FS_CO_EACCES; |
| 752 | else if (err == ENOTEMPTY) |
| 753 | status = BTA_FS_CO_ENOTEMPTY; |
| 754 | else |
| 755 | status = BTA_FS_CO_FAIL; |
| 756 | } |
| 757 | return (status); |
| 758 | } |
| 759 | |
| 760 | /******************************************************************************* |
| 761 | ** |
| 762 | ** Function bta_fs_co_unlink |
| 763 | ** |
| 764 | ** Description This function is called to remove a file whose name |
| 765 | ** is given by p_path. |
| 766 | ** |
| 767 | ** Parameters p_path - (input) name of file to remove (fully qualified path). |
| 768 | ** app_id - (input) application ID specified in the enable functions. |
| 769 | ** It can be used to identify which profile is the caller |
| 770 | ** of the call-out function. |
| 771 | ** |
| 772 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 773 | ** [BTA_FS_CO_OK if successful] |
| 774 | ** [BTA_FS_CO_EACCES if read-only] |
| 775 | ** [BTA_FS_CO_FAIL otherwise] |
| 776 | ** |
| 777 | *******************************************************************************/ |
| 778 | tBTA_FS_CO_STATUS bta_fs_co_unlink(const char *p_path, UINT8 app_id) |
| 779 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 780 | BTIF_TRACE_DEBUG("bta_fs_co_unlink"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 781 | int err; |
| 782 | tBTA_FS_CO_STATUS status = BTA_FS_CO_OK; |
Sai Aitharaju | b006449 | 2014-11-12 18:15:28 +0530 | [diff] [blame] | 783 | char *dirName=NULL, *tmp=NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 784 | struct stat buffer; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 785 | UNUSED(app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 786 | |
| 787 | if(! p_path) |
| 788 | return BTA_FS_CO_FAIL; |
| 789 | |
| 790 | /* buffer needs to be NULL terminated - so add one more byte to be zero'd out */ |
| 791 | #if 0 |
| 792 | dirName= (char*) calloc(1, strlen(p_path)); /* <--- this can cause problems */ |
| 793 | #else |
| 794 | dirName= (char*) calloc(1, strlen(p_path) + 1); |
| 795 | #endif |
Sai Aitharaju | b006449 | 2014-11-12 18:15:28 +0530 | [diff] [blame] | 796 | if(dirName) |
| 797 | strncpy(dirName, p_path, strlen(p_path)); |
| 798 | else |
| 799 | { |
| 800 | BTIF_TRACE_DEBUG("%s(): failed to allocate memory", __FUNCTION__); |
| 801 | return BTA_FS_CO_FAIL; |
| 802 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 803 | if((tmp=strrchr(dirName, '/'))) |
| 804 | { |
| 805 | *tmp='\0'; |
| 806 | } |
| 807 | if (stat(dirName, &buffer) == 0) |
| 808 | { |
| 809 | status=getAccess (6, &buffer, dirName); |
| 810 | free(dirName); |
| 811 | } |
| 812 | else |
| 813 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 814 | BTIF_TRACE_DEBUG("stat() failed! "); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 815 | free(dirName); |
| 816 | return BTA_FS_CO_FAIL; |
| 817 | } |
| 818 | |
| 819 | if(status!= BTA_FS_CO_OK) |
| 820 | return status; |
| 821 | |
| 822 | if ((unlink (p_path)) != 0) |
| 823 | { |
| 824 | err = errno; |
| 825 | if (err == EACCES) |
| 826 | status = BTA_FS_CO_EACCES; |
| 827 | else |
| 828 | status = BTA_FS_CO_FAIL; |
| 829 | } |
| 830 | return (status); |
| 831 | |
| 832 | } |
| 833 | |
| 834 | /******************************************************************************* |
| 835 | ** |
| 836 | ** Function bta_fs_co_getdirentry |
| 837 | ** |
| 838 | ** Description This function is called to get a directory entry for the |
| 839 | ** specified p_path. The first/next directory should be filled |
| 840 | ** into the location specified by p_entry. |
| 841 | ** |
| 842 | ** Parameters p_path - directory to search (Fully qualified path) |
| 843 | ** first_item - TRUE if first search, FALSE if next search |
| 844 | ** (p_cur contains previous) |
| 845 | ** p_entry (input/output) - Points to last entry data (valid when |
| 846 | ** first_item is FALSE) |
| 847 | ** evt - event that must be passed into the call-in function. |
| 848 | ** app_id - application ID specified in the enable functions. |
| 849 | ** It can be used to identify which profile is the caller |
| 850 | ** of the call-out function. |
| 851 | ** |
| 852 | ** Returns void |
| 853 | ** |
| 854 | ** Note: Upon completion of the request, the status is passed |
| 855 | ** in the bta_fs_ci_direntry() call-in function. |
| 856 | ** BTA_FS_CO_OK is returned when p_entry is valid, |
| 857 | ** BTA_FS_CO_EODIR is returned when no more entries [finished] |
| 858 | ** BTA_FS_CO_FAIL is returned if an error occurred |
| 859 | ** |
| 860 | *******************************************************************************/ |
| 861 | void bta_fs_co_getdirentry(const char *p_path, BOOLEAN first_item, |
| 862 | tBTA_FS_DIRENTRY *p_entry, UINT16 evt, UINT8 app_id) |
| 863 | { |
| 864 | tBTA_FS_CO_STATUS co_status = BTA_FS_CO_FAIL; |
| 865 | int status = -1; /* '0' - success, '-1' - fail */ |
| 866 | struct tm *p_tm; |
| 867 | DIR *dir; |
| 868 | struct dirent *dirent; |
| 869 | struct stat buf; |
| 870 | char fullname[500]; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 871 | UNUSED(app_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 872 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 873 | BTIF_TRACE_DEBUG("Entered bta_fs_co_getdirentry"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 874 | |
| 875 | /* First item is to be retrieved */ |
| 876 | if (first_item) |
| 877 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 878 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: path = %s", p_path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 879 | |
| 880 | dir = opendir(p_path); |
| 881 | if(dir == NULL) |
| 882 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 883 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: dir is NULL so error out with errno=%d", errno); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 884 | co_status = BTA_FS_CO_EODIR; |
| 885 | bta_fs_ci_direntry(co_status, evt); |
| 886 | return; |
| 887 | } |
| 888 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 889 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: dir = %p", dir); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 890 | if((dirent = readdir(dir)) != NULL) |
| 891 | { |
| 892 | p_entry->refdata = (UINT32) dir; /* Save this for future searches */ |
| 893 | status = 0; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 894 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: dirent = %p", dirent); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 895 | } |
| 896 | else |
| 897 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 898 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: dirent = %p", dirent); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 899 | /* Close the search if there are no more items */ |
| 900 | closedir( (DIR*) p_entry->refdata); |
| 901 | co_status = BTA_FS_CO_EODIR; |
| 902 | } |
| 903 | } |
| 904 | else /* Get the next entry based on the p_ref data from previous search */ |
| 905 | { |
| 906 | if ((dirent = readdir((DIR*)p_entry->refdata)) == NULL) |
| 907 | { |
| 908 | /* Close the search if there are no more items */ |
| 909 | closedir( (DIR*) p_entry->refdata); |
| 910 | co_status = BTA_FS_CO_EODIR; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 911 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: dirent = %p", dirent); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 912 | } |
| 913 | else |
| 914 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 915 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: dirent = %p", dirent); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 916 | status = 0; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (status == 0) |
| 921 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 922 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: status = 0"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 923 | |
| 924 | sprintf(fullname, "%s/%s", p_path, dirent->d_name); |
| 925 | |
| 926 | /* Load new values into the return structure (refdata is left untouched) */ |
| 927 | if (stat(fullname, &buf) == 0) { |
| 928 | p_entry->filesize = buf.st_size; |
| 929 | p_entry->mode = 0; /* Default is normal read/write file access */ |
| 930 | |
| 931 | if (S_ISDIR(buf.st_mode)) |
| 932 | p_entry->mode |= BTA_FS_A_DIR; |
| 933 | else |
| 934 | p_entry->mode |= BTA_FS_A_RDONLY; |
| 935 | |
| 936 | strcpy(p_entry->p_name, dirent->d_name); |
| 937 | #if 0 |
| 938 | fprintf(stderr, "bta_fs_co_getdirentry(): %s %9d %d\n", |
| 939 | dirent->d_name, |
| 940 | buf.st_size, |
| 941 | p_entry->mode); |
| 942 | #endif |
| 943 | p_tm = localtime((const time_t*)&buf.st_mtime); |
| 944 | if (p_tm != NULL) |
| 945 | { |
Sai Aitharaju | b006449 | 2014-11-12 18:15:28 +0530 | [diff] [blame] | 946 | snprintf(p_entry->crtime, sizeof(p_entry->crtime), "%04d%02d%02dT%02d%02d%02dZ", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 947 | p_tm->tm_year + 1900, /* Base Year ISO 6201 */ |
| 948 | p_tm->tm_mon + 1, /* month starts at 0 */ |
| 949 | p_tm->tm_mday, |
| 950 | p_tm->tm_hour, |
| 951 | p_tm->tm_min, |
| 952 | p_tm->tm_sec); |
| 953 | } |
| 954 | else |
| 955 | p_entry->crtime[0] = '\0'; /* No valid time */ |
| 956 | #if 0 |
| 957 | fprintf(stderr, "bta_fs_co_getdirentry(): %s %9d %d %s\n", |
| 958 | dirent->d_name, |
| 959 | p_entry->filesize, |
| 960 | p_entry->mode, |
| 961 | p_entry->crtime); |
| 962 | #endif |
| 963 | co_status = BTA_FS_CO_OK; |
| 964 | } else { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 965 | BTIF_TRACE_WARNING("stat() failed! "); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 966 | co_status = BTA_FS_CO_EACCES; |
| 967 | } |
| 968 | } |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 969 | BTIF_TRACE_DEBUG("bta_fs_co_getdirentry: calling bta_fs_ci_getdirentry"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 970 | |
| 971 | bta_fs_ci_direntry(co_status, evt); |
| 972 | } |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | /******************************************************************************* |
| 978 | ** |
| 979 | ** Function bta_fs_co_setdir |
| 980 | ** |
| 981 | ** Description This function is executed by BTA when the server changes the |
| 982 | ** local path |
| 983 | ** |
| 984 | ** Parameters p_path - the new path. |
| 985 | ** app_id - application ID specified in the enable functions. |
| 986 | ** It can be used to identify which profile is the caller |
| 987 | ** of the call-out function. |
| 988 | ** |
| 989 | ** Returns void |
| 990 | ** |
| 991 | *******************************************************************************/ |
| 992 | void bta_fs_co_setdir(const char *p_path, UINT8 app_id) |
| 993 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 994 | UNUSED(p_path); |
| 995 | UNUSED(app_id); |
| 996 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 997 | BTIF_TRACE_DEBUG("Entered %s. New path: %s", __FUNCTION__, p_path); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | /******************************************************************************* |
| 1001 | ** OBEX14 Reliable Session not supported. Stub associated callouts. |
| 1002 | ******************************************************************************/ |
| 1003 | |
| 1004 | /******************************************************************************* |
| 1005 | ** |
| 1006 | ** Function bta_fs_co_resume |
| 1007 | ** |
| 1008 | ** Description This function is executed by BTA when resuming a session. |
| 1009 | ** This is used to retrieve the session ID and related information |
| 1010 | ** |
| 1011 | ** Parameters evt - event that must be passed into the call-in function. |
| 1012 | ** app_id - application ID specified in the enable functions. |
| 1013 | ** It can be used to identify which profile is the caller |
| 1014 | ** of the call-out function. |
| 1015 | ** |
| 1016 | ** Returns void |
| 1017 | ** |
| 1018 | ** Note: Upon completion of the request, the related session information, |
| 1019 | ** if successful, and an error code (tBTA_FS_CO_STATUS) |
| 1020 | ** are returned in the call-in function, bta_fs_ci_resume(). |
| 1021 | ** |
| 1022 | *******************************************************************************/ |
| 1023 | void bta_fs_co_resume(UINT16 evt, UINT8 app_id) |
| 1024 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1025 | UNUSED(evt); |
| 1026 | UNUSED(app_id); |
| 1027 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1028 | BTIF_TRACE_WARNING("[CO] bta_fs_co_resume - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /******************************************************************************* |
| 1032 | ** |
| 1033 | ** Function bta_fs_co_set_perms |
| 1034 | ** |
| 1035 | ** Description This function is called to set the permission a file/directory |
| 1036 | ** with name as p_src_path. |
| 1037 | ** |
| 1038 | ** Parameters p_src_path - (input) name of file/directory to set permission (fully qualified path). |
| 1039 | ** p_perms - the permission . |
| 1040 | ** app_id - (input) application ID specified in the enable functions. |
| 1041 | ** It can be used to identify which profile is the caller |
| 1042 | ** of the call-out function. |
| 1043 | ** |
| 1044 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 1045 | ** [BTA_FS_CO_OK if successful] |
| 1046 | ** [BTA_FS_CO_EACCES if p_dest_path already exists or could not be created (invalid path); |
| 1047 | ** or p_src_path is a directory and p_dest_path specifies a different path. ] |
| 1048 | ** [BTA_FS_CO_FAIL otherwise] |
| 1049 | ** |
| 1050 | *******************************************************************************/ |
| 1051 | void bta_fs_co_set_perms(const char *p_src_path, UINT8 *p_perms, UINT16 evt, UINT8 app_id) |
| 1052 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1053 | UNUSED(p_src_path); |
| 1054 | UNUSED(p_perms); |
| 1055 | UNUSED(evt); |
| 1056 | UNUSED(app_id); |
| 1057 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1058 | BTIF_TRACE_WARNING("[CO] bta_fs_co_set_perms - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /******************************************************************************* |
| 1062 | ** |
| 1063 | ** Function bta_fs_co_rename |
| 1064 | ** |
| 1065 | ** Description This function is called to move a file/directory whose |
| 1066 | ** name is given by p_src_path to p_dest_path. |
| 1067 | ** |
| 1068 | ** Parameters p_src_path - (input) name of file/directory to be moved (fully qualified path). |
| 1069 | ** p_dest_path - (input) new name of file/directory(fully qualified path). |
| 1070 | ** p_perms - the permission of the new object. |
| 1071 | ** app_id - (input) application ID specified in the enable functions. |
| 1072 | ** It can be used to identify which profile is the caller |
| 1073 | ** of the call-out function. |
| 1074 | ** |
| 1075 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 1076 | ** [BTA_FS_CO_OK if successful] |
| 1077 | ** [BTA_FS_CO_EACCES if p_dest_path already exists or could not be created (invalid path); |
| 1078 | ** or p_src_path is a directory and p_dest_path specifies a different path. ] |
| 1079 | ** [BTA_FS_CO_FAIL otherwise] |
| 1080 | ** |
| 1081 | *******************************************************************************/ |
| 1082 | void bta_fs_co_rename(const char *p_src_path, const char *p_dest_path, UINT8 *p_perms, UINT16 evt, UINT8 app_id) |
| 1083 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1084 | UNUSED(p_src_path); |
| 1085 | UNUSED(p_dest_path); |
| 1086 | UNUSED(p_perms); |
| 1087 | UNUSED(evt); |
| 1088 | UNUSED(app_id); |
| 1089 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1090 | BTIF_TRACE_WARNING("[CO] bta_fs_co_rename - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /******************************************************************************* |
| 1094 | ** |
| 1095 | ** Function bta_fs_co_copy |
| 1096 | ** |
| 1097 | ** Description This function is called to copy a file/directory whose |
| 1098 | ** name is given by p_src_path to p_dest_path. |
| 1099 | ** |
| 1100 | ** Parameters p_src_path - (input) name of file/directory to be copied (fully qualified path). |
| 1101 | ** p_dest_path - (input) new name of file/directory(fully qualified path). |
| 1102 | ** p_perms - the permission of the new object. |
| 1103 | ** evt - event that must be passed into the call-in function. |
| 1104 | ** app_id - (input) application ID specified in the enable functions. |
| 1105 | ** It can be used to identify which profile is the caller |
| 1106 | ** of the call-out function. |
| 1107 | ** |
| 1108 | ** Returns (tBTA_FS_CO_STATUS) status of the call. |
| 1109 | ** [BTA_FS_CO_OK if successful] |
| 1110 | ** [BTA_FS_CO_EIS_DIR if p_src_path is a folder] |
| 1111 | ** [BTA_FS_CO_EACCES if p_dest_path already exists or could not be created (invalid path); |
| 1112 | ** or p_src_path is a directory and p_dest_path specifies a different path. ] |
| 1113 | ** [BTA_FS_CO_FAIL otherwise] |
| 1114 | ** |
| 1115 | *******************************************************************************/ |
| 1116 | void bta_fs_co_copy(const char *p_src_path, const char *p_dest_path, UINT8 *p_perms, UINT16 evt, UINT8 app_id) |
| 1117 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1118 | UNUSED(p_src_path); |
| 1119 | UNUSED(p_dest_path); |
| 1120 | UNUSED(p_perms); |
| 1121 | UNUSED(evt); |
| 1122 | UNUSED(app_id); |
| 1123 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1124 | BTIF_TRACE_WARNING("[CO] bta_fs_co_copy - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /******************************************************************************* |
| 1128 | ** |
| 1129 | ** Function bta_fs_co_resume_op |
| 1130 | ** |
| 1131 | ** Description This function is executed by BTA when a reliable session is |
| 1132 | ** resumed and there was an interrupted operation. |
| 1133 | ** |
| 1134 | ** Parameters offset - the session ID and related information. |
| 1135 | ** evt - event that must be passed into the call-in function. |
| 1136 | ** app_id - application ID specified in the enable functions. |
| 1137 | ** It can be used to identify which profile is the caller |
| 1138 | ** of the call-out function. |
| 1139 | ** |
| 1140 | ** Returns void |
| 1141 | ** |
| 1142 | *******************************************************************************/ |
| 1143 | void bta_fs_co_resume_op(UINT32 offset, UINT16 evt, UINT8 app_id) |
| 1144 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1145 | UNUSED(offset); |
| 1146 | UNUSED(evt); |
| 1147 | UNUSED(app_id); |
| 1148 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1149 | BTIF_TRACE_WARNING("[CO] bta_fs_co_resume_op - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | /******************************************************************************* |
| 1154 | ** |
| 1155 | ** Function bta_fs_co_session_info |
| 1156 | ** |
| 1157 | ** Description This function is executed by BTA when a reliable session is |
| 1158 | ** established (p_sess_info != NULL) or ended (p_sess_info == NULL). |
| 1159 | ** |
| 1160 | ** Parameters bd_addr - the peer address |
| 1161 | ** p_sess_info - the session ID and related information. |
| 1162 | ** app_id - application ID specified in the enable functions. |
| 1163 | ** It can be used to identify which profile is the caller |
| 1164 | ** of the call-out function. |
| 1165 | ** |
| 1166 | ** Returns void |
| 1167 | ** |
| 1168 | *******************************************************************************/ |
| 1169 | void bta_fs_co_session_info(BD_ADDR bd_addr, UINT8 *p_sess_info, UINT8 ssn, |
| 1170 | tBTA_FS_CO_SESS_ST new_st, char *p_path, UINT8 *p_info, UINT8 app_id) |
| 1171 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1172 | UNUSED(bd_addr); |
| 1173 | UNUSED(p_sess_info); |
| 1174 | UNUSED(ssn); |
| 1175 | UNUSED(new_st); |
| 1176 | UNUSED(p_path); |
| 1177 | UNUSED(p_info); |
| 1178 | UNUSED(app_id); |
| 1179 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1180 | BTIF_TRACE_WARNING("[CO] bta_fs_co_session_info - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | /******************************************************************************* |
| 1185 | ** |
| 1186 | ** Function bta_fs_co_suspend |
| 1187 | ** |
| 1188 | ** Description This function is executed by BTA when a reliable session is |
| 1189 | ** suspended. |
| 1190 | ** |
| 1191 | ** Parameters bd_addr - the peer address |
| 1192 | ** ssn - the session sequence number. |
| 1193 | ** info - the BTA specific information (like last active operation). |
| 1194 | ** p_offset- the location to receive object offset of the suspended session |
| 1195 | ** app_id - application ID specified in the enable functions. |
| 1196 | ** It can be used to identify which profile is the caller |
| 1197 | ** of the call-out function. |
| 1198 | ** |
| 1199 | ** Returns void |
| 1200 | ** |
| 1201 | *******************************************************************************/ |
| 1202 | void bta_fs_co_suspend(BD_ADDR bd_addr, UINT8 *p_sess_info, UINT8 ssn, |
| 1203 | UINT32 *p_timeout, UINT32 *p_offset, UINT8 info, UINT8 app_id) |
| 1204 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1205 | UNUSED(bd_addr); |
| 1206 | UNUSED(p_sess_info); |
| 1207 | UNUSED(ssn); |
| 1208 | UNUSED(p_timeout); |
| 1209 | UNUSED(p_offset); |
| 1210 | UNUSED(info); |
| 1211 | UNUSED(app_id); |
| 1212 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1213 | BTIF_TRACE_WARNING("[CO] bta_fs_co_suspend - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /******************************************************************************* |
| 1217 | ** |
| 1218 | ** Function bta_fs_co_sess_ssn |
| 1219 | ** |
| 1220 | ** Description This function is executed by BTA when resuming a session. |
| 1221 | ** This is used to inform call-out module if the ssn/file offset |
| 1222 | ** needs to be adjusted. |
| 1223 | ** |
| 1224 | ** Parameters ssn - the session sequence number of the first request |
| 1225 | ** after resume. |
| 1226 | ** app_id - application ID specified in the enable functions. |
| 1227 | ** It can be used to identify which profile is the caller |
| 1228 | ** of the call-out function. |
| 1229 | ** |
| 1230 | ** Returns void |
| 1231 | ** |
| 1232 | *******************************************************************************/ |
| 1233 | void bta_fs_co_sess_ssn(int fd, UINT8 ssn, UINT8 app_id) |
| 1234 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1235 | UNUSED(fd); |
| 1236 | UNUSED(ssn); |
| 1237 | UNUSED(app_id); |
| 1238 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1239 | BTIF_TRACE_WARNING("[CO] bta_fs_co_suspend - NOT implemented"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |