Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

bookmark.c remove some global buffers

refactor to allow removing some of the static buffers

Change-Id: Ia3ff6ea28f35634fd8c31b023431ad53bd542085

+117 -65
+112 -65
apps/bookmark.c
··· 78 78 int speed; 79 79 } bm; 80 80 81 - #define TEMP_BUF_SIZE (MAX_PATH + 1) 81 + /* Temp buffer used for reading and filename creation */ 82 + #define TEMP_BUF_SIZE (MAX(MAX_BOOKMARK_SIZE, MAX_PATH + 1)) 82 83 static char global_temp_buffer[TEMP_BUF_SIZE]; 83 - /* File name created by generate_bookmark_file_name */ 84 - static char global_bookmark_file_name[MAX_PATH]; 85 - static char global_read_buffer[MAX_BOOKMARK_SIZE]; 84 + 86 85 /* Bookmark created by create_bookmark*/ 87 86 static char global_bookmark[MAX_BOOKMARK_SIZE]; 88 - /* Filename from parsed bookmark (can be made local where needed) */ 89 - static char global_filename[MAX_PATH]; 90 87 91 88 static const char* skip_token(const char* s) 92 89 { ··· 138 135 /* the filename tokens are to be extracted. */ 139 136 /* Returns true on successful bookmark parse. */ 140 137 /* ----------------------------------------------------------------------- */ 141 - static bool parse_bookmark(const char *bookmark, 138 + static bool parse_bookmark(char *filenamebuf, 139 + size_t filenamebufsz, 140 + const char *bookmark, 142 141 const bool parse_filenames, 143 142 const bool strip_dir) 144 143 { ··· 201 200 end++; 202 201 } 203 202 } 204 - strmemccpy(global_filename, end, MAX_PATH); 203 + if(filenamebuf) 204 + strmemccpy(filenamebuf, end, filenamebufsz); 205 205 } 206 206 } 207 207 208 208 return true; 209 209 } 210 210 211 + /* ------------------------------------------------------------------------- */ 212 + /* This function takes a filename and appends .tmp. This function also opens */ 213 + /* the resulting file based on oflags, filename will be in buf on return */ 214 + /* Returns file descriptor */ 215 + /* --------------------------------------------------------------------------*/ 216 + static int open_temp_bookmark(char *buf, 217 + size_t bufsz, 218 + int oflags, 219 + const char* filename) 220 + { 221 + /* Opening up a temp bookmark file */ 222 + return open_pathfmt(buf, bufsz, oflags, "%s.tmp", filename); 223 + } 224 + 211 225 /* ----------------------------------------------------------------------- */ 212 226 /* This function adds a bookmark to a file. */ 213 227 /* Returns true on successful bookmark add. */ ··· 228 242 bool equal; 229 243 230 244 /* Opening up a temp bookmark file */ 231 - temp_bookmark_file = open_pathfmt(global_temp_buffer, sizeof(global_temp_buffer), 232 - O_WRONLY | O_CREAT | O_TRUNC, "%s.tmp", bookmark_file_name); 245 + temp_bookmark_file = open_temp_bookmark(global_temp_buffer, 246 + sizeof(global_temp_buffer), 247 + O_WRONLY | O_CREAT | O_TRUNC, 248 + bookmark_file_name); 249 + 233 250 if (temp_bookmark_file < 0) 234 251 return false; /* can't open the temp file */ 235 252 ··· 254 271 bookmark_file = open(bookmark_file_name, O_RDONLY); 255 272 if (bookmark_file >= 0) 256 273 { 257 - while (read_line(bookmark_file, global_read_buffer, 258 - sizeof(global_read_buffer)) > 0) 274 + while (read_line(bookmark_file, global_temp_buffer, 275 + sizeof(global_temp_buffer)) > 0) 259 276 { 260 277 /* The MRB has a max of MAX_BOOKMARKS in it */ 261 278 /* This keeps it from getting too large */ 262 279 if (most_recent && (bookmark_count >= MAX_BOOKMARKS)) 263 280 break; 264 281 265 - if (!parse_bookmark(global_read_buffer, false, false)) 282 + if (!parse_bookmark(NULL, 0, global_temp_buffer, false, false)) 266 283 break; 267 284 268 285 equal = false; 269 286 if (comp_playlist) 270 287 { 271 - if (get_playlist_and_track(global_read_buffer, &bm_pl_start, 288 + if (get_playlist_and_track(global_temp_buffer, &bm_pl_start, 272 289 &bm_pl_end, &bm_track)) 273 290 { 274 291 bm_pl_len = bm_pl_end - bm_pl_start; 275 - equal = (pl_len == bm_pl_len) && !strncmp(pl_start, bm_pl_start, pl_len); 292 + equal = (pl_len == bm_pl_len) && 293 + !strncmp(pl_start, bm_pl_start, pl_len); 294 + 276 295 if (equal && comp_track) 277 296 equal = !strcmp(track, bm_track); 278 297 } ··· 280 299 if (!equal) 281 300 { 282 301 bookmark_count++; 283 - write(temp_bookmark_file, global_read_buffer, 284 - strlen(global_read_buffer)); 302 + write(temp_bookmark_file, global_temp_buffer, 303 + strlen(global_temp_buffer)); 285 304 write(temp_bookmark_file, "\n", 1); 286 305 } 287 306 } ··· 289 308 } 290 309 close(temp_bookmark_file); 291 310 311 + /* only retrieve the path*/ 312 + open_temp_bookmark(global_temp_buffer, 313 + sizeof(global_temp_buffer), 314 + O_PATH, 315 + bookmark_file_name); 292 316 remove(bookmark_file_name); 293 317 rename(global_temp_buffer, bookmark_file_name); 294 318 ··· 303 327 /* could be placed. */ 304 328 /* Returns true if the file name is generated, false if it was too long */ 305 329 /* ----------------------------------------------------------------------- */ 306 - static bool generate_bookmark_file_name(const char *in) 330 + static bool generate_bookmark_file_name(char *filenamebuf, 331 + size_t filenamebufsz, 332 + const char *bmarknamein) 307 333 { 308 334 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */ 309 - /* otherwise, name it based on the in variable */ 310 - if (!strcmp("/", in)) 311 - strcpy(global_bookmark_file_name, "/root_dir.bmark"); 335 + /* otherwise, name it based on the bmarknamein variable */ 336 + if (!strcmp("/", bmarknamein)) 337 + strmemccpy(filenamebuf, "/root_dir.bmark", filenamebufsz); 312 338 else 313 339 { 314 340 #ifdef HAVE_MULTIVOLUME 315 341 /* The "root" of an extra volume need special handling too. */ 316 342 const char *filename; 317 - path_strip_volume(in, &filename, true); 343 + path_strip_volume(bmarknamein, &filename, true); 318 344 bool volume_root = *filename == '\0'; 319 345 #endif 320 - size_t len = strlcpy(global_bookmark_file_name, in, MAX_PATH); 321 - if(len >= MAX_PATH) 346 + size_t len = strlcpy(filenamebuf, bmarknamein, filenamebufsz); 347 + if(len >= filenamebufsz) 322 348 return false; 323 349 324 - if(global_bookmark_file_name[len-1] == '/') { 325 - global_bookmark_file_name[len-1] = '\0'; 350 + if(filenamebuf[len-1] == '/') { 351 + filenamebuf[len-1] = '\0'; 326 352 len--; 327 353 } 328 354 329 355 #ifdef HAVE_MULTIVOLUME 330 356 if (volume_root) 331 - len = strlcat(global_bookmark_file_name, "/volume_dir.bmark", MAX_PATH); 357 + len = strlcat(filenamebuf, "/volume_dir.bmark", filenamebufsz); 332 358 else 333 359 #endif 334 - len = strlcat(global_bookmark_file_name, ".bmark", MAX_PATH); 360 + len = strlcat(filenamebuf, ".bmark", filenamebufsz); 335 361 336 - if(len >= MAX_PATH) 362 + if(len >= filenamebufsz) 337 363 return false; 338 364 } 339 365 ··· 348 374 /* resume_file*milliseconds*MP3 Title* */ 349 375 /* Returns true on successful bookmark write. */ 350 376 /* Returns false if any part of the bookmarking process fails. It is */ 351 - /* possible that a bookmark is successfully added to the most recent */ 377 + /* possible that a bookmark is successfully added to the most recent */ 352 378 /* bookmark list but fails to be added to the bookmark file or vice versa. */ 353 379 /* ------------------------------------------------------------------------*/ 354 380 static bool write_bookmark(bool create_bookmark_file, const char *bookmark) 355 381 { 382 + char bm_filename[MAX_PATH]; 356 383 bool ret=true; 357 384 358 385 if (!bookmark) ··· 370 397 { 371 398 char* name = playlist_get_name(NULL, global_temp_buffer, 372 399 sizeof(global_temp_buffer)); 373 - if (generate_bookmark_file_name(name)) 400 + 401 + if (generate_bookmark_file_name(bm_filename, 402 + sizeof(bm_filename), name)) 374 403 { 375 - ret = ret & add_bookmark(global_bookmark_file_name, bookmark, false); 404 + ret = ret & add_bookmark(bm_filename, bookmark, false); 376 405 } 377 406 else 378 407 { ··· 395 424 if(file < 0) 396 425 return -1; 397 426 398 - while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0) 427 + while(read_line(file, global_temp_buffer, sizeof(global_temp_buffer)) > 0) 399 428 { 400 429 read_count++; 401 430 } ··· 426 455 bookmarks->count = 0; 427 456 bookmarks->reload = false; 428 457 429 - while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0) 458 + while(read_line(file, global_temp_buffer, sizeof(global_temp_buffer)) > 0) 430 459 { 431 460 read_count++; 432 461 433 462 if (read_count >= first_line) 434 463 { 435 - dest -= strlen(global_read_buffer) + 1; 464 + dest -= strlen(global_temp_buffer) + 1; 436 465 437 466 if (dest < ((char*) bookmarks) + sizeof(*bookmarks) 438 467 + (sizeof(char*) * (bookmarks->count + 1))) ··· 440 469 break; 441 470 } 442 471 443 - strcpy(dest, global_read_buffer); 472 + strcpy(dest, global_temp_buffer); 444 473 bookmarks->items[bookmarks->count] = dest; 445 474 bookmarks->count++; 446 475 } ··· 455 484 char *buffer, 456 485 size_t buffer_len) 457 486 { 487 + char fnamebuf[MAX_PATH]; 458 488 struct bookmark_list* bookmarks = (struct bookmark_list*) data; 459 489 int index = list_index / 2; 460 490 ··· 510 540 } 511 541 } 512 542 513 - if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true, true)) 543 + if (!parse_bookmark(fnamebuf, sizeof(fnamebuf), 544 + bookmarks->items[index - bookmarks->start], true, true)) 514 545 { 515 546 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " "; 516 547 } ··· 544 575 } 545 576 else 546 577 { 547 - name = global_filename; 578 + name = fnamebuf; 548 579 format = "%s"; 549 580 } 550 581 551 - strrsplt(global_filename, '.'); 552 - snprintf(buffer, buffer_len, format, name, global_filename); 582 + strrsplt(fnamebuf, '.'); 583 + snprintf(buffer, buffer_len, format, name, fnamebuf); 553 584 return buffer; 554 585 } 555 586 else ··· 570 601 int bookmark_id, 571 602 bool show_playlist_name) 572 603 { 573 - if (!parse_bookmark(bookmark, true, false)) 604 + char fnamebuf[MAX_PATH]; 605 + if (!parse_bookmark(fnamebuf, sizeof(fnamebuf), bookmark, true, false)) 574 606 { 575 607 talk_id(LANG_BOOKMARK_INVALID, false); 576 608 return; ··· 604 636 /* Track filename */ 605 637 if(!is_dir) 606 638 global_temp_buffer[0] = 0; 607 - talk_file_or_spell(global_temp_buffer, global_filename, 639 + talk_file_or_spell(global_temp_buffer, fnamebuf, 608 640 TALK_IDARRAY(VOICE_FILE), true); 609 641 } 610 642 ··· 636 668 int bookmark_count = 0; 637 669 638 670 /* Opening up a temp bookmark file */ 639 - temp_bookmark_file = open_pathfmt(global_temp_buffer, sizeof(global_temp_buffer), 640 - O_WRONLY | O_CREAT | O_TRUNC, "%s.tmp", bookmark_file_name); 671 + temp_bookmark_file = open_temp_bookmark(global_temp_buffer, 672 + sizeof(global_temp_buffer), 673 + O_WRONLY | O_CREAT | O_TRUNC, 674 + bookmark_file_name); 641 675 642 676 if (temp_bookmark_file < 0) 643 677 return false; /* can't open the temp file */ ··· 646 680 bookmark_file = open(bookmark_file_name, O_RDONLY); 647 681 if (bookmark_file >= 0) 648 682 { 649 - while (read_line(bookmark_file, global_read_buffer, 650 - sizeof(global_read_buffer)) > 0) 683 + while (read_line(bookmark_file, global_temp_buffer, 684 + sizeof(global_temp_buffer)) > 0) 651 685 { 652 686 if (bookmark_id != bookmark_count) 653 687 { 654 - write(temp_bookmark_file, global_read_buffer, 655 - strlen(global_read_buffer)); 688 + write(temp_bookmark_file, global_temp_buffer, 689 + strlen(global_temp_buffer)); 656 690 write(temp_bookmark_file, "\n", 1); 657 691 } 658 692 bookmark_count++; ··· 661 695 } 662 696 close(temp_bookmark_file); 663 697 698 + /* only retrieve the path*/ 699 + open_temp_bookmark(global_temp_buffer, 700 + sizeof(global_temp_buffer), 701 + O_PATH, 702 + bookmark_file_name); 703 + 664 704 remove(bookmark_file_name); 665 705 rename(global_temp_buffer, bookmark_file_name); 666 706 ··· 695 735 bookmarks->filename = bookmark_file_name; 696 736 bookmarks->start = 0; 697 737 bookmarks->show_playlist_name 698 - = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0; 699 - gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL); 738 + = (strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0); 739 + 740 + gui_synclist_init(&list, &get_bookmark_info, 741 + (void*) bookmarks, false, 2, NULL); 742 + 700 743 if(global_settings.talk_menu) 701 744 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb); 702 745 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK), ··· 831 874 /* ------------------------------------------------------------------------*/ 832 875 static bool play_bookmark(const char* bookmark) 833 876 { 877 + char fnamebuf[MAX_PATH]; 834 878 #if defined(HAVE_PITCHCONTROL) 835 879 /* preset pitch and speed to 100% in case bookmark doesn't have info */ 836 880 bm.pitch = sound_get_pitch(); 837 881 bm.speed = dsp_get_timestretch(); 838 882 #endif 839 883 840 - if (parse_bookmark(bookmark, true, true)) 884 + if (parse_bookmark(fnamebuf, sizeof(fnamebuf), bookmark, true, true)) 841 885 { 842 886 global_settings.repeat_mode = bm.repeat_mode; 843 887 global_settings.playlist_shuffle = bm.shuffle; ··· 848 892 if (!warn_on_pl_erase()) 849 893 return false; 850 894 return bookmark_play(global_temp_buffer, bm.resume_index, 851 - bm.resume_time, bm.resume_offset, bm.resume_seed, global_filename); 895 + bm.resume_time, bm.resume_offset, bm.resume_seed, fnamebuf); 852 896 } 853 897 854 898 return false; ··· 924 968 file); 925 969 926 970 /* checking to see if the bookmark is valid */ 927 - if (parse_bookmark(global_bookmark, false, false)) 971 + if (parse_bookmark(NULL, 0, global_bookmark, false, false)) 928 972 return global_bookmark; 929 973 else 930 974 return NULL; ··· 958 1002 /* ----------------------------------------------------------------------- */ 959 1003 int bookmark_load_menu(void) 960 1004 { 1005 + char bm_filename[MAX_PATH]; 961 1006 char* bookmark; 962 1007 int ret = BOOKMARK_FAIL; 963 1008 ··· 965 1010 966 1011 char* name = playlist_get_name(NULL, global_temp_buffer, 967 1012 sizeof(global_temp_buffer)); 968 - if (generate_bookmark_file_name(name)) 1013 + if (generate_bookmark_file_name(bm_filename, sizeof(bm_filename), name)) 969 1014 { 970 - ret = select_bookmark(global_bookmark_file_name, false, &bookmark); 1015 + ret = select_bookmark(bm_filename, false, &bookmark); 971 1016 if (bookmark != NULL) 972 1017 { 973 1018 ret = play_bookmark(bookmark) ? BOOKMARK_SUCCESS : BOOKMARK_FAIL; ··· 1053 1098 /* ------------------------------------------------------------------------*/ 1054 1099 int bookmark_autoload(const char* file) 1055 1100 { 1101 + char bm_filename[MAX_PATH]; 1056 1102 char* bookmark; 1057 1103 1058 1104 if(global_settings.autoloadbookmark == BOOKMARK_NO) 1059 1105 return BOOKMARK_DONT_RESUME; 1060 1106 1061 1107 /*Checking to see if a bookmark file exists.*/ 1062 - if(!generate_bookmark_file_name(file)) 1108 + if(!generate_bookmark_file_name(bm_filename, sizeof(bm_filename), file)) 1063 1109 { 1064 1110 return BOOKMARK_DONT_RESUME; 1065 1111 } 1066 1112 1067 - if(!file_exists(global_bookmark_file_name)) 1113 + if(!file_exists(bm_filename)) 1068 1114 return BOOKMARK_DONT_RESUME; 1069 1115 1070 1116 if(global_settings.autoloadbookmark == BOOKMARK_YES) 1071 1117 { 1072 - return bookmark_load(global_bookmark_file_name, true) ? BOOKMARK_DO_RESUME : 1073 - BOOKMARK_DONT_RESUME; 1118 + return (bookmark_load(bm_filename, true) 1119 + ? BOOKMARK_DO_RESUME : BOOKMARK_DONT_RESUME); 1074 1120 } 1075 1121 else 1076 1122 { 1077 - int ret = select_bookmark(global_bookmark_file_name, true, &bookmark); 1123 + int ret = select_bookmark(bm_filename, true, &bookmark); 1078 1124 1079 1125 if (bookmark != NULL) 1080 1126 { ··· 1090 1136 return BOOKMARK_DO_RESUME; 1091 1137 } 1092 1138 1093 - return ret != BOOKMARK_SUCCESS ? BOOKMARK_CANCEL : BOOKMARK_DONT_RESUME; 1139 + return ret != (BOOKMARK_SUCCESS ? BOOKMARK_CANCEL : BOOKMARK_DONT_RESUME); 1094 1140 } 1095 1141 } 1096 1142 ··· 1109 1155 fd = open(file, O_RDONLY); 1110 1156 if(fd >= 0) 1111 1157 { 1112 - if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0) 1113 - bookmark=global_read_buffer; 1158 + if(read_line(fd, global_temp_buffer, sizeof(global_temp_buffer)) > 0) 1159 + bookmark=global_temp_buffer; 1114 1160 close(fd); 1115 1161 } 1116 1162 } ··· 1143 1189 /* ----------------------------------------------------------------------- */ 1144 1190 bool bookmark_exists(void) 1145 1191 { 1192 + char bm_filename[MAX_PATH]; 1146 1193 bool exist=false; 1147 1194 1148 1195 char* name = playlist_get_name(NULL, global_temp_buffer, 1149 1196 sizeof(global_temp_buffer)); 1150 - if (generate_bookmark_file_name(name)) 1197 + if (generate_bookmark_file_name(bm_filename, sizeof(bm_filename), name)) 1151 1198 { 1152 - exist = file_exists(global_bookmark_file_name); 1199 + exist = file_exists(bm_filename); 1153 1200 } 1154 1201 return exist; 1155 1202 }
+2
apps/misc.c
··· 1425 1425 va_start(ap, pathfmt); 1426 1426 vsnprintf(buf, size, pathfmt, ap); 1427 1427 va_end(ap); 1428 + if ((oflag & O_PATH) == O_PATH) 1429 + return -1; 1428 1430 return open(buf, oflag, 0666); 1429 1431 } 1430 1432
+3
apps/misc.h
··· 122 122 #define BOM_UTF_16_SIZE 2 123 123 124 124 int split_string(char *str, const char needle, char *vector[], int vector_length); 125 + #ifndef O_PATH 126 + #define O_PATH 0x2000 127 + #endif 125 128 int open_pathfmt(char *buf, size_t size, int oflag, const char *pathfmt, ...); 126 129 int open_utf8(const char* pathname, int flags); 127 130 int string_option(const char *option, const char *const oplist[], bool ignore_case);