patch-src_db_simple_db_plugin_c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. --- mpd-0.17.6.orig/src/db/simple_db_plugin.c 2013-08-01 09:15:41.000000000 +0200
  2. +++ mpd-0.17.6/src/db/simple_db_plugin.c 2013-10-17 17:51:34.000000000 +0200
  3. @@ -28,6 +28,9 @@
  4. #include "conf.h"
  5. #include "directory.h"
  6. +#include <sys/mount.h>
  7. +#include <mntent.h>
  8. +#include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. @@ -128,8 +131,8 @@ simple_db_check(struct simple_db *db, GE
  13. return false;
  14. }
  15. - /* Check if we can write to the directory */
  16. - if (access(dirPath, X_OK | W_OK)) {
  17. + /* Check if we can change into the directory */
  18. + if (access(dirPath, X_OK)) {
  19. g_set_error(error_r, simple_db_quark(), errno,
  20. "Can't create db file in \"%s\": %s",
  21. dirPath, g_strerror(errno));
  22. @@ -159,9 +162,9 @@ simple_db_check(struct simple_db *db, GE
  23. }
  24. /* And check that we can write to it */
  25. - if (access(db->path, R_OK | W_OK)) {
  26. + if (access(db->path, R_OK)) {
  27. g_set_error(error_r, simple_db_quark(), errno,
  28. - "Can't open db file \"%s\" for reading/writing: %s",
  29. + "Can't open db file \"%s\" for reading: %s",
  30. db->path, g_strerror(errno));
  31. return false;
  32. }
  33. @@ -305,6 +308,9 @@ simple_db_save(struct db *_db, GError **
  34. {
  35. struct simple_db *db = (struct simple_db *)_db;
  36. struct directory *music_root = db->root;
  37. + struct mntent *mnt;
  38. + int remount;
  39. + FILE *f;
  40. db_lock();
  41. @@ -317,6 +323,26 @@ simple_db_save(struct db *_db, GError **
  42. db_unlock();
  43. g_debug("writing DB");
  44. +
  45. + remount = 0;
  46. + /* check if /data is mounted read-only */
  47. + if ((f = setmntent("/proc/mounts", "r")) == NULL)
  48. + g_message("Checking /proc/mounts failed");
  49. +
  50. + while ((mnt = getmntent(f)) != NULL) {
  51. + if (strcmp(mnt->mnt_dir, "/data") == 0 &&
  52. + hasmntopt(mnt, MNTOPT_RO) != NULL) {
  53. + remount = 1;
  54. + }
  55. + }
  56. + endmntent(f);
  57. +
  58. + if (remount) {
  59. + if (mount("","/data",0,MS_REMOUNT,0)<0) {
  60. + g_error("Remounting /data rw failed");
  61. + }
  62. + g_message("Mounted /data successfully in read-write mode");
  63. + }
  64. FILE *fp = fopen(db->path, "w");
  65. if (!fp) {
  66. @@ -338,6 +364,16 @@ simple_db_save(struct db *_db, GError **
  67. fclose(fp);
  68. + if (remount) {
  69. + sync();
  70. + if (mount("","/data",0,MS_REMOUNT|MS_RDONLY,0)<0) {
  71. + g_error("Remounting /data ro failed");
  72. + }
  73. + g_message("Mounted /data successfully in read-only mode");
  74. + }
  75. +
  76. + g_message("Successfully written database to file: %s", db->path);
  77. +
  78. struct stat st;
  79. if (stat(db->path, &st) == 0)
  80. db->mtime = st.st_mtime;