From a184a58126a8c9eb3981d1ad13c6f310fb88b8cd Mon Sep 17 00:00:00 2001 From: Sehong Na Date: Sat, 31 May 2014 12:53:34 +0900 Subject: [PATCH] Initialize Tizen 2.3 --- AUTHORS | 3 + LICENSE.APLv2.0 | 206 ++++ Makefile.am | 137 +++ autogen.sh | 12 + config.in | 161 +++ configure.ac | 167 +++ lib/include/media-server-ipc.h | 121 +++ lib/include/media-util-db.h | 61 ++ lib/include/media-util-dbg.h | 86 ++ lib/include/media-util-err.h | 93 ++ lib/include/media-util-internal.h | 55 + lib/include/media-util-ipc.h | 66 ++ lib/include/media-util-noti-common.h | 82 ++ lib/include/media-util-noti.h | 94 ++ lib/include/media-util-register.h | 71 ++ lib/include/media-util.h | 41 + lib/media-util-db.c | 556 ++++++++++ lib/media-util-ipc.c | 430 ++++++++ lib/media-util-noti.c | 321 ++++++ lib/media-util-register.c | 298 ++++++ libmedia-utils.manifest | 6 + libmedia-utils.pc.in | 11 + media-server-plugin | 1 + mobile/media-server.manifest | 27 + packaging/media-scanner.service.wearable | 10 + packaging/media-server.service.mobile | 13 + packaging/media-server.service.wearable | 14 + packaging/media-server.spec | 153 +++ src/common/include/media-common-dbg.h | 58 + src/common/include/media-common-external-storage.h | 44 + src/common/include/media-common-types.h | 112 ++ src/common/include/media-common-utils.h | 95 ++ src/common/media-common-external-storage.c | 228 ++++ src/common/media-common-utils.c | 352 ++++++ src/mediadb-update.c | 163 +++ src/scanner/include/media-scanner-db-svc.h | 135 +++ src/scanner/include/media-scanner-dbg.h | 65 ++ src/scanner/include/media-scanner-scan.h | 43 + src/scanner/include/media-scanner-socket.h | 42 + src/scanner/media-scanner-db-svc.c | 705 ++++++++++++ src/scanner/media-scanner-scan.c | 1127 ++++++++++++++++++++ src/scanner/media-scanner-socket.c | 183 ++++ src/scanner/media-scanner.c | 324 ++++++ src/server/include/media-server-db-svc.h | 58 + src/server/include/media-server-db.h | 31 + src/server/include/media-server-dbg.h | 62 ++ src/server/include/media-server-scanner.h | 28 + src/server/include/media-server-socket.h | 48 + src/server/include/media-server-thumb.h | 46 + src/server/media-server-db-svc.c | 304 ++++++ src/server/media-server-db.c | 138 +++ src/server/media-server-main.c | 440 ++++++++ src/server/media-server-scanner.c | 246 +++++ src/server/media-server-socket.c | 718 +++++++++++++ src/server/media-server-thumb.c | 997 +++++++++++++++++ wearable/media-server.manifest | 26 + 56 files changed, 10114 insertions(+) create mode 100644 AUTHORS create mode 100644 LICENSE.APLv2.0 create mode 100755 Makefile.am create mode 100755 autogen.sh create mode 100644 config.in create mode 100755 configure.ac create mode 100755 lib/include/media-server-ipc.h create mode 100755 lib/include/media-util-db.h create mode 100755 lib/include/media-util-dbg.h create mode 100755 lib/include/media-util-err.h create mode 100755 lib/include/media-util-internal.h create mode 100755 lib/include/media-util-ipc.h create mode 100755 lib/include/media-util-noti-common.h create mode 100755 lib/include/media-util-noti.h create mode 100755 lib/include/media-util-register.h create mode 100755 lib/include/media-util.h create mode 100755 lib/media-util-db.c create mode 100755 lib/media-util-ipc.c create mode 100755 lib/media-util-noti.c create mode 100755 lib/media-util-register.c create mode 100644 libmedia-utils.manifest create mode 100755 libmedia-utils.pc.in create mode 100644 media-server-plugin create mode 100644 mobile/media-server.manifest create mode 100644 packaging/media-scanner.service.wearable create mode 100644 packaging/media-server.service.mobile create mode 100644 packaging/media-server.service.wearable create mode 100755 packaging/media-server.spec create mode 100755 src/common/include/media-common-dbg.h create mode 100755 src/common/include/media-common-external-storage.h create mode 100755 src/common/include/media-common-types.h create mode 100755 src/common/include/media-common-utils.h create mode 100755 src/common/media-common-external-storage.c create mode 100755 src/common/media-common-utils.c create mode 100644 src/mediadb-update.c create mode 100755 src/scanner/include/media-scanner-db-svc.h create mode 100755 src/scanner/include/media-scanner-dbg.h create mode 100755 src/scanner/include/media-scanner-scan.h create mode 100755 src/scanner/include/media-scanner-socket.h create mode 100755 src/scanner/media-scanner-db-svc.c create mode 100755 src/scanner/media-scanner-scan.c create mode 100755 src/scanner/media-scanner-socket.c create mode 100755 src/scanner/media-scanner.c create mode 100755 src/server/include/media-server-db-svc.h create mode 100755 src/server/include/media-server-db.h create mode 100755 src/server/include/media-server-dbg.h create mode 100755 src/server/include/media-server-scanner.h create mode 100755 src/server/include/media-server-socket.h create mode 100755 src/server/include/media-server-thumb.h create mode 100755 src/server/media-server-db-svc.c create mode 100755 src/server/media-server-db.c create mode 100755 src/server/media-server-main.c create mode 100755 src/server/media-server-scanner.c create mode 100755 src/server/media-server-socket.c create mode 100755 src/server/media-server-thumb.c create mode 100644 wearable/media-server.manifest diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..699f897 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,3 @@ +Yong Yeon Kim +Hyunjun Ko +Haejeong Kim diff --git a/LICENSE.APLv2.0 b/LICENSE.APLv2.0 new file mode 100644 index 0000000..f94008a --- /dev/null +++ b/LICENSE.APLv2.0 @@ -0,0 +1,206 @@ +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + diff --git a/Makefile.am b/Makefile.am new file mode 100755 index 0000000..76e8c1d --- /dev/null +++ b/Makefile.am @@ -0,0 +1,137 @@ +ACLOCAL_AMFLAGS = -I m4 + +SUBDIRS = . + +AM_CPPFLAGS = $(FMS_DEBUG_FLAGS) + +AM_LDFLAGS=-Wl,--as-needed -Wl,--hash-style=both + +### pkgconfig ### +pkgconfigdir = $(libdir)/pkgconfig +dist_pkgconfig_DATA = libmedia-utils.pc + +### libmedia-utils.la ### +lib_LTLIBRARIES = libmedia-utils.la +libmedia_utils_la_SOURCES = lib/media-util-noti.c \ + lib/media-util-ipc.c \ + lib/media-util-db.c \ + lib/media-util-register.c + +libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \ + $(GLIB_CFLAGS) \ + $(DLOG_CFLAGS) \ + $(DBUS_CFLAGS) \ + $(SQLITE3_CFLAGS) \ + $(DB_UTIL_CFLAGS) \ + $(PHONESTATUS_CFLAGS) + +libmedia_utils_la_LIBADD = $(GLIB_LIBS) \ + $(DLOG_LIBS) \ + $(DBUS_LIBS) \ + $(SQLITE3_LIBS) \ + $(DB_UTIL_LIBS) \ + $(PHONESTATUS_LIBS) + + +### file-manager-server ### +bin_PROGRAMS = media-server \ + media-scanner \ + mediadb-update + +media_server_SOURCES = src/common/media-common-utils.c \ + src/common/media-common-external-storage.c \ + src/server/media-server-db-svc.c \ + src/server/media-server-db.c \ + src/server/media-server-socket.c \ + src/server/media-server-thumb.c \ + src/server/media-server-scanner.c \ + src/server/media-server-main.c + +media_server_CFLAGS = -I${srcdir}/src/common/include \ + -I${srcdir}/src/server/include \ + -I${srcdir}/lib/include \ + $(GTHREAD_CFLAGS) \ + $(GLIB_CFLAGS) \ + $(DLOG_CFLAGS) \ + $(PHONESTATUS_CFLAGS) \ + $(DBUS_CFLAGS) +if USE_NOTIFICATION +media_server_CFLAGS += $(STATUS_CFLAGS) +endif + +media_server_CFLAGS += $(LIBPMCONTROL_CFLAGS) + +media_server_CFLAGS += $(SECURITY_CFLAGS) + +media_server_LDADD = libmedia-utils.la \ + $(GLIB_LIBS) \ + $(GTHREAD_LIBS) \ + $(PHONESTATUS_LIBS) \ + $(DLOG_LIBS) \ + $(AUL_LIBS) \ + $(DBUS_LIBS) \ + -ldl +if USE_NOTIFICATION +media_server_LDADD += $(STATUS_LIBS) +endif + +media_server_LDADD += $(LIBPMCONTROL_LIBS) + +media_server_LDADD += $(SECURITY_LIBS) + + + +media_scanner_SOURCES = src/common/media-common-utils.c \ + src/common/media-common-external-storage.c \ + src/scanner/media-scanner-db-svc.c \ + src/scanner/media-scanner-scan.c \ + src/scanner/media-scanner-socket.c \ + src/scanner/media-scanner.c + +media_scanner_CFLAGS = -I${srcdir}/lib/include \ + -I${srcdir}/src/common/include \ + -I${srcdir}/src/scanner/include \ + $(GTHREAD_CFLAGS) \ + $(GLIB_CFLAGS) \ + $(PHONESTATUS_CFLAGS) \ + $(DLOG_CFLAGS) \ + $(DBUS_CFLAGS) +if USE_NOTIFICATION +media_scanner_CFLAGS += $(STATUS_CFLAGS) +endif + +media_scanner_CFLAGS += $(LIBPMCONTROL_CFLAGS) + +media_scanner_LDADD = libmedia-utils.la \ + $(GLIB_LIBS) \ + $(GTHREAD_LIBS) \ + $(PHONESTATUS_LIBS) \ + $(DLOG_LIBS) \ + $(DBUS_LIBS) \ + -ldl +if USE_NOTIFICATION +media_scanner_LDADD += $(STATUS_LIBS) +endif + +media_scanner_LDADD += $(LIBPMCONTROL_LIBS) + +mediadb_update_SOURCES = src/mediadb-update.c + +mediadb_update_CFLAGS = -I${srcdir}/lib/include \ + $(GTHREAD_CFLAGS) \ + $(GLIB_CFLAGS) + +mediadb_update_LDADD = libmedia-utils.la \ + $(GLIB_LIBS) \ + $(GTHREAD_LIBS) + +### includeheaders ### +includeheadersdir = $(includedir)/media-utils +includeheaders_HEADERS = lib/include/media-util-noti-common.h \ + lib/include/media-util-noti.h \ + lib/include/media-util-register.h \ + lib/include/media-util-err.h \ + lib/include/media-util-db.h \ + lib/include/media-util-ipc.h \ + lib/include/media-util.h \ + lib/include/media-server-ipc.h diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..a9d7ee4 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +rm -rf autom4te.cache +rm -f aclocal.m4 ltmain.sh +mkdir -p m4 + +echo "Running aclocal..." ; aclocal $ACLOCAL_FLAGS -I m4 || exit 1 +echo "Running autoheader..." ; autoheader || exit 1 +echo "Running autoconf..." ; autoconf || exit 1 +echo "Running libtoolize..." ; (libtoolize --copy --automake --force || glibtoolize --automake) || exit 1 +echo "Running automake..." ; automake --add-missing --copy --foreign --force-missing || exit 1 +echo "You can now compile and build package." diff --git a/config.in b/config.in new file mode 100644 index 0000000..e4e5149 --- /dev/null +++ b/config.in @@ -0,0 +1,161 @@ +/* config.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if the `closedir' function returns void instead of `int'. */ +#undef CLOSEDIR_VOID + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_DIRENT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_DLFCN_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_FCNTL_H + +/* Define to 1 if you have the `fork' function. */ +#undef HAVE_FORK + +/* Define to 1 if you have the `gettimeofday' function. */ +#undef HAVE_GETTIMEOFDAY + +/* Define to 1 if you have the header file. */ +#undef HAVE_INTTYPES_H + +/* Define to 1 if `lstat' has the bug that it succeeds when given the + zero-length file name argument. */ +#undef HAVE_LSTAT_EMPTY_STRING_BUG + +/* Define to 1 if your system has a GNU libc compatible `malloc' function, and + to 0 otherwise. */ +#undef HAVE_MALLOC + +/* Define to 1 if you have the header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the `memset' function. */ +#undef HAVE_MEMSET + +/* Define to 1 if you have the `mkdir' function. */ +#undef HAVE_MKDIR + +/* Define to 1 if you have the header file, and it defines `DIR'. */ +#undef HAVE_NDIR_H + +/* Define to 1 if stdbool.h conforms to C99. */ +#undef HAVE_STDBOOL_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDINT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDLIB_H + +/* Define to 1 if you have the `strcasecmp' function. */ +#undef HAVE_STRCASECMP + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRINGS_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRING_H + +/* Define to 1 if you have the `strstr' function. */ +#undef HAVE_STRSTR + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_SYS_DIR_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_FILE_H + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_SYS_NDIR_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_STAT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TYPES_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_VFS_H + +/* Define to 1 if you have that is POSIX.1 compatible. */ +#undef HAVE_SYS_WAIT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_UNISTD_H + +/* Define to 1 if you have the `vfork' function. */ +#undef HAVE_VFORK + +/* Define to 1 if you have the header file. */ +#undef HAVE_VFORK_H + +/* Define to 1 if `fork' works. */ +#undef HAVE_WORKING_FORK + +/* Define to 1 if `vfork' works. */ +#undef HAVE_WORKING_VFORK + +/* Define to 1 if the system has the type `_Bool'. */ +#undef HAVE__BOOL + +/* Define to 1 if `lstat' dereferences a symlink specified with a trailing + slash. */ +#undef LSTAT_FOLLOWS_SLASHED_SYMLINK + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#undef LT_OBJDIR + +/* Name of package */ +#undef PACKAGE + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME + +/* Define to the home page for this package. */ +#undef PACKAGE_URL + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION + +/* Define to 1 if you have the ANSI C header files. */ +#undef STDC_HEADERS + +/* Define to 1 if you can safely include both and . */ +#undef TIME_WITH_SYS_TIME + +/* Define to 1 if your declares `struct tm'. */ +#undef TM_IN_SYS_TIME + +/* Version number of package */ +#undef VERSION + +/* Define to empty if `const' does not conform to ANSI C. */ +#undef const + +/* Define to rpl_malloc if the replacement function should be used. */ +#undef malloc + +/* Define to `int' if does not define. */ +#undef pid_t + +/* Define to `unsigned int' if does not define. */ +#undef size_t + +/* Define as `fork' if `vfork' does not work. */ +#undef vfork diff --git a/configure.ac b/configure.ac new file mode 100755 index 0000000..ff09ca8 --- /dev/null +++ b/configure.ac @@ -0,0 +1,167 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.61) +AC_INIT([media-server], [1.0]) +AC_CONFIG_AUX_DIR([build-aux]) +AC_CONFIG_HEADERS([config.h:config.in]) + +AM_INIT_AUTOMAKE([-Wall -Werror foreign]) + +# for platform setting +AC_CONFIG_MACRO_DIR([m4]) + +# Checks for programs. +m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) +AC_PROG_CC +AM_PROG_CC_C_O +AC_PROG_INSTALL +AC_PROG_MAKE_SET +AC_PROG_LIBTOOL + +# Check target +if ! (test "x$ARCH" = "xarmel" -o "x$ARCH" = "xi386"); then + echo "$ARCH" +# ARCH=ARM +fi + +#if test "x$MACHINE" = "xfloater"; then + CPPFLAGS="$CPPFLAGS -D _FM_GENERIC_ -D EXPORT_API=\"__attribute__((visibility(\\\"default\\\")))\"" + echo "" + echo $CPPFLAGS + echo "### check point ###" + echo "" +#fi + +CPPFLAGS="${CPPFLAGS} -DRND_LINUX" + +#For supporting Large file +CPPFLAGS="${CPPFLAGS} -D_FILE_OFFSET_BITS=64" + +# FMS_DEBUG - File Manager Service debug options +# To open debug options: +# export FMS_DEBUG=1 or configure --enable-debug +AC_ARG_ENABLE([debug], + [AS_HELP_STRING([--enable-debug], [Enable debug options])], + [case "x$enableval" in + xyes) debug=true;; + xno) debug=false;; + *) AC_MSG_ERROR([Bad value %enableval for --enable-debug]);; + esac], + [debug=false]) +if test "x$debug" = "xtrue" -o "x$FMS_DEBUG" = "x1"; then + FMS_DEBUG_FLAGS="-D FEXPLORER_DEBUG -g" +else + FMS_DEBUG_FLAGS="" +fi +AC_SUBST(FMS_DEBUG_FLAGS) + +# Checks for libraries. +PKG_CHECK_MODULES(GTHREAD, gthread-2.0) +AC_SUBST(GTHREAD_CFLAGS) +AC_SUBST(GTHREAD_LIBS) + +PKG_CHECK_MODULES(GLIB, glib-2.0) +AC_SUBST(GLIB_CFLAGS) +AC_SUBST(GLIB_LIBS) + +PKG_CHECK_MODULES(DLOG, dlog) +AC_SUBST(DLOG_CFLAGS) +AC_SUBST(DLOG_LIBS) + +PKG_CHECK_MODULES(PHONESTATUS, vconf) +AC_SUBST(PHONESTATUS_CFLAGS) +AC_SUBST(PHONESTATUS_LIBS) + +PKG_CHECK_MODULES(DBUS, dbus-glib-1) +AC_SUBST(DBUS_CFLAGS) +AC_SUBST(DBUS_LIBS) + +PKG_CHECK_MODULES(SQLITE, sqlite3) +AC_SUBST(SQLITE3_CFLAGS) +AC_SUBST(SQLITE3_LIBS) + +PKG_CHECK_MODULES(DB_UTIL, db-util) +AC_SUBST(DB_UTIL_CFLAGS) +AC_SUBST(DB_UTIL_LIBS) + +dnl use notification -------------------------------------------------------------------------- +AC_ARG_ENABLE(notification, AC_HELP_STRING([--enable-notification], [using notification]), +[ + case "${enableval}" in + yes) USE_NOTIFICATION=yes ;; + no) USE_NOTIFICATION=no ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-notification) ;; + esac +],[USE_NOTIFICATION=yes]) +if test "x$USE_NOTIFICATION" = "xyes"; then + #ticker noti library + PKG_CHECK_MODULES(STATUS, notification) + AC_SUBST(STATUS_CFLAGS) + AC_SUBST(STATUS_LIBS) +fi +AM_CONDITIONAL(USE_NOTIFICATION, test "x$USE_NOTIFICATION" = "xyes") + + +dnl use deviced -------------------------------------------------------------------------- +AC_ARG_ENABLE(deviced, AC_HELP_STRING([--enable-deviced], [using deviced]), +[ + case "${enableval}" in + yes) USE_DEVICED=yes ;; + no) USE_DEVICED=no ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-deviced) ;; + esac +],[USE_DEVICED=yes]) +if test "x$USE_DEVICED" = "xyes"; then + PKG_CHECK_MODULES(LIBPMCONTROL, deviced) + AC_SUBST(LIBPMCONTROL_CFLAGS) + AC_SUBST(LIBPMCONTROL_LIBS) +else + PKG_CHECK_MODULES(LIBPMCONTROL, pmapi) + AC_SUBST(LIBPMCONTROL_CFLAGS) + AC_SUBST(LIBPMCONTROL_LIBS) +fi +AM_CONDITIONAL(USE_DEVICED, test "x$USE_DEVICED" = "xyes") + +dnl use security server -------------------------------------------------------------------------- +AC_ARG_ENABLE(security-server, AC_HELP_STRING([--enable-security-server], [using security-server]), +[ + case "${enableval}" in + yes) USE_SECURITY=yes ;; + no) USE_SECURITY=no ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-security-security) ;; + esac +],[USE_SECURITY=yes]) +if test "x$USE_SECURITY" = "xyes"; then + #sercurity server + PKG_CHECK_MODULES(SECURITY, security-server) + AC_SUBST(SECURITY_CFLAGS) + AC_SUBST(SECURITY_LIBS) +fi +AM_CONDITIONAL(USE_SECURITY, test "x$USE_SECURITY" = "xyes") + +#Checks for header files. +AC_HEADER_DIRENT +AC_HEADER_STDC +AC_HEADER_SYS_WAIT +AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/file.h sys/vfs.h unistd.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_CONST +AC_TYPE_SIZE_T +AC_HEADER_TIME +AC_STRUCT_TM + +# Checks for library functions. +AC_FUNC_CLOSEDIR_VOID +AC_FUNC_FORK +AC_FUNC_LSTAT +AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK +AC_FUNC_MALLOC +AC_CHECK_FUNCS([gettimeofday memset mkdir strcasecmp strstr]) + +AC_CONFIG_FILES([Makefile + libmedia-utils.pc + ]) +AC_OUTPUT diff --git a/lib/include/media-server-ipc.h b/lib/include/media-server-ipc.h new file mode 100755 index 0000000..cc460a2 --- /dev/null +++ b/lib/include/media-server-ipc.h @@ -0,0 +1,121 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines IPC protocol + * + * @file media-server-ipc.h + * @author Haejeong Kim(backto.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_SERVER_IPC_H_ +#define _MEDIA_SERVER_IPC_H_ + +#define MS_TIMEOUT_SEC_3 3 /**< Response from Server time out */ +#define MS_TIMEOUT_SEC_10 10 /**< Response from Server time out */ +#define MS_TIMEOUT_SEC_20 20 /**< Response from Media server time out */ + +typedef enum{ + MS_DB_BATCH_UPDATE_PORT = 0, /**< Media DB batch update */ + MS_SCAN_DAEMON_PORT, /**< Port of communication between scanner and server */ + MS_SCANNER_PORT, /**< Directory Scanner */ + MS_DB_UPDATE_PORT, /**< Media DB Update */ + MS_THUMB_CREATOR_PORT, /**< Create thumbnail */ + MS_THUMB_COMM_PORT, /**< Port of communication between creator and server */ + MS_THUMB_DAEMON_PORT, /**< Port of Thumbnail server */ + MS_PORT_MAX, +}ms_msg_port_type_e; + +#define MAX_MSG_SIZE 4096*2 +#define MAX_FILEPATH_LEN 4096 + +typedef enum{ + MS_MSG_DB_UPDATE = 0, /**< Media DB Update */ + MS_MSG_DB_UPDATE_BATCH_START, /**< Start of media DB update batch */ + MS_MSG_DB_UPDATE_BATCH, /**< Perform of media DB update batch */ + MS_MSG_DB_UPDATE_BATCH_END, /**< End of media DB update batch */ + MS_MSG_DIRECTORY_SCANNING, /**< Non recursive Directory Scan and Media DB Update*/ + MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE,/**< Recursive Directory Scan and Media DB Update*/ + MS_MSG_BURSTSHOT_INSERT, + MS_MSG_BULK_INSERT, /**< Request bulk insert */ + MS_MSG_STORAGE_ALL, + MS_MSG_STORAGE_PARTIAL, + MS_MSG_STORAGE_INVALID, + MS_MSG_THUMB_SERVER_READY, /**< Ready from thumbnail server */ + MS_MSG_THUMB_EXTRACT_ALL_DONE, /**< Done of all-thumbnail extracting */ + MS_MSG_SCANNER_READY, /**< Ready from media scanner */ + MS_MSG_SCANNER_RESULT, /**< Result of directory scanning */ + MS_MSG_SCANNER_BULK_RESULT, /**< Request bulk insert */ + MS_MSG_MAX /**< Invalid msg type */ +}ms_msg_type_e; + +#define MS_SCANNER_FIFO_PATH_REQ "/tmp/media-scanner-fifo-req" +#define MS_SCANNER_FIFO_PATH_RES "/tmp/media-scanner-fifo-res" +#define MS_SCANNER_FIFO_MODE 0666 + +typedef struct +{ + int sock_fd; + char *sock_path; +}ms_sock_info_s; + +typedef struct +{ + ms_msg_type_e msg_type; + int pid; + int result; + size_t msg_size; /*this is size of message below and this does not include the terminationg null byte ('\0'). */ + char msg[MAX_MSG_SIZE]; +}ms_comm_msg_s; + +typedef enum { + CLIENT_SOCKET, + SERVER_SOCKET +} ms_socket_type_e; + +typedef enum { + MS_MEDIA_THUMB_LARGE, + MS_MEDIA_THUMB_SMALL, +} ms_thumb_type_e; + +typedef struct { + ms_msg_type_e msg_type; +} ms_thumb_server_msg; + +typedef struct _thumbMsg{ + int msg_type; + int thumb_type; + int status; + int pid; + int thumb_size; + int thumb_width; + int thumb_height; + int origin_width; + int origin_height; + int origin_path_size; + int dest_path_size; + char org_path[MAX_FILEPATH_LEN]; + char dst_path[MAX_FILEPATH_LEN]; +} thumbMsg; + + +#endif /*_MEDIA_SERVER_IPC_H_*/ diff --git a/lib/include/media-util-db.h b/lib/include/media-util-db.h new file mode 100755 index 0000000..efbea0f --- /dev/null +++ b/lib/include/media-util-db.h @@ -0,0 +1,61 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-noti.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + #ifndef _MEDIA_UTIL_DB_H_ +#define _MEDIA_UTIL_DB_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void MediaDBHandle; /**< Handle */ + +int media_db_connect(MediaDBHandle **handle); + +int media_db_disconnect(MediaDBHandle *handle); + +int media_db_request_update_db(const char *query_str); + +int media_db_request_update_db_batch_start(const char *query_str); + +int media_db_request_update_db_batch(const char *query_str); + +int media_db_request_update_db_batch_end(const char *query_str); + +int media_db_request_update_db_batch_clear(void); + +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /*_MEDIA_UTIL_DB_H_*/ diff --git a/lib/include/media-util-dbg.h b/lib/include/media-util-dbg.h new file mode 100755 index 0000000..c0ec755 --- /dev/null +++ b/lib/include/media-util-dbg.h @@ -0,0 +1,86 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-dbg.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_UTIL_DBG_H_ +#define _MEDIA_UTIL_DBG_H_ + +#include +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "MEDIA_UTIL" + +#define FONT_COLOR_RESET "\033[0m" +#define FONT_COLOR_RED "\033[31m" +#define FONT_COLOR_GREEN "\033[32m" +#define FONT_COLOR_YELLOW "\033[33m" +#define FONT_COLOR_BLUE "\033[34m" +#define FONT_COLOR_PURPLE "\033[35m" +#define FONT_COLOR_CYAN "\033[36m" +#define FONT_COLOR_GRAY "\033[37m" + +#define MSAPI_DBG_SLOG(fmt, args...) do { \ + SECURE_LOGD(fmt "\n", ##args); \ + } while (0) + +#define MSAPI_DBG(fmt, arg...) do { \ + LOGD(FONT_COLOR_RESET fmt, ##arg); \ + } while (0) + +#define MSAPI_DBG_INFO(fmt, arg...) do { \ + LOGI(FONT_COLOR_GREEN fmt, ##arg); \ + } while (0) + +#define MSAPI_DBG_ERR(fmt, arg...) do { \ + LOGE(FONT_COLOR_RED fmt, ##arg); \ + } while (0) + +#define MSAPI_DBG_FUNC() do { \ + LOGD(FONT_COLOR_RESET); \ + } while (0) + +#define MSAPI_RETV_IF(expr, val) do { \ + if(expr) { \ + LOGE(FONT_COLOR_RED); \ + return (val); \ + } \ + } while (0) + +#define MSAPI_RETVM_IF(expr, val, fmt, arg...) do { \ + if(expr) { \ + LOGE(FONT_COLOR_RED fmt, ##arg); \ + return (val); \ + } \ + } while (0) + +#endif /*_MEDIA_UTIL_DBG_H_*/ diff --git a/lib/include/media-util-err.h b/lib/include/media-util-err.h new file mode 100755 index 0000000..c39e7c4 --- /dev/null +++ b/lib/include/media-util-err.h @@ -0,0 +1,93 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-err.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_UTIL_ERR_H_ +#define _MEDIA_UTIL_ERR_H_ + +#define MS_MEDIA_ERR_NONE 0 + +/* internal operation error*/ +#define MS_MEDIA_ERR_INTERNAL -1 +#define MS_MEDIA_ERR_INVALID_PARAMETER -2 /* invalid parameter(s) */ +#define MS_MEDIA_ERR_INVALID_PATH -3 /* Invalid path */ +#define MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL -4 /* exception of memory allocation */ +#define MS_MEDIA_ERR_NOT_ENOUGH_SPACE -5 /* not enough space in storage */ + +/* DB operation error*/ +#define MS_MEDIA_ERR_DB_CONNECT_FAIL -11 /* connecting database fails */ +#define MS_MEDIA_ERR_DB_DISCONNECT_FAIL -12 /* disconnecting database fails */ +#define MS_MEDIA_ERR_DB_INSERT_FAIL -13 /* inserting record fails */ +#define MS_MEDIA_ERR_DB_DELETE_FAIL -14 /* deleting record fails */ +#define MS_MEDIA_ERR_DB_UPDATE_FAIL -15 /* updating record fails */ +#define MS_MEDIA_ERR_DB_BUSY_FAIL -16 /* DB Busy */ +#define MS_MEDIA_ERR_DB_CONSTRAINT_FAIL -17 /* DB CONSTRAINT fails - In case of insert, the record already exists */ +#define MS_MEDIA_ERR_DB_BATCH_UPDATE_BUSY -18 /* Batch update thread is full */ + +/* IPC operation error*/ +#define MS_MEDIA_ERR_SOCKET_INTERNAL -21 /* receive error from socket API */ +#define MS_MEDIA_ERR_SOCKET_CONN -22 /* socket connect error */ +#define MS_MEDIA_ERR_SOCKET_BIND -23 /* socket binding fails */ +#define MS_MEDIA_ERR_SOCKET_SEND -24 /* socket sending fails */ +#define MS_MEDIA_ERR_SOCKET_RECEIVE -25 /* socket receiving fails */ +#define MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT -26 /* socket receive timeout error */ +#define MS_MEDIA_ERR_SOCKET_ACCEPT -27 /* socket accept fails */ + +#define MS_MEDIA_ERR_DBUS_ADD_FILTER -31 /* DBUS add filter fails*/ +#define MS_MEDIA_ERR_DBUS_GET -32 /* DBUS get fails */ + +#define MS_MEDIA_ERR_FIFO_MAKE_FAIL -41 /* FIFO making fails */ + +/* DIRECTORY error*/ +#define MS_MEDIA_ERR_DIR_OPEN_FAIL -51 /* direcotry opennig fails */ +#define MS_MEDIA_ERR_DIR_CLOSE_FAIL -53 /* directory closing fails */ +#define MS_MEDIA_ERR_DIR_READ_FAIL -52 /* directory reading fails */ + +/* FILE error*/ +#define MS_MEDIA_ERR_FILE_OPEN_FAIL -61 /* file opennig fails */ +#define MS_MEDIA_ERR_FILE_CLOSE_FAIL -62 /* file closing fails */ +#define MS_MEDIA_ERR_FILE_READ_FAIL -63 /* file reading fails */ +#define MS_MEDIA_ERR_FILE_WRITE_FAIL -64 /* file writing fails */ + +/* MEDIA SERVER error*/ +#define MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL -101 /* DB server busy */ +#define MS_MEDIA_ERR_SCANNER_FORCE_STOP -102 /* scanning is stopped forcely */ +#define MS_MEDIA_ERR_PERMISSION_DENIED -103 /* Do have permission of request */ + +/*ETC*/ +#define MS_MEDIA_ERR_VCONF_SET_FAIL -201 /* vconf setting fails*/ +#define MS_MEDIA_ERR_VCONF_GET_FAIL -202 /* vconf getting fails*/ +#define MS_MEDIA_ERR_SCANNER_NOT_READY -203 /* scanner is not ready */ +#define MS_MEDIA_ERR_DYNAMIC_LINK -204 /* fail to dynamic link */ +#define MS_MEDIA_ERR_INVALID_IPC_MESSAGE -205 /* received message is not valid */ +#define MS_MEDIA_ERR_DATA_TAINTED -206 /* received data is tainted */ +#define MS_MEDIA_ERR_SEND_NOTI_FAIL -207 /* sending notification is failed */ + +#define MS_MEDIA_ERR_MAX -999 + +#endif /*_MEDIA_UTIL_ERR_H_*/ diff --git a/lib/include/media-util-internal.h b/lib/include/media-util-internal.h new file mode 100755 index 0000000..7cf8396 --- /dev/null +++ b/lib/include/media-util-internal.h @@ -0,0 +1,55 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-internal.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#ifndef _MEDIA_UTIL_INTERNAL_H_ +#define _MEDIA_UTIL_INTERNAL_H_ + +#include "media-util-db.h" + +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef TRUE +#define TRUE 1 +#endif + +#define MS_SAFE_FREE(src) { if(src) {free(src); src = NULL;} } +#define MS_MALLOC(src, size) { if (size > SIZE_MAX || size <= 0) {src = NULL;} \ + else { src = malloc(size); memset(src, 0x0, size);} } +#define MS_STRING_VALID(str) \ + ((str != NULL && strlen(str) > 0) ? TRUE : FALSE) + +int media_db_update_db(MediaDBHandle *handle, const char *query_str); + +int media_db_update_db_batch_start(const char *query_str); +int media_db_update_db_batch(const char *query_str); +int media_db_update_db_batch_end(MediaDBHandle *handle, const char *query_str); + +#endif /*_MEDIA_UTIL_INTERNAL_H_*/ diff --git a/lib/include/media-util-ipc.h b/lib/include/media-util-ipc.h new file mode 100755 index 0000000..aa4d8c5 --- /dev/null +++ b/lib/include/media-util-ipc.h @@ -0,0 +1,66 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of IPC. + * + * @file media-util-ipc.h + * @author Haejeong Kim(backto.kim@samsung.com) + * @version 1.0 + * @brief + */ + #ifndef _MEDIA_UTIL_IPC_H_ +#define _MEDIA_UTIL_IPC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#include "media-server-ipc.h" + +#define SERVER_IP "127.0.0.1" + +typedef enum { + MS_PROTOCOL_UDP, + MS_PROTOCOL_TCP +} ms_protocol_e; + +int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, ms_sock_info_s* sock_info); +int ms_ipc_create_server_socket(ms_protocol_e protocol, ms_msg_port_type_e port, int *sock_fd); +int ms_ipc_send_msg_to_server(int sockfd, ms_msg_port_type_e port, ms_comm_msg_s *send_msg, struct sockaddr_un *serv_addr); +int ms_ipc_send_msg_to_server_tcp(int sockfd, ms_msg_port_type_e port, ms_comm_msg_s *send_msg, struct sockaddr_un *serv_addr); +int ms_ipc_send_msg_to_client(int sockfd, ms_comm_msg_s *send_msg, struct sockaddr_un *client_addr); +int ms_ipc_send_msg_to_client_tcp(int sockfd, ms_comm_msg_s *send_msg, struct sockaddr_un *client_addr); +int ms_ipc_receive_message(int sockfd, void *recv_msg, unsigned int msg_size, struct sockaddr_un *client_addr, unsigned int *size); +int ms_ipc_receive_message_tcp(int client_sock, ms_comm_msg_s *recv_msg); +int ms_ipc_wait_message(int sockfd, void *recv_msg, unsigned int msg_size, struct sockaddr_un *recv_addr, unsigned int *size); +int ms_ipc_delete_client_socket(ms_sock_info_s* sock_info); +int ms_ipc_accept_client_tcp(int serv_sock, int* client_sock); + +#ifdef __cplusplus +} +#endif + +#endif /*_MEDIA_UTIL_IPC_H_*/ diff --git a/lib/include/media-util-noti-common.h b/lib/include/media-util-noti-common.h new file mode 100755 index 0000000..60e8cf4 --- /dev/null +++ b/lib/include/media-util-noti-common.h @@ -0,0 +1,82 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-not-commoni.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + #ifndef _MEDIA_UTIL_NOTI_COMMON_H_ +#define _MEDIA_UTIL_NOTI_COMMON_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + MS_MEDIA_ITEM_FILE = 0, + MS_MEDIA_ITEM_DIRECTORY = 1, +}media_item_type_e; + +typedef enum { + MS_MEDIA_ITEM_INSERT = 0, + MS_MEDIA_ITEM_DELETE = 1, + MS_MEDIA_ITEM_UPDATE = 2, +}media_item_update_type_e; + +typedef enum { + MS_MEDIA_UNKNOWN = -1, /**< Unknown Conntent*/ + MS_MEDIA_IMAGE = 0, /**< Image Content*/ + MS_MEDIA_VIDEO = 1, /**< Video Content*/ + MS_MEDIA_SOUND = 2, /**< Sound Content like Ringtone*/ + MS_MEDIA_MUSIC = 3, /**< Music Content like mp3*/ + MS_MEDIA_OTHER = 4, /**< Invalid Content*/ +}media_type_e; + +typedef void (*db_update_cb)(int pid, /* mandatory */ + media_item_type_e item, /* mandatory */ + media_item_update_type_e update_type, /* mandatory */ + char* path, /* mandatory */ + char* uuid, /* optional */ + media_type_e media_type, /* optional */ + char *mime_type, /* optional */ + void *user_data); + +typedef void (*clear_user_data_cb)(void * user_data); + +typedef void *MediaNotiHandle; /**< Handle */ + +#define MS_MEDIA_DBUS_PATH "/com/mediaserver/dbus/notify" +#define MS_MEDIA_DBUS_INTERFACE "com.mediaserver.dbus.Signal" +#define MS_MEDIA_DBUS_MATCH_RULE "type='signal',interface='com.mediaserver.dbus.Signal'" + +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /*_MEDIA_UTIL_NOTI_COMMON_H_*/ diff --git a/lib/include/media-util-noti.h b/lib/include/media-util-noti.h new file mode 100755 index 0000000..c7e8b28 --- /dev/null +++ b/lib/include/media-util-noti.h @@ -0,0 +1,94 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-noti.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + #ifndef _MEDIA_UTIL_NOTI_H_ +#define _MEDIA_UTIL_NOTI_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "media-util-noti-common.h" + +/** +* @fn int media_db_update_subscribe(void); +* @brief This function announce media database is updated to other applications.
+* @return This function returns 0 on success, and -1 on failure. +* @param[in] none +* @remark This function is recommandation for other application being aware of database updating.
+* @par example +* @code + +#include +#include +#include + +void callback() +{ + printf("listen dbus from media-server\n"); +} + +int +main (int argc, char **argv) +{ + GMainLoop *loop; + + loop = g_main_loop_new (NULL, FALSE); + + media_db_update_subscribe(callback); + + g_main_loop_run (loop); + + return 0; +} + +*/ + +int media_db_update_subscribe(db_update_cb user_cb, void *user_data); + +int media_db_update_unsubscribe(void); + +int media_db_update_send(int pid, /* mandatory */ + media_item_type_e item, /* mandatory */ + media_item_update_type_e update_type, /* mandatory */ + char* path, /* mandatory */ + char* uuid, /* optional */ + media_type_e media_type, /* optional */ + char *mime_type /* optional */ + ); + +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /*_MEDIA_UTIL_NOTI_H_*/ diff --git a/lib/include/media-util-register.h b/lib/include/media-util-register.h new file mode 100755 index 0000000..cc6e16e --- /dev/null +++ b/lib/include/media-util-register.h @@ -0,0 +1,71 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-register.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + #ifndef _MEDIA_UTIL_REGISTER_H_ +#define _MEDIA_UTIL_REGISTER_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum +{ + MEDIA_DIRECTORY_SCAN = 0, + MEDIA_FILES_REGISTER, +} media_request_type_e; + +typedef struct +{ + int pid; + int result; + int request_type; + char *complete_path; /* if the request type is MEDIA_FILES_REGISTER, this value will be NULL. */ +}media_request_result_s; + +typedef void (*scan_complete_cb)(media_request_result_s *, void *); +typedef void (*insert_complete_cb)(media_request_result_s *, void *); + +int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data); + +int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data); + +int media_burstshot_register(const char *list_path, insert_complete_cb user_callback, void *user_data); + +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /*_MEDIA_UTIL_REGISTER_H_*/ diff --git a/lib/include/media-util.h b/lib/include/media-util.h new file mode 100755 index 0000000..1dca482 --- /dev/null +++ b/lib/include/media-util.h @@ -0,0 +1,41 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef _MEDIA_UTIL_H_ +#define _MEDIA_UTIL_H_ + +#include +#include +#include +#include +#include + +#define MOUNT_PATH "/opt/usr" +#define STORAGE_PATH "/opt/storage" + +#define MEDIA_ROOT_PATH_INTERNAL MOUNT_PATH"/media" +#define MEDIA_ROOT_PATH_SDCARD STORAGE_PATH"/sdcard" + +#define MEDIA_THUMB_ROOT_PATH MOUNT_PATH"/media" +#define MEDIA_DB_NAME MOUNT_PATH"/dbspace/.media.db" /**< media db name*/ +#define MEDIA_DATA_PATH MOUNT_PATH"/data/file-manager-service" + +#endif /*_MEDIA_UTIL_H_*/ diff --git a/lib/media-util-db.c b/lib/media-util-db.c new file mode 100755 index 0000000..e324f88 --- /dev/null +++ b/lib/media-util-db.c @@ -0,0 +1,556 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of Media DB. + * + * @file media-util-db.c + * @author Haejeong Kim(backto.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#include +#include +#include +#include +#include "media-server-ipc.h" +#include "media-util-dbg.h" +#include "media-util-internal.h" +#include "media-util.h" + +static __thread char **sql_list = NULL; +static __thread int g_list_idx = 0; + +static int __media_db_busy_handler(void *pData, int count); +static int __media_db_connect_db_with_handle(sqlite3 **db_handle); +static int __media_db_disconnect_db_with_handle(sqlite3 *db_handle); + +static void __media_db_destroy_sql_list() +{ + int i = 0; + + for (i = 0; i < g_list_idx; i++) { + MS_SAFE_FREE(sql_list[i]); + } + + MS_SAFE_FREE(sql_list); + g_list_idx = 0; +} + +static int __media_db_busy_handler(void *pData, int count) +{ + usleep(50000); + + MSAPI_DBG("media_db_busy_handler called : %d", count); + + return 100 - count; +} + +static int __media_db_connect_db_with_handle(sqlite3 **db_handle) +{ + int ret = MS_MEDIA_ERR_NONE; + + /*Connect DB*/ + ret = db_util_open(MEDIA_DB_NAME, db_handle, DB_UTIL_REGISTER_HOOK_METHOD); + + if (SQLITE_OK != ret) { + + MSAPI_DBG_ERR("error when db open"); + *db_handle = NULL; + return MS_MEDIA_ERR_DB_CONNECT_FAIL; + } + + if (*db_handle == NULL) { + MSAPI_DBG_ERR("*db_handle is NULL"); + return MS_MEDIA_ERR_DB_CONNECT_FAIL; + } + + /*Register busy handler*/ + ret = sqlite3_busy_handler(*db_handle, __media_db_busy_handler, NULL); + + if (SQLITE_OK != ret) { + + if (*db_handle) { + MSAPI_DBG_ERR("[error when register busy handler] %s\n", sqlite3_errmsg(*db_handle)); + } + + db_util_close(*db_handle); + *db_handle = NULL; + + return MS_MEDIA_ERR_DB_CONNECT_FAIL; + } + + return MS_MEDIA_ERR_NONE; +} + +static int __media_db_disconnect_db_with_handle(sqlite3 *db_handle) +{ + int ret = MS_MEDIA_ERR_NONE; + + ret = db_util_close(db_handle); + + if (SQLITE_OK != ret) { + MSAPI_DBG_ERR("error when db close"); + MSAPI_DBG_ERR("Error : %s", sqlite3_errmsg(db_handle)); + db_handle = NULL; + return MS_MEDIA_ERR_DB_DISCONNECT_FAIL; + } + + return MS_MEDIA_ERR_NONE; +} + +extern char MEDIA_IPC_PATH[][50]; + +static int __media_db_request_update_tcp(ms_msg_type_e msg_type, const char *request_msg) +{ + int ret = MS_MEDIA_ERR_NONE; + int request_msg_size = 0; + int sockfd = -1; + ms_sock_info_s sock_info; + struct sockaddr_un serv_addr; + int port = MS_DB_UPDATE_PORT; + + if(!MS_STRING_VALID(request_msg)) + { + MSAPI_DBG_ERR("invalid query"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + request_msg_size = strlen(request_msg); + if(request_msg_size >= MAX_MSG_SIZE) + { + MSAPI_DBG_ERR("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + +// MSAPI_DBG("querysize[%d] query[%s]", request_msg_size, request_msg); + + ms_comm_msg_s send_msg; + memset((void *)&send_msg, 0, sizeof(ms_comm_msg_s)); + + send_msg.msg_type = msg_type; + send_msg.msg_size = request_msg_size; + strncpy(send_msg.msg, request_msg, request_msg_size); + + /*Create Socket*/ + ret = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info); + sockfd = sock_info.sock_fd; + MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret); + + /*Set server Address*/ + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sun_family = AF_UNIX; +// MSAPI_DBG_SLOG("%s", MEDIA_IPC_PATH[port]); + strcpy(serv_addr.sun_path, MEDIA_IPC_PATH[port]); + + /* Connecting to the media db server */ + if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { + MSAPI_DBG_ERR("connect error : %s", strerror(errno)); + close(sockfd); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + /* Send request */ + if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) { + MSAPI_DBG_ERR("send failed : %s", strerror(errno)); + close(sockfd); + return MS_MEDIA_ERR_SOCKET_SEND; + } + + /*Receive Response*/ + int recv_msg_size = -1; + int recv_msg = -1; +RETRY: + if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) { + MSAPI_DBG_ERR("recv failed : %s[%d]", strerror(errno), sockfd); + + if (errno == EINTR) { + MSAPI_DBG_ERR("catch interrupt: %s", strerror(errno)); + goto RETRY; + } + + close(sockfd); + if (errno == EWOULDBLOCK) { + MSAPI_DBG_ERR("Timeout. Can't try any more"); + return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; + } else { + MSAPI_DBG_ERR("recv failed : %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_RECEIVE; + } + } + + MSAPI_DBG("RECEIVE OK [%d]", recv_msg); + ret = recv_msg; + + close(sockfd); + + return ret; +} + +static __thread int g_tcp_client_sock = -1; + +static int __media_db_get_client_tcp_sock() +{ + return g_tcp_client_sock; +} + +static int __media_db_prepare_tcp_client_socket() +{ + int ret = MS_MEDIA_ERR_NONE; + int sockfd = -1; + ms_sock_info_s sock_info; + struct sockaddr_un serv_addr; + int port = MS_DB_BATCH_UPDATE_PORT; + + /*Create TCP Socket*/ + ret = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info); + sockfd = sock_info.sock_fd; + MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret); + + /*Set server Address*/ + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sun_family = AF_UNIX; +// MSAPI_DBG_SLOG("%s", MEDIA_IPC_PATH[port]); + strcpy(serv_addr.sun_path, MEDIA_IPC_PATH[port]); + + /* Connecting to the media db server */ + if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { + MSAPI_DBG_ERR("connect error : %s", strerror(errno)); + close(sockfd); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + g_tcp_client_sock = sockfd; + + MSAPI_DBG("Connected successfully"); + + return 0; +} + +static int __media_db_close_tcp_client_socket() +{ + int ret = MS_MEDIA_ERR_NONE; + + if (g_tcp_client_sock != -1) { + if (close(g_tcp_client_sock)<0) { + MSAPI_DBG_ERR("sock(%d) close failed [%s]", g_tcp_client_sock, strerror(errno)); + ret = MS_MEDIA_ERR_SOCKET_INTERNAL; + } + g_tcp_client_sock = -1; + } + + return ret; +} + +static int __media_db_request_batch_update(ms_msg_type_e msg_type, const char *request_msg) +{ + int ret = MS_MEDIA_ERR_NONE; + int request_msg_size = 0; + int sockfd = -1; + + if(!MS_STRING_VALID(request_msg)) + { + MSAPI_DBG_ERR("invalid query"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + request_msg_size = strlen(request_msg); + if(request_msg_size >= MAX_MSG_SIZE) + { + MSAPI_DBG_ERR("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + MSAPI_DBG_SLOG("querysize[%d] query[%s]", request_msg_size, request_msg); + ms_comm_msg_s send_msg; + memset((void *)&send_msg, 0, sizeof(ms_comm_msg_s)); + + send_msg.msg_type = msg_type; + send_msg.msg_size = request_msg_size; + strncpy(send_msg.msg, request_msg, request_msg_size); + + sockfd = __media_db_get_client_tcp_sock(); + if (sockfd <= 0) { + return MS_MEDIA_ERR_SOCKET_CONN; + } + + /* Send request */ + if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) { + MSAPI_DBG_ERR("send failed : %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_SEND; + } + + /*Receive Response*/ + int recv_msg_size = -1; + int recv_msg = -1; + if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) { + MSAPI_DBG_ERR("recv failed : %s[%d]", strerror(errno), sockfd); + + if (errno == EWOULDBLOCK) { + MSAPI_DBG_ERR("Timeout. Can't try any more"); + return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; + } else { + MSAPI_DBG_ERR("recv failed : %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_RECEIVE; + } + } + + MSAPI_DBG("RECEIVE OK [%d]", recv_msg); + ret = recv_msg; + + if (ret != MS_MEDIA_ERR_NONE) { + MSAPI_DBG_ERR("batch updated[%d] failed, error [%d]", msg_type, ret); + } + + return ret; +} + +static int _media_db_update_directly(sqlite3 *db_handle, const char *sql_str) +{ + int ret = MS_MEDIA_ERR_NONE; + char *zErrMsg = NULL; + +// MSAPI_DBG_SLOG("SQL = [%s]", sql_str); + + ret = sqlite3_exec(db_handle, sql_str, NULL, NULL, &zErrMsg); + + if (SQLITE_OK != ret) { + MSAPI_DBG_ERR("DB Update Fail SQL:%s", sql_str); + MSAPI_DBG_ERR("ERROR [%s]", zErrMsg); + if (ret == SQLITE_BUSY) + ret = MS_MEDIA_ERR_DB_BUSY_FAIL; + else if (ret == SQLITE_CONSTRAINT) + ret = MS_MEDIA_ERR_DB_CONSTRAINT_FAIL; + else + ret = MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + + if (zErrMsg) + sqlite3_free (zErrMsg); + + return ret; +} + +int media_db_connect(MediaDBHandle **handle) +{ + int ret = MS_MEDIA_ERR_NONE; + sqlite3 * db_handle = NULL; + + MSAPI_DBG_FUNC(); + + ret = __media_db_connect_db_with_handle(&db_handle); + MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret); + + *handle = db_handle; + return MS_MEDIA_ERR_NONE; +} + +int media_db_disconnect(MediaDBHandle *handle) +{ + sqlite3 * db_handle = (sqlite3 *)handle; + + MSAPI_DBG_FUNC(); + + MSAPI_RETVM_IF(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + + return __media_db_disconnect_db_with_handle(db_handle); +} + +int media_db_request_update_db(const char *query_str) +{ + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_DBG_FUNC(); + + MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); + + ret = __media_db_request_update_tcp(MS_MSG_DB_UPDATE, query_str); + if (ret != MS_MEDIA_ERR_NONE) { + MSAPI_DBG_ERR("__media_db_request_update_tcp failed : %d", ret); + } + + return ret; +} + +int media_db_request_update_db_batch_start(const char *query_str) +{ + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_DBG_FUNC(); + + MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); + + ret = __media_db_prepare_tcp_client_socket(); + if (ret != MS_MEDIA_ERR_NONE) { + MSAPI_DBG_ERR("__media_db_prepare_tcp_client_socket failed : %d", ret); + __media_db_close_tcp_client_socket(); + return ret; + } + + ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH_START, query_str); + if (ret != MS_MEDIA_ERR_NONE) { + __media_db_close_tcp_client_socket(); + } + + return ret; +} + +int media_db_request_update_db_batch(const char *query_str) +{ + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_DBG_FUNC(); + + MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); + + ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH, query_str); + if (ret != MS_MEDIA_ERR_NONE) { + __media_db_close_tcp_client_socket(); + } + + return ret; +} + +int media_db_request_update_db_batch_end(const char *query_str) +{ + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_DBG_FUNC(); + + if (!MS_STRING_VALID(query_str)) { + MSAPI_DBG_ERR("Invalid Query"); + __media_db_close_tcp_client_socket(); + return ret; + } + + ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH_END, query_str); + + __media_db_close_tcp_client_socket(); + + return ret; +} + +int media_db_update_db(MediaDBHandle *handle, const char *query_str) +{ + sqlite3 * db_handle = (sqlite3 *)handle; + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_RETVM_IF(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); + + ret = _media_db_update_directly(db_handle, query_str); + + return ret; +} + +int media_db_update_db_batch_start(const char *query_str) +{ + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); + + if (g_list_idx != 0) { + MSAPI_DBG_ERR("Current idx is not 0"); + ret = MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL; + } else { + sql_list = (char**)malloc(sizeof(char*)); + MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory"); + sql_list[g_list_idx++] = strdup(query_str); + MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory"); + } + + return ret; +} + +int media_db_update_db_batch(const char *query_str) +{ + int ret = MS_MEDIA_ERR_NONE; + + MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); + + sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*)); + MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory"); + + sql_list[g_list_idx++] = strdup(query_str); + MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory"); + + return ret; +} + +int media_db_update_db_batch_end(MediaDBHandle *handle, const char *query_str) +{ + sqlite3 * db_handle = (sqlite3 *)handle; + int ret = MS_MEDIA_ERR_NONE; + + if (db_handle == NULL || (!MS_STRING_VALID(query_str))) { + __media_db_destroy_sql_list(); + MSAPI_DBG_ERR("Handle is NULL"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*)); + if (sql_list == NULL) { + __media_db_destroy_sql_list(); + MSAPI_DBG_ERR("Out of memory"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + + sql_list[g_list_idx++] = strdup(query_str); + if (sql_list[g_list_idx - 1] == NULL) { + __media_db_destroy_sql_list(); + MSAPI_DBG_ERR("Out of memory"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + + int i = 0; + char *current_sql = NULL; + for (i = 0; i < g_list_idx; i++) { + current_sql = sql_list[i]; + ret = _media_db_update_directly(db_handle, current_sql); + if (ret < 0) { + if (i == 0) { + /* This is fail of "BEGIN" */ + MSAPI_DBG_ERR("Query failed : %s", current_sql); + break; + } else if (i == g_list_idx - 1) { + /* This is fail of "COMMIT" */ + MSAPI_DBG_ERR("Query failed : %s", current_sql); + break; + } else { + MSAPI_DBG_ERR("Query failed : %s, but keep going to run remaining queries", current_sql); + } + } + } + + __media_db_destroy_sql_list(); + + return ret; +} + +int media_db_request_update_db_batch_clear(void) +{ + int ret = MS_MEDIA_ERR_NONE; + + __media_db_destroy_sql_list(); + + return ret; +} diff --git a/lib/media-util-ipc.c b/lib/media-util-ipc.c new file mode 100755 index 0000000..e99c606 --- /dev/null +++ b/lib/media-util-ipc.c @@ -0,0 +1,430 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of IPC. + * + * @file media-util-ipc.c + * @author Haejeong Kim(backto.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#include +#include +#include +#include + +#include "media-util-dbg.h" +#include "media-util.h" + +char MEDIA_IPC_PATH[][50] ={ + {"/tmp/.media_ipc_dbbatchupdate"}, + {"/tmp/.media_ipc_scandaemon"}, + {"/tmp/.media_ipc_scanner"}, + {"/tmp/.media_ipc_dbupdate"}, + {"/tmp/.media_ipc_thumbcreator"}, + {"/tmp/.media_ipc_thumbcomm"}, + {"/tmp/.media_ipc_thumbdaemon"}, +}; + +#define MS_SOCK_PATH_PRFX "/tmp/.media_ipc_client" +#define MS_SOCK_PATH_TEMPLATE "XXXXXX" +#define MS_SOCK_PATH MS_SOCK_PATH_PRFX MS_SOCK_PATH_TEMPLATE + +static const char abc[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + +static int __make_rand_sock_and_bind(int sockfd, struct sockaddr_un *serv_addr, char *sock_path) +{ + int count; + char *rand_str = NULL; + int len = 0; + int ret = MS_MEDIA_ERR_NONE; + + len = strlen(sock_path); + + rand_str = &sock_path[len -strlen(MS_SOCK_PATH_TEMPLATE)]; + + for (count = 0; count < TMP_MAX; count++) { + + srand((unsigned int) time(0) + getpid()); + + rand_str[0] = abc[rand() % 62]; + rand_str[1] = abc[rand() % 62]; + rand_str[2] = abc[rand() % 62]; + rand_str[3] = abc[rand() % 62]; + rand_str[4] = abc[rand() % 62]; + rand_str[5] = abc[rand() % 62]; + + strcpy(serv_addr->sun_path, sock_path); + + /* Bind to the local address */ + if (bind(sockfd, (struct sockaddr *)serv_addr, sizeof(struct sockaddr_un)) < 0) { + MSAPI_DBG_ERR("bind failed : %s", strerror(errno)); + if(errno == EADDRINUSE) { + if (count == TMP_MAX -1) { + MSAPI_DBG_ERR("bind failed : arrive max count %d", TMP_MAX); + ret = MS_MEDIA_ERR_SOCKET_BIND; + break; + } + MSAPI_DBG_ERR("retry bind %d", count); + continue; + } else { + MSAPI_DBG_ERR("socket bind failed"); + close(sockfd); + ret= MS_MEDIA_ERR_SOCKET_BIND; + break; + } + } else { + ret = MS_MEDIA_ERR_NONE; + break; + } + } + + return ret; +} + +int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, ms_sock_info_s* sock_info) +{ + int sock = -1; + int err = MS_MEDIA_ERR_NONE; + struct sockaddr_un serv_addr; + char sock_path_temp[] = MS_SOCK_PATH; + + struct timeval tv_timeout = { timeout_sec, 0 }; + + if(protocol == MS_PROTOCOL_UDP) + { + /* Create a datagram/UDP socket */ + if ((sock = socket(PF_FILE, SOCK_DGRAM, 0)) < 0) { + MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sun_family = AF_UNIX; + /* make temp file for socket*/ + err = __make_rand_sock_and_bind(sock, &serv_addr, sock_path_temp); + if (err != MS_MEDIA_ERR_NONE) { + MSAPI_DBG_ERR("__make_rand_sock_and_bind failed"); + return err; + } + } + else + { + /*Create TCP Socket*/ + if ((sock = socket(PF_FILE, SOCK_STREAM, 0)) < 0) { + MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_CONN; + } + } + + if (timeout_sec > 0) { + if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) { + MSAPI_DBG_ERR("setsockopt failed: %s", strerror(errno)); + close(sock); + return MS_MEDIA_ERR_SOCKET_CONN; + } + } + + sock_info->sock_fd = sock; + if(protocol == MS_PROTOCOL_UDP) + sock_info->sock_path = strdup(sock_path_temp); + else + sock_info->sock_path = NULL; + + return MS_MEDIA_ERR_NONE; +} + +int ms_ipc_delete_client_socket(ms_sock_info_s* sock_info) +{ + int err = 0; + + close(sock_info->sock_fd); + MSAPI_DBG("sockfd %d close", sock_info->sock_fd); + if (sock_info->sock_path != NULL) { + err = unlink(sock_info->sock_path); + if (err< 0) { + MSAPI_DBG_ERR("unlink failed: %s", strerror(errno)); + } + free(sock_info->sock_path); + } + + return 0; +} + +int ms_ipc_create_server_socket(ms_protocol_e protocol, ms_msg_port_type_e port, int *sock_fd) +{ + int i; + bool bind_success = false; + int sock = -1; + struct sockaddr_un serv_addr; + unsigned short serv_port; + + serv_port = port; + + if(protocol == MS_PROTOCOL_UDP) + { + /* Create a datagram/UDP socket */ + if ((sock = socket(PF_FILE, SOCK_DGRAM, 0)) < 0) { + MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_CONN; + } + } + else + { + /* Create a TCP socket */ + if ((sock = socket(PF_FILE, SOCK_STREAM, 0)) < 0) { + MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_CONN; + } + } + + memset(&serv_addr, 0, sizeof(serv_addr)); + + serv_addr.sun_family = AF_UNIX; +// MSAPI_DBG_SLOG("%s", MEDIA_IPC_PATH[serv_port]); + unlink(MEDIA_IPC_PATH[serv_port]); + strcpy(serv_addr.sun_path, MEDIA_IPC_PATH[serv_port]); + + /* Bind to the local address */ + for (i = 0; i < 20; i ++) { + if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) { + bind_success = true; + break; + } + MSAPI_DBG("%d",i); + usleep(250000); + } + + if (bind_success == false) { + MSAPI_DBG_ERR("bind failed : %s %d_", strerror(errno), errno); + close(sock); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + MSAPI_DBG("bind success"); + + /* Listening */ + if (protocol == MS_PROTOCOL_TCP) { + if (listen(sock, SOMAXCONN) < 0) { + MSAPI_DBG_ERR("listen failed : %s", strerror(errno)); + close(sock); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + MSAPI_DBG("Listening..."); + } + + /*change permission of sock file*/ + if (chmod(MEDIA_IPC_PATH[serv_port], 0660) < 0) + MSAPI_DBG_ERR("chmod failed [%s]", strerror(errno)); + if (chown(MEDIA_IPC_PATH[serv_port], 0, 5000) < 0) + MSAPI_DBG_ERR("chown failed [%s]", strerror(errno)); + + *sock_fd = sock; + + return MS_MEDIA_ERR_NONE; +} + +int ms_ipc_send_msg_to_server(int sockfd, ms_msg_port_type_e port, ms_comm_msg_s *send_msg, struct sockaddr_un *serv_addr) +{ + int res = MS_MEDIA_ERR_NONE; + struct sockaddr_un addr; + + /* Set server Address */ + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, MEDIA_IPC_PATH[port]); +// MSAPI_DBG_SLOG("%s", addr.sun_path); + + if (sendto(sockfd, send_msg, sizeof(*(send_msg)), 0, (struct sockaddr *)&addr, sizeof(addr)) != sizeof(*(send_msg))) { + MSAPI_DBG_ERR("sendto failed [%s]", strerror(errno)); + res = MS_MEDIA_ERR_SOCKET_SEND; + } else { + MSAPI_DBG("sent result [%d]", send_msg->result); + MSAPI_DBG_SLOG("result message [%s]", send_msg->msg); + if (serv_addr != NULL) + *serv_addr = addr; + } + + return res; +} + +int ms_ipc_send_msg_to_server_tcp(int sockfd, ms_msg_port_type_e port, ms_comm_msg_s *send_msg, struct sockaddr_un *serv_addr) +{ + int res = MS_MEDIA_ERR_NONE; + struct sockaddr_un addr; + + /* Set server Address */ + memset(&addr, 0, sizeof(addr)); + + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, MEDIA_IPC_PATH[port]); +// MSAPI_DBG("%s", addr.sun_path); + + /* Connecting to the media db server */ + if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + MSAPI_DBG_ERR("connect error : %s", strerror(errno)); + close(sockfd); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + if (write(sockfd, send_msg, sizeof(*(send_msg))) != sizeof(*(send_msg))) { + MSAPI_DBG_ERR("write failed [%s]", strerror(errno)); + res = MS_MEDIA_ERR_SOCKET_SEND; + } else { + MSAPI_DBG("sent result [%d]", send_msg->result); + MSAPI_DBG_SLOG("result message [%s]", send_msg->msg); + if (serv_addr != NULL) + *serv_addr = addr; + } + + return res; +} + +int ms_ipc_send_msg_to_client(int sockfd, ms_comm_msg_s *send_msg, struct sockaddr_un *client_addr) +{ + int res = MS_MEDIA_ERR_NONE; + + if (sendto(sockfd, send_msg, sizeof(*(send_msg)), 0, (struct sockaddr *)client_addr, sizeof(*(client_addr))) != sizeof(*(send_msg))) { + MSAPI_DBG_ERR("sendto failed [%s]", strerror(errno)); + res = MS_MEDIA_ERR_SOCKET_SEND; + } else { + MSAPI_DBG("sent result [%d]", send_msg->result); + MSAPI_DBG_SLOG("result message [%s]", send_msg->msg); + } + + return res; +} + +int ms_ipc_send_msg_to_client_tcp(int sockfd, ms_comm_msg_s *send_msg, struct sockaddr_un *client_addr) +{ + int res = MS_MEDIA_ERR_NONE; + + if (write(sockfd, send_msg, sizeof(*(send_msg))) != sizeof(*(send_msg))) { + MSAPI_DBG_ERR("sendto failed [%s]", strerror(errno)); + res = MS_MEDIA_ERR_SOCKET_SEND; + } else { + MSAPI_DBG("sent result [%d]", send_msg->result); + MSAPI_DBG_SLOG("result message [%s]", send_msg->msg); + } + + return res; +} + +int ms_ipc_receive_message(int sockfd, void *recv_msg, unsigned int msg_size, struct sockaddr_un *recv_addr, unsigned int *addr_size) +{ + int recv_msg_size; + struct sockaddr_un addr; + socklen_t addr_len; + + if (!recv_msg) + return MS_MEDIA_ERR_INVALID_PARAMETER; + + addr_len = sizeof(addr); + if ((recv_msg_size = recvfrom(sockfd, recv_msg, msg_size, 0, (struct sockaddr *)&addr, &addr_len)) < 0) { + MSAPI_DBG_ERR("recvfrom failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_RECEIVE; + } + + MSAPI_DBG_SLOG("the path of received client address : %s", addr.sun_path); + + if (recv_addr != NULL) + *recv_addr = addr; + if (addr_size != NULL) + *addr_size = addr_len; + + return MS_MEDIA_ERR_NONE; +} + +int ms_ipc_wait_message(int sockfd, void *recv_msg, unsigned int msg_size, struct sockaddr_un *recv_addr, unsigned int *addr_size) +{ + int recv_msg_size; + socklen_t addr_len; + + if (!recv_msg ||!recv_addr) + return MS_MEDIA_ERR_INVALID_PARAMETER; + + addr_len = sizeof(struct sockaddr_un); + if ((recv_msg_size = recvfrom(sockfd, recv_msg, msg_size, 0, (struct sockaddr *)recv_addr, &addr_len)) < 0) { + MSAPI_DBG_ERR("recvfrom failed [%s]", strerror(errno)); + if (errno == EWOULDBLOCK) { + MSAPI_DBG_ERR("recvfrom Timeout."); + return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; + } else { + MSAPI_DBG_ERR("recvfrom error [%s]", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_RECEIVE; + } + } + + if (addr_size != NULL) + *addr_size = addr_len; + + return MS_MEDIA_ERR_NONE; +} + +int ms_ipc_accept_client_tcp(int serv_sock, int* client_sock) +{ + int sockfd = -1; + struct sockaddr_un client_addr; + socklen_t client_addr_len; + + if (client_sock == NULL) + return MS_MEDIA_ERR_INVALID_PARAMETER; + + client_addr_len = sizeof(client_addr); + if ((sockfd = accept(serv_sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) { + MSAPI_DBG_ERR("accept failed : %s", strerror(errno)); + *client_sock = -1; + return MS_MEDIA_ERR_SOCKET_ACCEPT; + } + + *client_sock = sockfd; + + return MS_MEDIA_ERR_NONE; +} + +int ms_ipc_receive_message_tcp(int client_sock, ms_comm_msg_s *recv_msg) +{ + int recv_msg_size = 0; + + if ((recv_msg_size = read(client_sock, recv_msg, sizeof(ms_comm_msg_s))) < 0) { + if (errno == EWOULDBLOCK) { + MSAPI_DBG_ERR("Timeout. Can't try any more"); + return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; + } else { + MSAPI_DBG_ERR("recv failed : %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_RECEIVE; + } + } + + MSAPI_DBG_SLOG("receive msg from [%d] %d, %s", recv_msg->pid, recv_msg->msg_type, recv_msg->msg); + + if (!(recv_msg->msg_size > 0 && recv_msg->msg_size < MAX_FILEPATH_LEN)) { + MSAPI_DBG_ERR("IPC message is wrong. message size is %d", recv_msg->msg_size); + return MS_MEDIA_ERR_INVALID_IPC_MESSAGE; + } + + return MS_MEDIA_ERR_NONE; +} + diff --git a/lib/media-util-noti.c b/lib/media-util-noti.c new file mode 100755 index 0000000..46cddf1 --- /dev/null +++ b/lib/media-util-noti.c @@ -0,0 +1,321 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-noti.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "media-util-internal.h" +#include "media-util-dbg.h" +#include "media-util.h" +#include "media-util-noti.h" + +DBusConnection *g_bus; +void *g_data_store; +GArray *handle_list; +static GStaticMutex noti_mutex = G_STATIC_MUTEX_INIT; +int ref_count; + +#define MS_MEDIA_DBUS_NAME "ms_db_updated" + +typedef struct noti_callback_data{ + db_update_cb user_callback; + void *user_data; +} noti_callback_data; + +static void +__free_data_fuction(void *memory) +{ + MS_SAFE_FREE(memory); + g_data_store = NULL; +} + +DBusHandlerResult +__get_message(DBusMessage *message, db_update_cb user_cb, void *userdata) +{ + /* A Ping signal on the com.burtonini.dbus.Signal interface */ + if (dbus_message_is_signal (message, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME)) { + int i = 0; + int current_type = DBUS_TYPE_INVALID; + DBusError error; + DBusMessageIter read_iter; + DBusBasicValue value[6]; + + dbus_int32_t item = -1; + dbus_int32_t pid = 0; + dbus_int32_t update_type = MS_MEDIA_UNKNOWN; + dbus_int32_t content_type = -1; + char *update_path = NULL; + char *uuid = NULL; + char *mime_type = NULL; + void *recevie_path = NULL; + int path_len = 0; + + dbus_error_init (&error); + MSAPI_DBG("size [%d]", sizeof(value)); + memset(value, 0x0, sizeof(value)); + + /* get data from dbus message */ + dbus_message_iter_init (message, &read_iter); + while ((current_type = dbus_message_iter_get_arg_type (&read_iter)) != DBUS_TYPE_INVALID){ + if (current_type == DBUS_TYPE_ARRAY) { + DBusMessageIter sub; + dbus_message_iter_recurse(&read_iter, &sub); + dbus_message_iter_get_fixed_array(&sub, &recevie_path, &path_len); + } else { + dbus_message_iter_get_basic (&read_iter, &value[i]); + i ++; + } + dbus_message_iter_next (&read_iter); + } + + item = value[0].i32; + pid = value[1].i32; + update_type = value[2].i32; + update_path = strndup(recevie_path, path_len); + if (value[3].str != NULL) uuid = strdup(value[3].str); + content_type = value[4].i32; + if (value[5].str != NULL) mime_type = strdup(value[5].str); + + if (item == MS_MEDIA_ITEM_DIRECTORY) + content_type = MS_MEDIA_UNKNOWN; + + /* getting data complete */ + user_cb(pid, + item, + update_type, + update_path, + uuid, + content_type, + mime_type, + userdata); + + MS_SAFE_FREE(update_path); + MS_SAFE_FREE(uuid); + MS_SAFE_FREE(mime_type); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static DBusHandlerResult +__message_filter (DBusConnection *connection, DBusMessage *message, void *user_data) +{ + db_update_cb user_cb = ((noti_callback_data*)user_data)->user_callback; + void *userdata = ((noti_callback_data*)user_data)->user_data; + DBusHandlerResult ret; + + ret = __get_message(message, user_cb, userdata); + + return ret; +} + + +int media_db_update_subscribe(db_update_cb user_cb, void *user_data) +{ + int ret = MS_MEDIA_ERR_NONE; + DBusError error; + noti_callback_data *callback_data = NULL; + + g_static_mutex_lock(¬i_mutex); + + if (g_bus == NULL) { + dbus_g_thread_init(); + + dbus_error_init (&error); + + g_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (!g_bus) { + MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message); + dbus_error_free (&error); + ret = MS_MEDIA_ERR_DBUS_GET; + goto ERROR; + } + + dbus_connection_setup_with_g_main (g_bus, NULL); + + MS_MALLOC(callback_data, sizeof(noti_callback_data)); + if (callback_data == NULL) { + MSAPI_DBG_ERR("MS_MALLOC failed"); + ret = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + goto ERROR; + } + callback_data->user_callback = user_cb; + callback_data->user_data = user_data; + + /* listening to messages from all objects as no path is specified */ + dbus_bus_add_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, &error); + if( !dbus_connection_add_filter (g_bus, __message_filter, callback_data, __free_data_fuction)) { + dbus_bus_remove_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, NULL); + ret = MS_MEDIA_ERR_DBUS_ADD_FILTER; + goto ERROR; + } + g_data_store = (void *)callback_data; + } + + ref_count ++; + + g_static_mutex_unlock(¬i_mutex); + + return MS_MEDIA_ERR_NONE; + +ERROR: + + if (g_bus != NULL) { + dbus_connection_unref(g_bus); + g_bus = NULL; + } + MS_SAFE_FREE(callback_data); + + g_static_mutex_unlock(¬i_mutex); + + return ret; +} + +int media_db_update_unsubscribe(void) +{ + if (g_bus == NULL) { + return MS_MEDIA_ERR_NONE; + } + + g_static_mutex_lock(¬i_mutex); + + if (ref_count == 1) { + dbus_connection_remove_filter(g_bus, __message_filter, g_data_store); + dbus_bus_remove_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, NULL); + dbus_connection_unref(g_bus); + + g_bus = NULL; + } + + ref_count --; + + g_static_mutex_unlock(¬i_mutex); + + return MS_MEDIA_ERR_NONE; +} + +int media_db_update_send(int pid, /* mandatory */ + media_item_type_e item, /* mandatory */ + media_item_update_type_e update_type, /* mandatory */ + char* path, /* mandatory */ + char* uuid, /* optional */ + media_type_e media_type, /* optional */ + char *mime_type /* optional */ + ) +{ + DBusMessage *message = NULL; + DBusConnection *bus = NULL; + DBusError error; + unsigned char *path_array = NULL; + int path_length = strlen(path) + 1; + + /* Get a connection to the session bus */ + dbus_error_init (&error); + bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (!bus) { + MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message); + dbus_error_free (&error); + return MS_MEDIA_ERR_DBUS_GET; + } + + message = dbus_message_new_signal (MS_MEDIA_DBUS_PATH, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME); + if (message != NULL) { + path_array = malloc(sizeof(unsigned char) * path_length); + memcpy(path_array, path, path_length); + + if (item == MS_MEDIA_ITEM_FILE) { + MSAPI_DBG("FILE CHANGED"); + if (uuid != NULL && mime_type != NULL) { + /* fill all datas */ + dbus_message_append_args (message, + DBUS_TYPE_INT32, &item, + DBUS_TYPE_INT32, &pid, + DBUS_TYPE_INT32, &update_type, + DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length, + DBUS_TYPE_STRING, &uuid, + DBUS_TYPE_INT32, &media_type, + DBUS_TYPE_STRING, &mime_type, + DBUS_TYPE_INVALID); + } else { + MSAPI_DBG_ERR("uuid or mime_type is NULL"); + MS_SAFE_FREE(path_array); + dbus_message_unref (message); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + } else if (item == MS_MEDIA_ITEM_DIRECTORY) { + MSAPI_DBG("DIRECTORY CHANGED"); + /* fill all datas */ + if(uuid != NULL) { + dbus_message_append_args (message, + DBUS_TYPE_INT32, &item, + DBUS_TYPE_INT32, &pid, + DBUS_TYPE_INT32, &update_type, + DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length, + DBUS_TYPE_STRING, &uuid, + DBUS_TYPE_INVALID); + } else { + dbus_message_append_args (message, + DBUS_TYPE_INT32, &item, + DBUS_TYPE_INT32, &pid, + DBUS_TYPE_INT32, &update_type, + DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length, + DBUS_TYPE_INVALID); + } + } else { + MSAPI_DBG("this request is wrong"); + } + + MS_SAFE_FREE(path_array); + + /* Send the signal */ + dbus_connection_send (bus, message, NULL); + + /* Free the signal now we have finished with it */ + dbus_message_unref (message); + + MSAPI_DBG("success send notification"); + } else { + MSAPI_DBG_ERR("dbus_message_new_signal failed"); + } + + dbus_connection_unref(bus); + + /* Return TRUE to tell the event loop we want to be called again */ + return MS_MEDIA_ERR_NONE; +} + diff --git a/lib/media-util-register.c b/lib/media-util-register.c new file mode 100755 index 0000000..4b8beb1 --- /dev/null +++ b/lib/media-util-register.c @@ -0,0 +1,298 @@ +/* + * Media Utility + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-util-register.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "media-server-ipc.h" +#include "media-util-internal.h" +#include "media-util-dbg.h" +#include "media-util.h" + +typedef struct media_callback_data{ + GSource *source; + scan_complete_cb user_callback; + void *user_data; + char *sock_path; +} media_callback_data; + +static bool _is_valid_path(const char *path) +{ + if (path == NULL) + return false; + + if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) { + return true; + } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) { + return true; + } else + return false; + + return true; +} + +static int _check_dir_path(const char *dir_path) +{ + struct stat sb; + DIR *dp = NULL; + + if (!_is_valid_path(dir_path)) { + MSAPI_DBG("Invalid path : %s", dir_path); + return MS_MEDIA_ERR_INVALID_PATH; + } + + if (stat(dir_path, &sb) == -1) { + MSAPI_DBG("stat failed"); + dp = opendir(dir_path); + if (dp == NULL) { + /*if openning directory is failed, check it exists. */ + if (errno == ENOENT) { + /* this directory is deleted */ + return MS_MEDIA_ERR_NONE; + } + } else { + closedir(dp); + } + return MS_MEDIA_ERR_INTERNAL; + } else { + if((sb.st_mode & S_IFMT) != S_IFDIR) { + MSAPI_DBG("Invalid path : %s is not directory", dir_path); + return MS_MEDIA_ERR_INVALID_PATH; + } + } + + return MS_MEDIA_ERR_NONE; +} + + +/* receive message from media-server[function : ms_receive_message_from_scanner] */ +gboolean _read_socket(GIOChannel *src, GIOCondition condition, gpointer data) +{ + GSource *source = NULL; + scan_complete_cb user_callback; + void *user_data = NULL; + ms_comm_msg_s recv_msg; + media_request_result_s req_result; + int sockfd = -1; + char *sock_path = NULL; + int recv_msg_size = 0; + + sockfd = g_io_channel_unix_get_fd(src); + if (sockfd < 0) { + MSAPI_DBG("sock fd is invalid!"); + return TRUE; + } + + memset(&recv_msg, 0x0, sizeof(ms_comm_msg_s)); + memset(&req_result, 0x0, sizeof(media_request_result_s)); + + if ((recv_msg_size = read(sockfd, &recv_msg, sizeof(ms_comm_msg_s))) < 0) { + MSAPI_DBG_ERR("recv failed : %s", strerror(errno)); + req_result.pid = -1; + req_result.result = MS_MEDIA_ERR_SOCKET_RECEIVE; + req_result.complete_path = NULL; + req_result.request_type = -1; + goto ERROR; + } + + req_result.pid = recv_msg.pid; + req_result.result = recv_msg.result; + if (recv_msg.msg_type ==MS_MSG_SCANNER_RESULT) { + req_result.complete_path = strdup(recv_msg.msg); + req_result.request_type = MEDIA_DIRECTORY_SCAN; + MSAPI_DBG("complete_path :%d", req_result.complete_path); + } else if (recv_msg.msg_type == MS_MSG_SCANNER_BULK_RESULT) { + req_result.complete_path = strdup(recv_msg.msg); + req_result.request_type = MEDIA_FILES_REGISTER; + } + + MSAPI_DBG("pid :%d", req_result.pid); + MSAPI_DBG("result :%d", req_result.result); + MSAPI_DBG("request_type :%d", req_result.request_type); + +ERROR: + source = ((media_callback_data *)data)->source; + user_callback = ((media_callback_data *)data)->user_callback; + user_data = ((media_callback_data *)data)->user_data; + sock_path = ((media_callback_data *)data)->sock_path; + + /*call user define function*/ + user_callback(&req_result, user_data); + + MS_SAFE_FREE(req_result.complete_path); + + /*close an IO channel*/ + g_io_channel_shutdown(src, FALSE, NULL); + g_io_channel_unref(src); + + g_source_destroy(source); + close(sockfd); + if (sock_path != NULL) { + MSAPI_DBG("delete path :%s", sock_path); + unlink(sock_path); + MS_SAFE_FREE(sock_path); + } + MS_SAFE_FREE(data); + + return TRUE; +} + +static int _attach_callback(int *sockfd, char* sock_path, scan_complete_cb user_callback, void *user_data) +{ + GIOChannel *channel = NULL; + GMainContext *context = NULL; + GSource *source = NULL; + media_callback_data *cb_data; + + /*get the global default main context*/ + context = g_main_context_default(); + + /* Create new channel to watch udp socket */ + channel = g_io_channel_unix_new(*sockfd); + source = g_io_create_watch(channel, G_IO_IN); + + cb_data = malloc(sizeof(media_callback_data)); + cb_data->source = source; + cb_data->user_callback = user_callback; + cb_data->user_data = user_data; + cb_data->sock_path = sock_path; + + /* Set callback to be called when socket is readable */ + g_source_set_callback(source, (GSourceFunc)_read_socket, cb_data, NULL); + g_source_attach(source, context); + g_source_unref(source); + + return MS_MEDIA_ERR_NONE; +} + +static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *request_msg, scan_complete_cb user_callback, void *user_data) +{ + int ret = MS_MEDIA_ERR_NONE; + int request_msg_size = 0; + int sockfd = -1; + int port = MS_SCANNER_PORT; + ms_comm_msg_s send_msg; + char *sock_path = NULL; + ms_sock_info_s sock_info; + + if(!MS_STRING_VALID(request_msg)) + { + MSAPI_DBG_ERR("invalid query"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + MSAPI_DBG("REQUEST DIRECTORY SCANNING"); + + request_msg_size = strlen(request_msg); + if(request_msg_size >= MAX_MSG_SIZE) + { + MSAPI_DBG_ERR("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + MSAPI_DBG("querysize[%d] query[%s]", request_msg_size, request_msg); + + memset((void *)&send_msg, 0, sizeof(ms_comm_msg_s)); + send_msg.msg_type = msg_type; + send_msg.pid = syscall(__NR_getpid); + send_msg.msg_size = request_msg_size; + strncpy(send_msg.msg, request_msg, request_msg_size); + + /*Create Socket*/ + ret = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, 0, &sock_info); + sockfd = sock_info.sock_fd; + if (sock_info.sock_path != NULL) + sock_path = sock_info.sock_path; + + MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret); + + ret = ms_ipc_send_msg_to_server_tcp(sockfd, port, &send_msg, NULL); + if (ret != MS_MEDIA_ERR_NONE) { + MSAPI_DBG_ERR("ms_ipc_send_msg_to_server failed : %d", ret); + ms_ipc_delete_client_socket(&sock_info); + return ret; + } + + ret = _attach_callback(&sockfd, sock_path, user_callback ,user_data); + if(ret != MS_MEDIA_ERR_NONE) + return ret; + + return ret; +} + + +int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data) +{ + int ret = MS_MEDIA_ERR_NONE; + + ret = _check_dir_path(directory_path); + if(ret != MS_MEDIA_ERR_NONE) + return ret; + + if (recursive_on == TRUE) + ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING, directory_path, user_callback, user_data); + else + ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, directory_path, user_callback, user_data); + + return ret; +} + +int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data) +{ + int ret = MS_MEDIA_ERR_NONE; + + ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, list_path, user_callback, user_data); + + MSAPI_DBG("client receive: %d", ret); + + return ret; +} + +int media_burstshot_register(const char *list_path, insert_complete_cb user_callback, void *user_data) +{ + int ret = MS_MEDIA_ERR_NONE; + + ret = __media_db_request_update_async(MS_MSG_BURSTSHOT_INSERT, list_path, user_callback, user_data); + + MSAPI_DBG("client receive: %d", ret); + + return ret; + +} + diff --git a/libmedia-utils.manifest b/libmedia-utils.manifest new file mode 100644 index 0000000..ca37499 --- /dev/null +++ b/libmedia-utils.manifest @@ -0,0 +1,6 @@ + + + + + + diff --git a/libmedia-utils.pc.in b/libmedia-utils.pc.in new file mode 100755 index 0000000..886d52d --- /dev/null +++ b/libmedia-utils.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: media contents managing service +Description: media contents managing service API +Requires: +Version: @VERSION@ +Libs: -L${libdir} -lmedia-utils +Cflags: -I${includedir}/media-utils diff --git a/media-server-plugin b/media-server-plugin new file mode 100644 index 0000000..aa0a393 --- /dev/null +++ b/media-server-plugin @@ -0,0 +1 @@ +libmedia-content-plugin.so diff --git a/mobile/media-server.manifest b/mobile/media-server.manifest new file mode 100644 index 0000000..d378f4c --- /dev/null +++ b/mobile/media-server.manifest @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packaging/media-scanner.service.wearable b/packaging/media-scanner.service.wearable new file mode 100644 index 0000000..d040e02 --- /dev/null +++ b/packaging/media-scanner.service.wearable @@ -0,0 +1,10 @@ +[Unit] +Description=Media scanner + +[Service] +Type=simple +ExecStart=/usr/bin/media-scanner +MemoryLimit=100M + +[Install] +WantedBy=multi-user.target diff --git a/packaging/media-server.service.mobile b/packaging/media-server.service.mobile new file mode 100644 index 0000000..7ad7b53 --- /dev/null +++ b/packaging/media-server.service.mobile @@ -0,0 +1,13 @@ +[Unit] +Description=Media server +After=vconf-setup.service + +[Service] +Type=simple +ExecStart=/usr/bin/media-server +Restart=always +RestartSec=0 + + +[Install] +WantedBy=multi-user.target diff --git a/packaging/media-server.service.wearable b/packaging/media-server.service.wearable new file mode 100644 index 0000000..c4326fe --- /dev/null +++ b/packaging/media-server.service.wearable @@ -0,0 +1,14 @@ +[Unit] +Description=Media server +Requires=icd.service +After=icd.service + +[Service] +Type=simple +ExecStart=/usr/bin/media-server +Restart=always +RestartSec=0 +MemoryLimit=100M + +[Install] +WantedBy=multi-user.target diff --git a/packaging/media-server.spec b/packaging/media-server.spec new file mode 100755 index 0000000..27a96ef --- /dev/null +++ b/packaging/media-server.spec @@ -0,0 +1,153 @@ +Name: media-server +Summary: File manager service server. +Version: 0.2.88 +Release: 1 +Group: utils +License: Apache License, Version 2.0 +Source0: %{name}-%{version}.tar.gz +Source1: media-server.service.wearable +Source2: media-scanner.service.wearable +Source3: media-server.service.mobile + +Requires(post): /usr/bin/vconftool + +%if %{_repository} == "wearable" +Requires: deviced +%else +Requires: system-server +%endif + +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(dbus-glib-1) +BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(db-util) + +%if %{_repository} == "wearable" +BuildRequires: pkgconfig(deviced) +%else +BuildRequires: pkgconfig(pmapi) +%endif + +BuildRequires: pkgconfig(security-server) + +%if %{_repository} == "mobile" +BuildRequires: pkgconfig(notification) +%endif + +%description +Description: File manager service server + +%package -n libmedia-utils +Summary: media server runtime library. +Group: TO_BE/FILLED_IN + +%description -n libmedia-utils +Description : media server runtime library. + + +%package -n libmedia-utils-devel +Summary: media server development library. +Group: Development/Libraries +Requires: libmedia-utils = %{version}-%{release} + +%description -n libmedia-utils-devel +Description: media server development library. + +%prep +%setup -q + +%build + +export GC_SECTIONS_FLAGS="-fdata-sections -ffunction-sections -Wl,--gc-sections" +export CFLAGS+=" ${GC_SECTIONS_FLAGS}" +export CXXFLAGS+=" ${GC_SECTIONS_FLAGS}" + +%if 0%{?sec_build_binary_debug_enable} +export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE" +export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE" +export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" +%endif + +%if %{_repository} == "wearable" +export CFLAGS="$CFLAGS -DTIZEN_WEARABLE" +export CXXFLAGS="$CXXFLAGS -DTIZEN_WEARABLE" +export FFLAGS="$FFLAGS -DTIZEN_WEARABLE" +%endif + +%autogen + +%configure \ +%if %{_repository} == "wearable" + --disable-notification \ +%else + --disable-deviced \ +%endif + --prefix=%{_prefix} --disable-static + + +make %{?jobs:-j%jobs} + +%install +%make_install + +mkdir -p %{buildroot}/usr/lib/systemd/system/multi-user.target.wants +%if %{_repository} == "wearable" +install -m 644 %{SOURCE1} %{buildroot}/usr/lib/systemd/system/media-server.service +%else +install -m 644 %{SOURCE3} %{buildroot}/usr/lib/systemd/system/media-server.service +%endif +ln -s ../media-server.service %{buildroot}/usr/lib/systemd/system/multi-user.target.wants/media-server.service + +%if %{_repository} == "wearable" +install -m 644 %{SOURCE2} %{buildroot}/usr/lib/systemd/system/media-scanner.service +#ln -s ../media-scanner.service %{buildroot}/usr/lib/systemd/system/multi-user.target.wants/media-scanner.service +%endif + +mkdir -p %{buildroot}/usr/etc +cp -rf %{_builddir}/%{name}-%{version}/media-server-plugin %{buildroot}/usr/etc/media-server-plugin + +#License +mkdir -p %{buildroot}/%{_datadir}/license +cp -rf %{_builddir}/%{name}-%{version}/LICENSE.APLv2.0 %{buildroot}/%{_datadir}/license/%{name} +cp -rf %{_builddir}/%{name}-%{version}/LICENSE.APLv2.0 %{buildroot}/%{_datadir}/license/libmedia-utils + +%post +vconftool set -t int db/filemanager/dbupdate "1" -f -s system::vconf_inhouse +vconftool set -t int memory/filemanager/Mmc "0" -i -f -s system::vconf_inhouse +vconftool set -t string db/private/mediaserver/mmc_info "" -f -s media-server::vconf + +%files +%if %{_repository} == "wearable" +%manifest wearable/media-server.manifest +%else +%manifest mobile/media-server.manifest +%endif +%defattr(-,root,root,-) +%{_bindir}/media-server +%{_bindir}/media-scanner +%{_bindir}/mediadb-update +/usr/lib/systemd/system/media-server.service +/usr/lib/systemd/system/multi-user.target.wants/media-server.service +%if %{_repository} == "wearable" +/usr/lib/systemd/system/media-scanner.service +%endif +#/usr/lib/systemd/system/multi-user.target.wants/media-scanner.service +/usr/etc/media-server-plugin +#License +%{_datadir}/license/%{name} +%{_datadir}/license/libmedia-utils + +%files -n libmedia-utils +%manifest libmedia-utils.manifest +%defattr(-,root,root,-) +%{_libdir}/libmedia-utils.so +%{_libdir}/libmedia-utils.so.0 +%{_libdir}/libmedia-utils.so.0.0.0 + +%files -n libmedia-utils-devel +%defattr(-,root,root,-) +%{_libdir}/pkgconfig/libmedia-utils.pc +%{_includedir}/media-utils/*.h + diff --git a/src/common/include/media-common-dbg.h b/src/common/include/media-common-dbg.h new file mode 100755 index 0000000..33b0e7b --- /dev/null +++ b/src/common/include/media-common-dbg.h @@ -0,0 +1,58 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-dbg.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#ifndef _MEDIA_SERVER_DBG_H_ +#define _MEDIA_SERVER_DBG_H_ + +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "MEDIA_COMMON" + +#define MS_DBG_SLOG(fmt, args...) SECURE_LOGD(fmt "\n", ##args); + +#define MS_DBG(fmt, args...) LOGD(fmt "\n", ##args); + +#define MS_DBG_INFO(fmt, args...) do{ if (true) { \ + LOGI(fmt "\n" , ##args); \ + }} while(false) + +#define MS_DBG_WARN(fmt, args...) do{ if (true) { \ + LOGW(fmt "\n", ##args); \ + }} while(false) + +#define MS_DBG_ERR(fmt, args...) do{ if (true) { \ + LOGE(fmt "\n", ##args); \ + }} while(false) + +#endif /*_MEDIA_SERVER_DBG_H_*/ diff --git a/src/common/include/media-common-external-storage.h b/src/common/include/media-common-external-storage.h new file mode 100755 index 0000000..3da873d --- /dev/null +++ b/src/common/include/media-common-external-storage.h @@ -0,0 +1,44 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef _MEDIA_SERVER_EXTERNAL_STORAGE_H_ +#define _MEDIA_SERVER_EXTERNAL_STORAGE_H_ + +#include "media-common-types.h" + +void +ms_make_default_path_mmc(void); + +int +ms_update_mmc_info(void); + +void +ms_mmc_removed_handler(void); + +int +ms_present_mmc_status(ms_sdcard_status_type_t status); + +void +ms_mmc_vconf_cb(void *data); + +ms_dir_scan_type_t +ms_get_mmc_state(void); + +#endif /*_MEDIA_SERVER_EXTERNAL_STORAGE_H_*/ diff --git a/src/common/include/media-common-types.h b/src/common/include/media-common-types.h new file mode 100755 index 0000000..9a96556 --- /dev/null +++ b/src/common/include/media-common-types.h @@ -0,0 +1,112 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-common-types.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#ifndef _MEDIA_SERVER_TYPES_H_ +#define _MEDIA_SERVER_TYPES_H_ + +#include +#include +#include +#include +#include + +#define FMS_PERF + +#define MS_VALIND 1 +#define MS_INVALID 0 +#define MS_RECURSIVE 1 +#define MS_NON_RECURSIVE 0 + +/*This macro is used to save and check information of inserted memory card*/ +#define MS_MMC_INFO_KEY "db/private/mediaserver/mmc_info" + +/*Use for Poweroff sequence*/ +#define POWEROFF -1 /*This number uses for stopping Scannig thread*/ + +#define MS_SAFE_FREE(src) { if(src) {free(src); src = NULL;} } +#define MS_MALLOC(src, size) { if (size > SIZE_MAX || size <= 0) {src = NULL;} \ + else { src = malloc(size); memset(src, 0x0, size);} } + +/*System default folder definition*/ +#define FAT_FILENAME_LEN_MAX 255 /* not inc null */ +#define FAT_FILEPATH_LEN_MAX 4096 /* inc null */ + +/* The following MACROs(TAF_XXX) are defined in "fs-limit.h"*/ +#define MS_FILE_NAME_LEN_MAX FAT_FILENAME_LEN_MAX /**< File name max length on file system */ +#define MS_FILE_PATH_LEN_MAX FAT_FILEPATH_LEN_MAX /**< File path max length (include file name) on file system */ + +#define MS_SOCK_NOT_ALLOCATE -1 + +typedef enum { + MS_STORAGE_INTERNAL, /**< Stored only in phone */ + MS_STORAGE_EXTERNAL, /**< Stored only in MMC */ +} ms_storage_type_t; + +typedef enum { + MS_SDCARD_INSERTED, /**< Stored only in phone */ + MS_SDCARD_REMOVED, /**< Stored only in MMC */ +} ms_sdcard_status_type_t; + +typedef enum { + MS_SCAN_INVALID, + MS_SCAN_PART, + MS_SCAN_ALL, +} ms_dir_scan_type_t; + +typedef enum { + MS_DB_UPDATING = 0, + MS_DB_UPDATED = 1 +} ms_db_status_type_t; + +typedef struct ms_dir_scan_info { + char *name; + struct ms_dir_scan_info *parent; + struct ms_dir_scan_info *Rbrother; + struct ms_dir_scan_info *next; +} ms_dir_scan_info; + +typedef struct { + char *path; + ms_storage_type_t storage_type; + ms_dir_scan_type_t scan_type; + int pid; + bool recursive_on; +} ms_scan_data_t; + +typedef struct { + char *path; + int pid; +} ms_register_data_t; + +/** + * @} + */ + +#endif /*_MEDIA_SERVER_TYPES_H_*/ diff --git a/src/common/include/media-common-utils.h b/src/common/include/media-common-utils.h new file mode 100755 index 0000000..3f01b7e --- /dev/null +++ b/src/common/include/media-common-utils.h @@ -0,0 +1,95 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-common-utils.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#ifndef _MEDIA_SERVER_UTILS_H__ +#define _MEDIA_SERVER_UTILS_H__ + +#include "media-common-types.h" + +int +ms_db_init(bool need_db_create); + +bool +ms_is_mmc_inserted(void); + +void +ms_usb_vconf_cb(void *data); + +int +ms_start(bool need_db_create); + +void +ms_end(void); + +ms_storage_type_t +ms_get_storage_type_by_full(const char *path); + +int +ms_strappend(char *res, const int size, const char *pattern, + const char *str1, const char *str2); + +int +ms_strcopy(char *res, const int size, const char *pattern, + const char *str1); + +bool +ms_config_get_int(const char *key, int *value); + +bool +ms_config_set_int(const char *key, int value); + +bool +ms_config_get_str(const char *key, char *value); + +bool +ms_config_set_str(const char *key, const char *value); + +bool +ms_config_get_bool(const char *key, int *value); + +#ifdef FMS_PERF +void +ms_check_start_time(struct timeval *start_time); + +void +ms_check_end_time(struct timeval *end_time); + +void +ms_check_time_diff(struct timeval *start_time, struct timeval *end_time); +#endif/*FMS_PERF */ + +typedef void (*power_off_cb)(void *user_data); + +int ms_add_poweoff_event_receiver(power_off_cb user_callback, void *user_data); +/** + * @} + */ +#endif/*_MEDIA_SERVER_UTILS_H__*/ + diff --git a/src/common/media-common-external-storage.c b/src/common/media-common-external-storage.c new file mode 100755 index 0000000..40520bd --- /dev/null +++ b/src/common/media-common-external-storage.c @@ -0,0 +1,228 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include +#include +#ifndef TIZEN_WEARABLE +#include +#endif + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-dbg.h" +#include "media-common-utils.h" +#include "media-common-external-storage.h" + +#define MMC_INFO_SIZE 256 + +char default_path[][MS_FILE_NAME_LEN_MAX + 1] = { + {"/opt/storage/sdcard/Images"}, + {"/opt/storage/sdcard/Videos"}, + {"/opt/storage/sdcard/Sounds"}, + {"/opt/storage/sdcard/Downloads"}, +}; + +#define DIR_NUM ((int)(sizeof(default_path)/sizeof(default_path[0]))) + +void +ms_make_default_path_mmc(void) +{ + int i = 0; + int ret = 0; + DIR *dp = NULL; + + for (i = 0; i < DIR_NUM; ++i) { + dp = opendir(default_path[i]); + if (dp == NULL) { + ret = mkdir(default_path[i], 0777); + if (ret < 0) { + MS_DBG_ERR("make fail"); + } + /*this fuction for emulator*/ + /*at the first time, the directroies are made permission 755*/ + ret = chmod(default_path[i], 0777); + if (ret != 0) { + MS_DBG_ERR("chmod failed [%s]", strerror(errno)); + } + ret = chown(default_path[i], 5000, 5000); + if (ret != 0) { + MS_DBG_ERR("chown failed [%s]", strerror(errno)); + } + } else { + closedir(dp); + } + } +} + +int +_ms_update_mmc_info(const char *cid) +{ + bool res; + + if (cid == NULL) { + MS_DBG_ERR("Parameters are invalid"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + res = ms_config_set_str(MS_MMC_INFO_KEY, cid); + if (!res) { + MS_DBG_ERR("fail to get MS_MMC_INFO_KEY"); + return MS_MEDIA_ERR_VCONF_SET_FAIL; + } + + return MS_MEDIA_ERR_NONE; +} + +bool +_ms_check_mmc_info(const char *cid) +{ + char pre_mmc_info[MMC_INFO_SIZE] = { 0 }; + bool res = false; + + if (cid == NULL) { + MS_DBG_ERR("Parameters are invalid"); + return false; + } + + res = ms_config_get_str(MS_MMC_INFO_KEY, pre_mmc_info); + if (!res) { + MS_DBG_ERR("fail to get MS_MMC_INFO_KEY"); + return false; + } + + MS_DBG("Last MMC info = %s", pre_mmc_info); + MS_DBG("Current MMC info = %s", cid); + + if (strcmp(pre_mmc_info, cid) == 0) { + return true; + } + + return false; +} + +static int +_get_contents(const char *filename, char *buf) +{ + FILE *fp; + + fp = fopen(filename, "rt"); + if (fp == NULL) { + MS_DBG_ERR("fp is NULL. file name : %s", filename); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + if (fgets(buf, 255, fp) == NULL) + MS_DBG_ERR("fgets failed"); + + fclose(fp); + + return MS_MEDIA_ERR_NONE; +} + +/*need optimize*/ +int +_ms_get_mmc_info(char *cid) +{ + char *path = "/sys/block/mmcblk1/device/cid"; + + if (_get_contents(path, cid) != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("_get_contents failed"); + } + + return MS_MEDIA_ERR_NONE; +} + +ms_dir_scan_type_t +ms_get_mmc_state(void) +{ + char cid[MMC_INFO_SIZE] = { 0 }; + ms_dir_scan_type_t ret = MS_SCAN_ALL; + + /*get new info */ + _ms_get_mmc_info(cid); + + /*check it's same mmc */ + if (_ms_check_mmc_info(cid)) { + ret = MS_SCAN_PART; + } + + return ret; +} + +int +ms_update_mmc_info(void) +{ + int err; + char cid[MMC_INFO_SIZE] = { 0 }; + + err = _ms_get_mmc_info(cid); + + err = _ms_update_mmc_info(cid); + + /*Active flush */ + if (!malloc_trim(0)) + MS_DBG_ERR("malloc_trim is failed"); + + return err; +} + +#define _GETSYSTEMSTR(ID) dgettext("sys_string", (ID)) + +void update_lang(void) +{ + char *lang = NULL; + char *r = NULL; + + lang = vconf_get_str(VCONFKEY_LANGSET); + if (lang) { + setenv("LANG", lang, 1); + setenv("LC_MESSAGES", lang, 1); + r = setlocale(LC_ALL, ""); + if (r == NULL) { + r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET)); + MS_DBG_ERR("*****appcore setlocale=%s", r); + } + free(lang); + } +} + +int +ms_present_mmc_status(ms_sdcard_status_type_t status) +{ +#ifndef TIZEN_WEARABLE + int ret = NOTIFICATION_ERROR_NONE; + + update_lang(); + + if (status == MS_SDCARD_INSERTED) + ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_PREPARING_SD_CARD")); + else if (status == MS_SDCARD_REMOVED) + ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_SD_CARD_REMOVED")); + + if(ret != NOTIFICATION_ERROR_NONE) + return MS_MEDIA_ERR_INTERNAL; +#endif + return MS_MEDIA_ERR_NONE; +} + diff --git a/src/common/media-common-utils.c b/src/common/media-common-utils.c new file mode 100755 index 0000000..dd5d16d --- /dev/null +++ b/src/common/media-common-utils.c @@ -0,0 +1,352 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-utils.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief This file implements main database operation. + */ + +#include +#include +#include +#include +#include + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-dbg.h" +#include "media-common-utils.h" + +#ifdef FMS_PERF +#include +#define MILLION 1000000L +struct timeval g_mmc_start_time; +struct timeval g_mmc_end_time; +#endif + +#define MS_DRM_CONTENT_TYPE_LENGTH 100 + +#ifdef FMS_PERF +void +ms_check_start_time(struct timeval *start_time) +{ + gettimeofday(start_time, NULL); +} + +void +ms_check_end_time(struct timeval *end_time) +{ + gettimeofday(end_time, NULL); +} + +void +ms_check_time_diff(struct timeval *start_time, struct timeval *end_time) +{ + struct timeval time; + long difftime; + + time.tv_sec = end_time->tv_sec - start_time->tv_sec; + time.tv_usec = end_time->tv_usec - start_time->tv_usec; + difftime = MILLION * time.tv_sec + time.tv_usec; + MS_DBG("The function_to_time took %ld microseconds or %f seconds.", + difftime, difftime / (double)MILLION); +} +#endif + +bool +ms_is_mmc_inserted(void) +{ + int data = -1; + ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data); + if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) { + return false; + } else { + return true; + } +} + +ms_storage_type_t +ms_get_storage_type_by_full(const char *path) +{ + if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) { + return MS_STORAGE_INTERNAL; + } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) { + return MS_STORAGE_EXTERNAL; + } else + return MS_MEDIA_ERR_INVALID_PATH; +} + +int +ms_strappend(char *res, const int size, const char *pattern, + const char *str1, const char *str2) +{ + int len = 0; + int real_size = size - 1; + + if (!res ||!pattern || !str1 ||!str2 ) + return MS_MEDIA_ERR_INVALID_PARAMETER; + + if (real_size < (strlen(str1) + strlen(str2))) + return MS_MEDIA_ERR_INVALID_PARAMETER; + + len = snprintf(res, real_size, pattern, str1, str2); + if (len < 0) { + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + res[len] = '\0'; + + return MS_MEDIA_ERR_NONE; +} + +int +ms_strcopy(char *res, const int size, const char *pattern, const char *str1) +{ + int len = 0; + int real_size = size; + + if (!res || !pattern || !str1) { + MS_DBG_ERR("parameta is invalid"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + if (real_size < strlen(str1)) { + MS_DBG_ERR("size is wrong"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + len = snprintf(res, real_size, pattern, str1); + if (len < 0) { + MS_DBG_ERR("snprintf failed"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + res[len] = '\0'; + + return MS_MEDIA_ERR_NONE; +} + +bool +ms_config_get_int(const char *key, int *value) +{ + int err; + + if (!key || !value) { + MS_DBG_ERR("Arguments key or value is NULL"); + return false; + } + + err = vconf_get_int(key, value); + if (err == 0) + return true; + else if (err == -1) + return false; + else + MS_DBG_ERR("Unexpected error code: %d", err); + + return false; +} + +bool +ms_config_set_int(const char *key, int value) +{ + int err; + + if (!key) { + MS_DBG_ERR("Arguments key is NULL"); + return false; + } + + err = vconf_set_int(key, value); + if (err == 0) + return true; + else if (err == -1) + return false; + else + MS_DBG_ERR("Unexpected error code: %d", err); + + return false; +} + +bool +ms_config_get_str(const char *key, char *value) +{ + char *res; + if (!key || !value) { + MS_DBG_ERR("Arguments key or value is NULL"); + return false; + } + + res = vconf_get_str(key); + if (res) { + strncpy(value, res, strlen(res) + 1); + return true; + } + + return false; +} + +bool +ms_config_set_str(const char *key, const char *value) +{ + int err; + + if (!key || !value) { + MS_DBG_ERR("Arguments key or value is NULL"); + return false; + } + + err = vconf_set_str(key, value); + if (err == 0) + return true; + else + MS_DBG_ERR("fail to vconf_set_str %d", err); + + return false; +} + +bool +ms_config_get_bool(const char *key, int *value) +{ + int err; + + if (!key || !value) { + MS_DBG_ERR("Arguments key or value is NULL"); + return false; + } + + err = vconf_get_bool(key, value); + if (err == 0) + return true; + else if (err == -1) + return false; + else + MS_DBG_ERR("Unexpected error code: %d", err); + + return false; +} + +#define SYS_DBUS_NAME "ChangeState" +#define SYS_DBUS_PATH "/Org/Tizen/System/DeviceD/PowerOff" +#define SYS_DBUS_INTERFACE "org.tizen.system.deviced.PowerOff" +#define SYS_DBUS_MATCH_RULE "type='signal',interface='org.tizen.system.deviced.PowerOff'" + +typedef struct pwoff_callback_data{ + power_off_cb user_callback; + void *user_data; +} pwoff_callback_data; + +DBusHandlerResult +__get_dbus_message(DBusMessage *message, void *user_cb, void *userdata) +{ + MS_DBG(""); + + /* A Ping signal on the com.burtonini.dbus.Signal interface */ + if (dbus_message_is_signal (message, SYS_DBUS_INTERFACE, SYS_DBUS_NAME)) { + int current_type = DBUS_TYPE_INVALID; + DBusError error; + DBusMessageIter read_iter; + DBusBasicValue value; + power_off_cb cb_func = (power_off_cb)user_cb; + + dbus_error_init (&error); + + /* get data from dbus message */ + dbus_message_iter_init (message, &read_iter); + while ((current_type = dbus_message_iter_get_arg_type (&read_iter)) != DBUS_TYPE_INVALID){ + dbus_message_iter_get_basic (&read_iter, &value); + switch(current_type) { + case DBUS_TYPE_INT32: + MS_DBG_WARN("value[%d]", value.i32); + break; + default: + MS_DBG_ERR("current type : %d", current_type); + break; + } + + if (value.i32 == 2 || value.i32 == 3) { + MS_DBG_WARN("PREPARE POWER OFF"); + break; + } + + dbus_message_iter_next (&read_iter); + } + + if (value.i32 == 2 || value.i32 == 3) + cb_func(userdata); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static DBusHandlerResult +__sysman_message_filter (DBusConnection *connection, DBusMessage *message, void *user_data) +{ + DBusHandlerResult ret; + + pwoff_callback_data *cb_data = (pwoff_callback_data *)user_data; + + MS_DBG(""); + + ret = __get_dbus_message(message, cb_data->user_callback, cb_data->user_data); + + MS_DBG(""); + + return ret; +} + +int ms_add_poweoff_event_receiver(power_off_cb user_callback, void *user_data) +{ + DBusConnection *dbus; + DBusError error; + pwoff_callback_data *cb_data = NULL; + + /*add noti receiver for power off*/ + dbus_error_init (&error); + + dbus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (!dbus) { + MS_DBG_ERR ("Failed to connect to the D-BUS daemon: %s", error.message); + return MS_MEDIA_ERR_DBUS_GET; + } + + dbus_connection_setup_with_g_main (dbus, NULL); + + cb_data = malloc(sizeof(pwoff_callback_data)); + cb_data->user_callback = user_callback; + cb_data->user_data = user_data; + + /* listening to messages from all objects as no path is specified */ + dbus_bus_add_match (dbus, SYS_DBUS_MATCH_RULE, &error); + if( !dbus_connection_add_filter (dbus, __sysman_message_filter, cb_data, NULL)) { + dbus_bus_remove_match (dbus, SYS_DBUS_MATCH_RULE, NULL); + return MS_MEDIA_ERR_DBUS_ADD_FILTER; + MS_DBG_ERR(""); + } + + return MS_MEDIA_ERR_NONE; +} \ No newline at end of file diff --git a/src/mediadb-update.c b/src/mediadb-update.c new file mode 100644 index 0000000..bf84e5f --- /dev/null +++ b/src/mediadb-update.c @@ -0,0 +1,163 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include + + +#include +#include "media-util.h" + +GMainLoop * mainloop = NULL; + +void callback(media_request_result_s * result, void *user_data) +{ + printf("db updating done\n"); + + g_main_loop_quit(mainloop); +} + +void print_help() +{ + printf("=======================================================================================\n"); + printf("\n"); + printf("db-update [option] \n"); + printf("\n"); + printf("[option]\n"); + printf(" -r : [only directory] update all directory recursivly under \n"); + printf("\n"); + printf("db-update --help for check this messages.\n"); + printf("\n"); + printf("A file or directory must exists under /opt/usr/media or /opt/storage/sdcard.\n"); + printf("Using /opt/storage/sdcard is allowed SD card is mounted.\n"); + printf("\n"); + printf("=======================================================================================\n"); +} + +int dir_scan_non_recursive(char *path) +{ + return media_directory_scanning_async(path, FALSE, callback, NULL); +} + +int dir_scan_recursive(char *path) +{ + return media_directory_scanning_async(path, TRUE, callback, NULL); +} + +typedef enum { + DIRECTORY_OK, + FILE_OK, + NOT_OK, +}check_result; + +check_result check_path(char *path) +{ + struct stat buf; + DIR *dp = NULL; + + /*check the path directory or file*/ + if (stat(path, &buf) == 0) { + if (S_ISDIR(buf.st_mode)) { + printf("This is directory\n"); + return DIRECTORY_OK; + } else { + dp = opendir(path); + if (dp == NULL) { + /*if openning directory is failed, check it exists. */ + if (errno == ENOENT) { + /* this directory is deleted */ + return DIRECTORY_OK; + } + } else { + closedir(dp); + } + print_help(); + return NOT_OK; + } + } else { + printf("stat error : %s", strerror(errno)); + } + + return NOT_OK; +} + +int main(int argc, char **argv) +{ + int ret; + char *argv1 = NULL; + char *argv2 = NULL; + + if (argc > 3 ||argc < 2) { + print_help(); + exit(1); + } + + argv1 = strdup(argv[1]); + + mainloop = g_main_loop_new(NULL, FALSE); + + if (argc == 2) { + if (strcmp(argv1 , "--help") == 0) { + print_help(); + exit(1); + } + + if (check_path(argv1) == DIRECTORY_OK) { + ret = dir_scan_non_recursive(argv1); + if (ret != 0) { + printf("error : %d\n", ret); + exit(1); + } + } else { + printf("[%d]invalid path\n", __LINE__); + print_help(); + exit(1); + } + } else if (argc == 3) { + argv2 = strdup(argv[2]); + if (strcmp(argv1, "-r") == 0) { + if (check_path(argv2) == DIRECTORY_OK) { + ret = dir_scan_recursive(argv2); + if (ret != 0) { + printf("error : %d\n", ret); + exit(1); + } + } else { + printf("[%d]invalid path\n", __LINE__); + print_help(); + exit(1); + } + } else { + printf("[%d] invalide option\n", __LINE__); + print_help(); + exit(1); + } + } + + g_main_loop_run(mainloop); + + exit(0); +} diff --git a/src/scanner/include/media-scanner-db-svc.h b/src/scanner/include/media-scanner-db-svc.h new file mode 100755 index 0000000..0553ff1 --- /dev/null +++ b/src/scanner/include/media-scanner-db-svc.h @@ -0,0 +1,135 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-db-svc.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_SCANNER_DB_SVC_H_ +#define _MEDIA_SCANNER_DB_SVC_H_ + +#include "media-common-types.h" + +typedef int (*CONNECT)(void**, char **); +typedef int (*DISCONNECT)(void*, char **); +typedef int (*CHECK_ITEM_EXIST)(void*, const char*, bool*, char **); +typedef int (*INSERT_ITEM_BEGIN)(void*, int, int, int, char **); +typedef int (*INSERT_ITEM_END)(void*, char **); +typedef int (*INSERT_ITEM)(void*, const char*, int, char **); +typedef int (*INSERT_ITEM_IMMEDIATELY)(void*, const char*, int, char **); +typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, char **); +typedef int (*SET_ITEM_VALIDITY_BEGIN)(void*, int, char **); +typedef int (*SET_ITEM_VALIDITY_END)(void*, char **); +typedef int (*SET_ITEM_VALIDITY)(void*, const char*, int, int, char **); +typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, int, char **); +typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, int, char **); +typedef int (*UPDATE_BEGIN)(void); +typedef int (*UPDATE_END)(void); +typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, int, int, char**); +typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char*, char**); +typedef int (*INSERT_BURST_ITEM)(void *, const char *, int , char **); +typedef int (*SEND_DIR_UPDATE_NOTI)(void *, const char *, char **); +typedef int (*COUNT_DELETE_ITEMS_IN_FOLDER)(void *, const char *, int *, char **); +typedef int (*DELETE_ITEM)(void *, const char *, char **); +typedef int (*GET_FOLDER_LIST)(void *, char*, char ***, int **, int **, int *, char **); +typedef int (*UPDATE_FOLDER_TIME)(void *, const char *, char **); + +int +msc_load_functions(void); + +void +msc_unload_functions(void); + +int +msc_connect_db(void ***handle); + +int +msc_disconnect_db(void ***handle); + +int +msc_validate_item(void **handle, const char *path); + +int +msc_insert_item_batch(void **handle, const char *path); + +int +msc_insert_burst_item(void **handle, const char *path); + +bool +msc_delete_all_items(void **handle, ms_storage_type_t store_type); + +int +msc_validaty_change_all_items(void **handle, ms_storage_type_t store_type, bool validity); + +bool +msc_delete_invalid_items(void **handle, ms_storage_type_t store_type); + +int +msc_set_folder_validity(void **handle, const char *path, int validity, int recursive); + +int +msc_delete_invalid_items_in_folder(void **handle, const char*path); + +int +msc_send_dir_update_noti(void **handle, const char*path); + +int +msc_count_delete_items_in_folder(void **handle, const char*path, int *count); + +typedef struct msc_dir_info_s { + char *dir_path; + int modified_time; + int item_num; +} msc_dir_info_s; + +int +msc_get_folder_list(void **handle, char* start_path, GArray **dir_array); + +int +msc_update_folder_time(void **handle, char *folder_path); + +int +msc_insert_item_immediately(void **handle, const char *path); +/**************************************************************************************************** +FOR BULK COMMIT +*****************************************************************************************************/ +typedef enum { + MS_NOTI_DISABLE = 0, /**< Stored only in phone */ + MS_NOTI_ENABLE = 1, /**< Stored only in MMC */ +} ms_noti_status_e; + +void +msc_register_start(void **handle, ms_noti_status_e noti_status, int pid); + +void +msc_register_end(void **handle); + +void +msc_validate_start(void **handle); + +void +msc_validate_end(void **handle); + +#endif /*_MEDIA_SCANNER_DB_SVC_H_*/ diff --git a/src/scanner/include/media-scanner-dbg.h b/src/scanner/include/media-scanner-dbg.h new file mode 100755 index 0000000..0d9cc8a --- /dev/null +++ b/src/scanner/include/media-scanner-dbg.h @@ -0,0 +1,65 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-scanner-dbg.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#ifndef _MEDIA_SCANNER_DBG_H_ +#define _MEDIA_SCANNER_DBG_H_ +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "MEDIA_SCANNER" + +#define MSC_DBG_SLOG(fmt, args...) do{ if (true) { \ + SECURE_LOGD(fmt "\n", ##args); \ + }} while(false) + +#define MSC_DBG(fmt, args...) do{ if (true) { \ + LOGD(fmt "\n", ##args); \ + }} while(false) + +#define MSC_DBG_INFO(fmt, args...) do{ if (true) { \ + LOGI(fmt "\n", ##args); \ + }} while(false) + +#define MSC_DBG_ERR(fmt, args...) do{ if (true) { \ + LOGE(fmt "\n", ##args); \ + }} while(false) + +#define MSC_DBG_WAN(fmt, args...) do{ if (true) { \ + LOGW(fmt "\n", ##args); \ + }} while(false) + +/** + * @} + */ +#endif /*_MEDIA_SCANNER_DBG_H_*/ + diff --git a/src/scanner/include/media-scanner-scan.h b/src/scanner/include/media-scanner-scan.h new file mode 100755 index 0000000..49e32c9 --- /dev/null +++ b/src/scanner/include/media-scanner-scan.h @@ -0,0 +1,43 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-scan.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_SCANNER_SCAN_H_ +#define _MEDIA_SCANNER_SCAN_H_ + +gboolean msc_directory_scan_thread(void *data); + +gboolean msc_register_thread(void *data); + +gboolean msc_storage_scan_thread(void *data); + +int msc_check_remain_task(void); + +ms_db_status_type_t msc_check_scanning_status(void); + +#endif /*_MEDIA_SCANNER_SCAN_H_*/ \ No newline at end of file diff --git a/src/scanner/include/media-scanner-socket.h b/src/scanner/include/media-scanner-socket.h new file mode 100755 index 0000000..4d026b3 --- /dev/null +++ b/src/scanner/include/media-scanner-socket.h @@ -0,0 +1,42 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-thumb.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_SCANNER_SOCKET_H_ +#define _MEDIA_SCANNER_SOCKET_H_ + +#include "media-common-types.h" +#include "media-server-ipc.h" + +gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data); + +int msc_send_ready(void); + +int msc_send_result(int result, ms_comm_msg_s *scan_data); + +#endif /*_MEDIA_SCANNER_SOCKET_H_*/ diff --git a/src/scanner/media-scanner-db-svc.c b/src/scanner/media-scanner-db-svc.c new file mode 100755 index 0000000..fc3209b --- /dev/null +++ b/src/scanner/media-scanner-db-svc.c @@ -0,0 +1,705 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-db-svc.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief This file implements main database operation. + */ + +#include + +#include "media-util.h" + +#include "media-common-utils.h" +#include "media-scanner-dbg.h" +#include "media-scanner-db-svc.h" + +#define CONFIG_PATH "/usr/etc/media-server-plugin" +#define EXT ".so" +#define EXT_LEN 3 +#define MSC_REGISTER_COUNT 100 /*For bundle commit*/ +#define MSC_VALID_COUNT 100 /*For bundle commit*/ + +GMutex * db_mutex; +GArray *so_array; +void ***func_array; +int lib_num; +void **scan_func_handle = NULL; /*dlopen handel*/ +extern int insert_count; + +enum func_list { + eCONNECT, + eDISCONNECT, + eEXIST, + eINSERT_BEGIN, + eINSERT_END, + eINSERT_BATCH, + eINSERT_ITEM_IMMEDIATELY, + eSET_ALL_VALIDITY, + eSET_VALIDITY_BEGIN, + eSET_VALIDITY_END, + eSET_VALIDITY, + eDELETE_ALL, + eDELETE_INVALID_ITEMS, + eUPDATE_BEGIN, + eUPDATE_END, + eSET_FOLDER_VALIDITY, + eDELETE_FOLDER, + eINSERT_BURST, + eSEND_DIR_UPDATE_NOTI, + eCOUNT_DELETE_ITEMS_IN_FOLDER, + eDELETE_ITEM, + eGET_FOLDER_LIST, + eUPDATE_FOLDER_TIME, + eFUNC_MAX +}; + +static int +_msc_token_data(char *buf, char **name) +{ + int len; + char* pos = NULL; + + pos = strstr(buf, EXT); + if (pos == NULL) { + MSC_DBG_ERR("This is not shared object library."); + name = NULL; + return -1; + } else { + len = pos - buf + EXT_LEN; + *name = strndup(buf, len); + } + + return 0; +} + +static bool +_msc_load_config() +{ + int ret; + FILE *fp; + char *so_name = NULL; + char buf[256] = {0}; + + fp = fopen(CONFIG_PATH, "rt"); + if (fp == NULL) { + MSC_DBG_ERR("fp is NULL"); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + while(1) { + if (fgets(buf, 256, fp) == NULL) + break; + + ret = _msc_token_data(buf, &so_name); + if (ret == 0) { + /*add to array*/ + g_array_append_val(so_array, so_name); + so_name = NULL; + } + } + + fclose(fp); + + return MS_MEDIA_ERR_NONE; +} + +int +msc_load_functions(void) +{ + int lib_index = 0, func_index; + char func_list[eFUNC_MAX][40] = { + "connect_db", + "disconnect_db", + "check_item_exist", + "insert_item_begin", + "insert_item_end", + "insert_item", + "insert_item_immediately", + "set_all_storage_items_validity", + "set_item_validity_begin", + "set_item_validity_end", + "set_item_validity", + "delete_all_items_in_storage", + "delete_all_invalid_items_in_storage", + "update_begin", + "update_end", + "set_folder_item_validity", + "delete_all_invalid_items_in_folder", + "insert_burst_item", + "send_dir_update_noti", + "count_delete_items_in_folder", + "delete_item", + "get_folder_list", + "update_folder_time", + }; + /*init array for adding name of so*/ + so_array = g_array_new(FALSE, FALSE, sizeof(char*)); + + /*load information of so*/ + _msc_load_config(); + + if(so_array->len == 0) { + MSC_DBG_ERR("There is no information for functions"); + return MS_MEDIA_ERR_DYNAMIC_LINK; + } + + /*the number of functions*/ + lib_num = so_array->len; + + MSC_DBG_SLOG("The number of information of so : %d", lib_num); + MS_MALLOC(scan_func_handle, sizeof(void*) * lib_num); + if (scan_func_handle == NULL) { + MSC_DBG_ERR("malloc failed"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + + while(lib_index < lib_num) { + /*get handle*/ + MSC_DBG_SLOG("[name of so : %s]", g_array_index(so_array, char*, lib_index)); + scan_func_handle[lib_index] = dlopen(g_array_index(so_array, char*, lib_index), RTLD_LAZY); + if (!scan_func_handle[lib_index]) { + MSC_DBG_ERR("%s", dlerror()); + MS_SAFE_FREE(scan_func_handle); + return MS_MEDIA_ERR_DYNAMIC_LINK; + } + lib_index++; + } + + dlerror(); /* Clear any existing error */ + + /*allocate for array of functions*/ + MS_MALLOC(func_array, sizeof(void*) * lib_num); + if (func_array == NULL) { + MSC_DBG_ERR("malloc failed"); + MS_SAFE_FREE(scan_func_handle); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + + for(lib_index = 0 ; lib_index < lib_num; lib_index ++) { + MS_MALLOC(func_array[lib_index], sizeof(void*) * eFUNC_MAX); + if (func_array[lib_index] == NULL) { + int index; + + for (index = 0; index < lib_index; index ++) { + MS_SAFE_FREE(func_array[index]); + } + MS_SAFE_FREE(func_array); + MS_SAFE_FREE(scan_func_handle); + + MSC_DBG_ERR("malloc failed"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + } + + /*add functions to array */ + for (lib_index = 0; lib_index < lib_num; lib_index++) { + for (func_index = 0; func_index < eFUNC_MAX ; func_index++) { + func_array[lib_index][func_index] = dlsym(scan_func_handle[lib_index], func_list[func_index]); + if (func_array[lib_index][func_index] == NULL) { + int index; + + for (index = 0; index < lib_index; index ++) { + MS_SAFE_FREE(func_array[index]); + } + MS_SAFE_FREE(func_array); + MS_SAFE_FREE(scan_func_handle); + + MSC_DBG_ERR("dlsym failed"); + return MS_MEDIA_ERR_DYNAMIC_LINK; + } + } + } + + return MS_MEDIA_ERR_NONE; +} + +void +msc_unload_functions(void) +{ + int lib_index; + + for (lib_index = 0; lib_index < lib_num; lib_index ++) + dlclose(scan_func_handle[lib_index]); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + if (func_array[lib_index]) { + MS_SAFE_FREE(func_array[lib_index]); + } + } + + MS_SAFE_FREE (func_array); + MS_SAFE_FREE (scan_func_handle); + if (so_array) g_array_free(so_array, TRUE); +} + +int +msc_connect_db(void ***handle) +{ + int lib_index; + int ret; + char * err_msg = NULL; + + /*Lock mutex for openning db*/ + g_mutex_lock(db_mutex); + + MS_MALLOC(*handle, sizeof (void*) * lib_num); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + g_mutex_unlock(db_mutex); + + return MS_MEDIA_ERR_DB_CONNECT_FAIL; + } + } + + MSC_DBG_INFO("connect Media DB"); + + g_mutex_unlock(db_mutex); + + return MS_MEDIA_ERR_NONE; +} + +int +msc_disconnect_db(void ***handle) +{ + int lib_index; + int ret; + char * err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((DISCONNECT)func_array[lib_index][eDISCONNECT])((*handle)[lib_index], &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_DB_DISCONNECT_FAIL; + } + } + + MS_SAFE_FREE(*handle); + + MSC_DBG_INFO("Disconnect Media DB"); + + return MS_MEDIA_ERR_NONE; +} + +int +msc_validate_item(void **handle, const char *path) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + bool modified = FALSE; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + /*check exist in Media DB, If file is not exist, insert data in DB. */ + ret = ((CHECK_ITEM_EXIST)func_array[lib_index][eEXIST])(handle[lib_index], path, &modified, &err_msg); /*dlopen*/ + if (ret != 0) { + MS_SAFE_FREE(err_msg); + ret = msc_insert_item_batch(handle, path); + if (ret != 0) { + res = MS_MEDIA_ERR_DB_INSERT_FAIL; + } else { + insert_count++; + } + } else { + if (modified == FALSE) { + /*if meta data of file exist, change valid field to "1" */ + ret = ((SET_ITEM_VALIDITY)func_array[lib_index][eSET_VALIDITY])(handle[lib_index], path, true, true, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + } else { + /* the file has same name but it is changed, so we have to update DB */ + ret = ((DELETE_ITEM)func_array[lib_index][eDELETE_ITEM])(handle[lib_index], path, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_DELETE_FAIL; + } else { + ret = msc_insert_item_immediately(handle, path); + if (ret != 0) { + res = MS_MEDIA_ERR_DB_INSERT_FAIL; + } else { + insert_count++; + } + } + } + } + } + + return res; +} + +int +msc_validaty_change_all_items(void **handle, ms_storage_type_t store_type, bool validity) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, validity, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + } + + return res; +} + +int +msc_insert_item_batch(void **handle, const char *path) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + ms_storage_type_t storage_type; + + storage_type = ms_get_storage_type_by_full(path); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_INSERT_FAIL; + } + } + + return res; +} + +int +msc_insert_item_immediately(void **handle, const char *path) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + ms_storage_type_t storage_type; + + storage_type = ms_get_storage_type_by_full(path); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((INSERT_ITEM_IMMEDIATELY)func_array[lib_index][eINSERT_ITEM_IMMEDIATELY])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_INSERT_FAIL; + } + } + + return res; +} + +int +msc_insert_burst_item(void **handle, const char *path) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + ms_storage_type_t storage_type; + + storage_type = ms_get_storage_type_by_full(path); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((INSERT_BURST_ITEM)func_array[lib_index][eINSERT_BURST])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_INSERT_FAIL; + } + } + + return res; +} + +bool +msc_delete_all_items(void **handle, ms_storage_type_t store_type) +{ + int lib_index; + int ret = 0; + char *err_msg = NULL; + + /* To reset media db when differnet mmc is inserted. */ + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((DELETE_ALL_ITEMS_IN_STORAGE)func_array[lib_index][eDELETE_ALL])(handle[lib_index], store_type, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return false; + } + } + + return true; +} + +bool +msc_delete_invalid_items(void **handle, ms_storage_type_t store_type) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((DELETE_ALL_INVALID_ITMES_IN_STORAGE)func_array[lib_index][eDELETE_INVALID_ITEMS])(handle[lib_index], store_type, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return false; + } + } + + return true; +} + +int +msc_set_folder_validity(void **handle, const char *path, int validity, int recursive) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((SET_FOLDER_ITEM_VALIDITY)func_array[lib_index][eSET_FOLDER_VALIDITY])(handle[lib_index], path, validity, recursive,&err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + } + + return MS_MEDIA_ERR_NONE; +} + +int +msc_delete_invalid_items_in_folder(void **handle, const char*path) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[lib_index][eDELETE_FOLDER])(handle[lib_index], path, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + } + + return MS_MEDIA_ERR_NONE; +} + +int +msc_send_dir_update_noti(void **handle, const char*path) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((SEND_DIR_UPDATE_NOTI)func_array[lib_index][eSEND_DIR_UPDATE_NOTI])(handle[lib_index], path, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_SEND_NOTI_FAIL; + } + } + + return MS_MEDIA_ERR_NONE; +} + +int +msc_count_delete_items_in_folder(void **handle, const char*path, int *count) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((COUNT_DELETE_ITEMS_IN_FOLDER)func_array[lib_index][eCOUNT_DELETE_ITEMS_IN_FOLDER])(handle[lib_index], path, count, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_INTERNAL; + } + } + + return MS_MEDIA_ERR_NONE; +} + +int +msc_get_folder_list(void **handle, char* start_path, GArray **dir_array) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + char **folder_list = NULL; + int *modified_time_list = NULL; + int *item_num_list = NULL; + int count = 0; + int i = 0; + + msc_dir_info_s* dir_info = NULL; + MSC_DBG_ERR("start path: %s", start_path); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((GET_FOLDER_LIST)func_array[lib_index][eGET_FOLDER_LIST])(handle[lib_index], start_path, &folder_list, &modified_time_list, &item_num_list, &count, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_INTERNAL; + } + } + + MSC_DBG_ERR("OK"); + + *dir_array = g_array_new(FALSE, FALSE, sizeof(msc_dir_info_s*)); + if (count != 0) { + for(i = 0; i < count; i ++) { + dir_info = malloc(sizeof(msc_dir_info_s)); + dir_info->dir_path = strdup(folder_list[i]); + dir_info->modified_time = modified_time_list[i]; + dir_info->item_num = item_num_list[i]; + g_array_append_val(*dir_array, dir_info); + MS_SAFE_FREE(folder_list[i]); +// MSC_DBG("%d get path : %s, %d", i, dir_info->dir_path, dir_info->modified_time); + } + } + + MS_SAFE_FREE(folder_list); + MS_SAFE_FREE(modified_time_list); + + return MS_MEDIA_ERR_NONE; +} + +int +msc_update_folder_time(void **handle, char *folder_path) +{ + int lib_index; + int ret; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((UPDATE_FOLDER_TIME)func_array[lib_index][eUPDATE_FOLDER_TIME])(handle[lib_index], folder_path, &err_msg); /*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_INTERNAL; + } + } + + return MS_MEDIA_ERR_NONE; +} + +/**************************************************************************************************** +FOR BULK COMMIT +*****************************************************************************************************/ + +void +msc_register_start(void **handle, ms_noti_status_e noti_status, int pid) +{ + int lib_index; + int ret = 0; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((INSERT_ITEM_BEGIN)func_array[lib_index][eINSERT_BEGIN])(handle[lib_index], MSC_REGISTER_COUNT, noti_status, pid, &err_msg);/*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + } + } +} + +void +msc_register_end(void **handle) +{ + int lib_index; + int ret = 0; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((INSERT_ITEM_END)func_array[lib_index][eINSERT_END])(handle[lib_index], &err_msg);/*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + } + } + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((UPDATE_END)func_array[lib_index][eUPDATE_END])();/*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + } + } +} + +void +msc_validate_start(void **handle) +{ + int lib_index; + int ret = 0; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((SET_ITEM_VALIDITY_BEGIN)func_array[lib_index][eSET_VALIDITY_BEGIN])(handle[lib_index], MSC_VALID_COUNT, &err_msg);/*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + } + } +} + +void +msc_validate_end(void **handle) +{ + int lib_index; + int ret = 0; + char *err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((SET_ITEM_VALIDITY_END)func_array[lib_index][eSET_VALIDITY_END])(handle[lib_index], &err_msg);/*dlopen*/ + if (ret != 0) { + MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + } + } +} + diff --git a/src/scanner/media-scanner-scan.c b/src/scanner/media-scanner-scan.c new file mode 100755 index 0000000..ddf3922 --- /dev/null +++ b/src/scanner/media-scanner-scan.c @@ -0,0 +1,1127 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-scan.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#include +#include +#include +#include +#include +#ifdef TIZEN_WEARABLE +#include +#else +#include +#endif +#include + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-utils.h" +#include "media-common-external-storage.h" +#include "media-scanner-dbg.h" +#include "media-scanner-db-svc.h" +#include "media-scanner-socket.h" +#include "media-scanner-scan.h" + +typedef struct msc_scan_data { + char *name; + struct msc_scan_data *next; +} msc_scan_data; + +int mmc_state = 0; +bool power_off; +GAsyncQueue * storage_queue; +GAsyncQueue *scan_queue; +GAsyncQueue *reg_queue; +int insert_count; + +#ifdef FMS_PERF +extern struct timeval g_mmc_start_time; +extern struct timeval g_mmc_end_time; +#endif + +static int __msc_set_power_mode(ms_db_status_type_t status); +static int __msc_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type); +static int __msc_check_stop_status(ms_storage_type_t storage_type); +static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t storage_type, int scan_type); +static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data); +static int __msc_check_file_path(const char *file_path); +static int __msc_make_file_list(char *file_path, GArray **path_array); +static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array); +static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data); +static int __msc_clear_file_list(GArray *path_array); +static bool __msc_check_scan_ignore(char * path); +static bool __msc_is_valid_path(const char *path); +static void __msc_check_dir_path(char *dir_path); +static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data); +static void __msc_bacth_commit_enable(void* handle, bool ins_status, bool valid_status, bool noti_enable, int pid); +static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status); + +static int __msc_set_power_mode(ms_db_status_type_t status) +{ + int res = MS_MEDIA_ERR_NONE; + int err; + + switch (status) { + case MS_DB_UPDATING: +#ifdef TIZEN_WEARABLE + err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0); +#else + err = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0); +#endif + if (err != 0) + res = MS_MEDIA_ERR_INTERNAL; + break; + case MS_DB_UPDATED: +#ifdef TIZEN_WEARABLE + err = display_unlock_state(LCD_OFF, STAY_CUR_STATE); +#else + err = pm_unlock_state(LCD_OFF, STAY_CUR_STATE); +#endif + if (err != 0) + res = MS_MEDIA_ERR_INTERNAL; + break; + default: + MSC_DBG_ERR("Unacceptable type : %d", status); + break; + } + + return res; +} + +static int __msc_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type) +{ + int res = MS_MEDIA_ERR_NONE; + int err = 0; + + if (status == MS_DB_UPDATING) { + if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) { + res = MS_MEDIA_ERR_VCONF_SET_FAIL; + MSC_DBG_ERR("ms_config_set_int failed"); + } + + if (storage_type == MS_STORAGE_EXTERNAL) { + if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) { + res = MS_MEDIA_ERR_VCONF_SET_FAIL; + MSC_DBG_ERR("ms_config_set_int failed"); + } + } + } else if (status == MS_DB_UPDATED) { + if(!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) { + res = MS_MEDIA_ERR_VCONF_SET_FAIL; + MSC_DBG_ERR("ms_config_set_int failed"); + } + + if (storage_type == MS_STORAGE_EXTERNAL) { + if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) { + res = MS_MEDIA_ERR_VCONF_SET_FAIL; + MSC_DBG_ERR("ms_config_set_int failed"); + } + } + } + + err = __msc_set_power_mode(status); + if (err != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("__msc_set_power_mode fail"); + res = err; + } + + return res; +} + +static bool __msc_check_scan_ignore(char * path) +{ + DIR *dp = NULL; + struct dirent entry; + struct dirent *result; + char *ignore_path = ".scan_ignore"; + + if(strstr(path, "/.")) + { + MSC_DBG_ERR("hidden path"); + return true;; + } + + dp = opendir(path); + if (dp == NULL) { + MSC_DBG_ERR("%s folder opendir fails", path); + return true; + } + + while (!readdir_r(dp, &entry, &result)) { + if (result == NULL) + break; + + if (strcmp(entry.d_name, ignore_path) == 0) { + closedir(dp); + return true; + } + } + + closedir(dp); + return false; +} + +static int __msc_check_stop_status(ms_storage_type_t storage_type) +{ + int ret = MS_MEDIA_ERR_NONE; + + /*check poweroff status*/ + if (power_off) { + MSC_DBG_ERR("Power off"); + ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP; + } + + /*check SD card in out */ + if ((mmc_state != VCONFKEY_SYSMAN_MMC_MOUNTED) && (storage_type == MS_STORAGE_EXTERNAL)) { + MSC_DBG_ERR("Directory scanning is stopped"); + ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP; + } + + return ret; +} + +static void __msc_check_dir_path(char *dir_path) +{ + /* need implementation */ + /* if dir_path is not NULL terminated, this function will occure crash */ + int len = strlen(dir_path); + + if (dir_path[len -1] == '/') + dir_path[len -1] = '\0'; +} + +static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t storage_type, int scan_type) +{ + DIR *dp = NULL; + GArray *dir_array = NULL; + struct dirent entry; + struct dirent *result = NULL; + int ret = MS_MEDIA_ERR_NONE; + char *new_path = NULL; + char *current_path = NULL; + char path[MS_FILE_PATH_LEN_MAX] = { 0 }; + int (*scan_function)(void **, const char*) = NULL; + + /* make new array for storing directory */ + dir_array = g_array_new (FALSE, FALSE, sizeof (char*)); + if (dir_array == NULL){ + MSC_DBG_ERR("g_array_new failed"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + /* add first direcotiry to directory array */ + g_array_append_val (dir_array, start_path); + + scan_function = (scan_type == MS_MSG_STORAGE_ALL) ? msc_insert_item_batch : msc_validate_item; + + /*start db update. the number of element in the array , db update is complete.*/ + while (dir_array->len != 0) { + /*check poweroff status*/ + ret = __msc_check_stop_status(storage_type); + if (ret != MS_MEDIA_ERR_NONE) { + goto STOP_SCAN; + } + /* get the current path from directory array */ + current_path = g_array_index(dir_array , char*, 0); + g_array_remove_index (dir_array, 0); +// MSC_DBG_SLOG("%d", dir_array->len); + + if (__msc_check_scan_ignore(current_path)) { + MSC_DBG_ERR("%s is ignore", current_path); + MS_SAFE_FREE(current_path); + continue; + } + + dp = opendir(current_path); + if (dp != NULL) { + while (!readdir_r(dp, &entry, &result)) { + /*check poweroff status*/ + ret = __msc_check_stop_status(storage_type); + if (ret != MS_MEDIA_ERR_NONE) { + goto STOP_SCAN; + } + + if (result == NULL) + break; + + if (entry.d_name[0] == '.') + continue; + + if (entry.d_type & DT_REG) { + if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("ms_strappend failed"); + continue; + } + /* insert into media DB */ + if (scan_function(handle,path) != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("failed to update db : %d", scan_type); + continue; + } + } else if (entry.d_type & DT_DIR) { + if (scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) { + /* this request is recursive scanning */ + if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("ms_strappend failed"); + continue; + } + /* add new directory to dir_array */ + new_path = strdup(path); + g_array_append_val (dir_array, new_path); + } else { + /* this request is recursive scanning */ + /* don't add new directory to dir_array */ + } + } + } + /* update modified time of directory */ + msc_update_folder_time(handle, current_path); + } else { + MSC_DBG_ERR("%s folder opendir fails", current_path); + } + if (dp) closedir(dp); + dp = NULL; + MS_SAFE_FREE(current_path); + } /*db update while */ +STOP_SCAN: + if (dp) closedir(dp); + + __msc_clear_file_list(dir_array); + + if (ret != MS_MEDIA_ERR_NONE) MSC_DBG_INFO("ret : %d", ret); + + return ret; +} + +static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data) +{ + int scan_type; + int err = MS_MEDIA_ERR_NONE; + char *start_path = NULL; + ms_storage_type_t storage_type; + + storage_type = ms_get_storage_type_by_full(scan_data->msg); + scan_type = scan_data->msg_type; + start_path = strndup(scan_data->msg, scan_data->msg_size); + + /*if scan type is not MS_SCAN_NONE, check data in db. */ + if (scan_type != MS_MSG_STORAGE_INVALID) { + MSC_DBG_INFO("INSERT"); + + err = __msc_dir_scan(handle, start_path, storage_type, scan_type); + if (err != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("error : %d", err); + } + } else if (scan_type == MS_MSG_STORAGE_INVALID) { + MSC_DBG_INFO("INVALID"); + + err = msc_set_folder_validity(handle, start_path, false, true); + if (err != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("error : %d", err); + } + + MS_SAFE_FREE(start_path); + } + + sync(); + + return err; +} + +gboolean msc_directory_scan_thread(void *data) +{ + ms_comm_msg_s *scan_data = NULL; + int err; + int ret; + void **handle = NULL; + int scan_type; + char *noti_path = NULL; + + while (1) { + scan_data = g_async_queue_pop(scan_queue); + if (scan_data->pid == POWEROFF) { + MSC_DBG_ERR("power off"); + goto _POWEROFF; + } + + MSC_DBG_INFO("DIRECTORY SCAN START [%s]", scan_data->msg); + + /*connect to media db, if conneting is failed, db updating is stopped*/ + err = msc_connect_db(&handle); + if (err != MS_MEDIA_ERR_NONE) + continue; + + scan_type = scan_data->msg_type; + + if (scan_type != MS_MSG_DIRECTORY_SCANNING + && scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) { + MSC_DBG_ERR("Invalid request"); + ret = MS_MEDIA_ERR_INVALID_PARAMETER; + goto NEXT; + } + + __msc_check_dir_path(scan_data->msg); + + /*change validity before scanning*/ + if (scan_type == MS_MSG_DIRECTORY_SCANNING) + err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_RECURSIVE); + else + err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE); + if (err != MS_MEDIA_ERR_NONE) + MSC_DBG_ERR("error : %d", err); + + /*call for bundle commit*/ + __msc_bacth_commit_enable(handle, TRUE, TRUE, MS_NOTI_DISABLE, 0); + + /*insert data into media db */ + ret = __msc_db_update(handle, scan_data); + + /*call for bundle commit*/ + __msc_bacth_commit_disable(handle, TRUE, TRUE); + + if (ret == MS_MEDIA_ERR_NONE) { + MSC_DBG_INFO("working normally"); + int count = 0; + + noti_path = strndup(scan_data->msg, scan_data->msg_size); + msc_count_delete_items_in_folder(handle, noti_path, &count); + + MSC_DBG_SLOG("delete count %d", count); + MSC_DBG_SLOG("insert count %d", insert_count); + + msc_delete_invalid_items_in_folder(handle, scan_data->msg); + + if ( !(count == 0 && insert_count == 0)) { + msc_send_dir_update_noti(handle, noti_path); + } + MS_SAFE_FREE(noti_path); + } + + insert_count = 0; + + if (power_off) { + MSC_DBG_WAN("power off"); + goto _POWEROFF; + } + + /*disconnect form media db*/ + if (handle) msc_disconnect_db(&handle); +NEXT: + /*Active flush */ + malloc_trim(0); + + msc_send_result(ret, scan_data); + + MS_SAFE_FREE(scan_data); + + MSC_DBG_INFO("DIRECTORY SCAN END [%d]", ret); + } /*thread while*/ + +_POWEROFF: + MS_SAFE_FREE(scan_data); + if (handle) msc_disconnect_db(&handle); + + return false; +} + +/* this thread process only the request of media-server */ +static int _check_folder_from_list(char *folder_path, int item_num, GArray *dir_array) +{ + int i; + int array_len = dir_array->len; + msc_dir_info_s* dir_info = NULL; + struct stat buf; + time_t mtime; + bool find_flag = false; + + if(stat(folder_path, &buf) == 0) { + mtime = buf.st_mtime; + } else { + return MS_MEDIA_ERR_INTERNAL; + } + + for (i = 0; i < array_len; i++) { + dir_info = g_array_index (dir_array, msc_dir_info_s*, i); + if (strcmp(folder_path, dir_info->dir_path) == 0) { + /* if modified time is same, the folder does not need updating */ + if ((mtime == dir_info->modified_time) && (item_num == dir_info->item_num)) { + if (mtime == 0) + continue; + + g_array_remove_index (dir_array, i); + MS_SAFE_FREE(dir_info->dir_path); + MS_SAFE_FREE(dir_info); + } + find_flag = true; + break; + } + } + + /* this folder does not exist in media DB, so this folder has to insert to DB */ + if ((find_flag == false) && + (item_num > 0)) { + dir_info = NULL; + dir_info = malloc(sizeof(msc_dir_info_s)); + dir_info->dir_path = strdup(folder_path); + dir_info->modified_time = -1; + g_array_append_val(dir_array, dir_info); + } + + return MS_MEDIA_ERR_NONE; +} + +static int __msc_compare_with_db(void **handle, const char*update_path, int scan_type, GArray **dir_array) +{ + DIR *dp = NULL; + GArray *read_dir_array = NULL; + struct dirent entry; + struct dirent *result = NULL; + int ret = MS_MEDIA_ERR_NONE; + char *new_path = NULL; + char *current_path = NULL; + char path[MS_FILE_PATH_LEN_MAX] = { 0 }; + char * start_path = strdup(update_path); + int item_num = 0; + + /*get directories list from media db*/ + ret = msc_get_folder_list(handle, start_path, dir_array); + if (ret != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("msc_get_folder_list is failed", ret); + return ret; + } + + MSC_DBG_ERR(" "); + /* make new array for storing directory */ + read_dir_array = g_array_new (FALSE, FALSE, sizeof (char*)); + if (read_dir_array == NULL){ + MSC_DBG_ERR("g_array_new failed"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + /* add first direcotiry to directory array */ + g_array_append_val (read_dir_array, start_path); + + /*start db update. the number of element in the array , db update is complete.*/ + while (read_dir_array->len != 0) { + /* get the current path from directory array */ + current_path = g_array_index(read_dir_array , char*, 0); + g_array_remove_index (read_dir_array, 0); +// MSC_DBG_ERR("%s", current_path); + + if (__msc_check_scan_ignore(current_path)) { + MSC_DBG_ERR("%s is ignore", current_path); + MS_SAFE_FREE(current_path); + continue; + } + + dp = opendir(current_path); + if (dp != NULL) { + while (!readdir_r(dp, &entry, &result)) { + if (result == NULL) + break; + + if (entry.d_name[0] == '.') { + continue; + } + + if (entry.d_type & DT_DIR) { + if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("ms_strappend failed"); + continue; + } + /* add new directory to dir_array */ + new_path = strdup(path); + g_array_append_val (read_dir_array, new_path); + } else if (entry.d_type & DT_REG) { + item_num++; + } + } + + /* find and compare modified time */ + _check_folder_from_list(current_path, item_num, *dir_array); + item_num = 0; + } else { + MSC_DBG_ERR("%s folder opendir fails", current_path); + } + if (dp) closedir(dp); + dp = NULL; + MS_SAFE_FREE(current_path); + } /*db update while */ + + __msc_clear_file_list(read_dir_array); + + MSC_DBG_INFO("ret : %d", ret); + MSC_DBG_INFO("update count : %d", (*dir_array)->len); + + return ret; +} + +static int _msc_db_update_partial(void **handle, ms_storage_type_t storage_type, GArray *dir_array) +{ + int i; + int err = MS_MEDIA_ERR_NONE; + msc_dir_info_s* dir_info = NULL; + char *update_path = NULL; + + for (i = 0; i < dir_array->len; i ++) { + dir_info = g_array_index (dir_array, msc_dir_info_s*, i); + update_path = strdup(dir_info->dir_path); + +// MSC_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time); + if (dir_info->modified_time != -1) { + err = msc_set_folder_validity(handle, update_path, MS_INVALID, MS_NON_RECURSIVE); + if (err != MS_MEDIA_ERR_NONE) { + MSC_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time); + MSC_DBG_ERR("error : %d", err); + } + } + + __msc_dir_scan(handle, update_path, storage_type, MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE); + +// if (dir_info->modified_time != -1) { +// msc_update_folder_time(handle, tmp_path); +// } + } + + /*delete all node*/ + while(dir_array->len != 0) { + msc_dir_info_s *data = NULL; + data = g_array_index(dir_array , msc_dir_info_s*, 0); + g_array_remove_index (dir_array, 0); + MS_SAFE_FREE(data->dir_path); + MS_SAFE_FREE(data); + } + g_array_free(dir_array, FALSE); + dir_array = NULL; + + return MS_MEDIA_ERR_NONE; +} + +gboolean msc_storage_scan_thread(void *data) +{ + ms_comm_msg_s *scan_data = NULL; + int ret; + int err; + void **handle = NULL; + ms_storage_type_t storage_type = MS_STORAGE_INTERNAL; + int scan_type; + bool valid_status = TRUE; + char *update_path = NULL; + GArray *dir_array = NULL; + + while (1) { + scan_data = g_async_queue_pop(storage_queue); + if (scan_data->pid == POWEROFF) { + MSC_DBG_WAN("power off"); + goto _POWEROFF; + } + + MSC_DBG_INFO("STORAGE SCAN START [%s]", scan_data->msg); + + scan_type = scan_data->msg_type; + if (scan_type != MS_MSG_STORAGE_ALL + && scan_type != MS_MSG_STORAGE_PARTIAL + && scan_type != MS_MSG_STORAGE_INVALID) { + MSC_DBG_ERR("Invalid request"); + ret = MS_MEDIA_ERR_INVALID_PARAMETER; + goto NEXT; + } + + /*connect to media db, if conneting is failed, db updating is stopped*/ + err = msc_connect_db(&handle); + if (err != MS_MEDIA_ERR_NONE) + continue; + + storage_type = ms_get_storage_type_by_full(scan_data->msg); + update_path = strndup(scan_data->msg, scan_data->msg_size); + + /*start db updating */ + __msc_set_db_status(MS_DB_UPDATING, storage_type); + + valid_status = (scan_type == MS_MSG_STORAGE_PARTIAL) ? TRUE : FALSE; + __msc_bacth_commit_enable(handle, TRUE, valid_status, MS_NOTI_DISABLE, 0); + +#ifdef FMS_PERF + ms_check_start_time(&g_mmc_start_time); +#endif + + if (scan_type == MS_MSG_STORAGE_PARTIAL && storage_type == MS_STORAGE_INTERNAL) { + msc_validaty_change_all_items(handle, storage_type, true); + + /* find and compare modified time */ + ret = __msc_compare_with_db(handle, update_path, scan_data->msg_type, &dir_array); + if (ret != MS_MEDIA_ERR_NONE) { + MSC_DBG_WAN("__msc_compare_with_db is falied"); + goto NEXT; + } + + if (dir_array->len != 0) { + MSC_DBG_INFO("DB UPDATING IS NEEDED"); + + ret = _msc_db_update_partial(handle, storage_type, dir_array); + } else { + MSC_DBG_INFO("THERE IS NO UPDATE"); + } + } else { + if (scan_type == MS_MSG_STORAGE_ALL) { + /* Delete all data before full scanning */ + if (!msc_delete_invalid_items(handle, storage_type)) { + MSC_DBG_ERR("msc_delete_all_record fails"); + } + + if (storage_type == MS_STORAGE_EXTERNAL) { + ms_update_mmc_info(); + } + } else if (scan_type == MS_MSG_STORAGE_PARTIAL) { + msc_validaty_change_all_items(handle, storage_type, false); + } + + ret = __msc_db_update(handle, scan_data); + } + + /*call for bundle commit*/ + __msc_bacth_commit_disable(handle, TRUE, valid_status); + + if (scan_type == MS_MSG_STORAGE_PARTIAL) { + int del_count = 0; + + /*check delete count*/ + MSC_DBG_INFO("update path : %s", update_path); + msc_count_delete_items_in_folder(handle, update_path, &del_count); + + /*if there is no delete content, do not call delete API*/ + if (del_count != 0) + msc_delete_invalid_items(handle, storage_type); + } + + /* send notification */ + msc_send_dir_update_noti(handle, update_path); + MS_SAFE_FREE(update_path); + +#ifdef FMS_PERF + ms_check_end_time(&g_mmc_end_time); + ms_check_time_diff(&g_mmc_start_time, &g_mmc_end_time); +#endif + +NEXT: + /*set vconf key mmc loading for indicator */ + __msc_set_db_status(MS_DB_UPDATED, storage_type); + + if (power_off) { + MSC_DBG_WAN("power off"); + goto _POWEROFF; + } + + /*disconnect form media db*/ + if (handle) msc_disconnect_db(&handle); + + /*Active flush */ + malloc_trim(0); + + msc_send_result(ret, scan_data); + + MS_SAFE_FREE(scan_data); + + MSC_DBG_INFO("STORAGE SCAN END[%d]", ret); + } /*thread while*/ + +_POWEROFF: + MS_SAFE_FREE(scan_data); + if (handle) msc_disconnect_db(&handle); + + return false; +} + +static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data) +{ + MSC_DBG_SLOG("path : %s", insert_data->msg); + + if (insert_data->pid == POWEROFF) { + g_array_prepend_val(register_array, insert_data); + } else if (insert_data->msg_type == MS_MSG_BURSTSHOT_INSERT) { + g_array_prepend_val(register_array, insert_data); + } else { + g_array_append_val(register_array, insert_data); + } +} + +static bool __msc_is_valid_path(const char *path) +{ + if (path == NULL) + return false; + + if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) { + return true; + } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) { + return true; + } else + return false; + + return true; +} + +static int __msc_check_file_path(const char *file_path) +{ + int exist; + struct stat file_st; + + /* check location of file */ + /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */ + if(!__msc_is_valid_path(file_path)) { + MSC_DBG_ERR("Invalid path : %s", file_path); + return MS_MEDIA_ERR_INVALID_PATH; + } + + /* check the file exits actually */ + exist = open(file_path, O_RDONLY); + if(exist < 0) { + MSC_DBG_ERR("error [%s, %s]", file_path, strerror(errno)); + return MS_MEDIA_ERR_INVALID_PATH; + } + close(exist); + + /* check type of the path */ + /* It must be a regular file */ + memset(&file_st, 0, sizeof(struct stat)); + if(stat(file_path, &file_st) == 0) { + if(!S_ISREG(file_st.st_mode)) { + /* In this case, it is not a regula file */ + MSC_DBG_ERR("this path is not a file"); + return MS_MEDIA_ERR_INVALID_PATH; + } + } else { + MSC_DBG_ERR("stat failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_INVALID_PATH; + } + + return MS_MEDIA_ERR_NONE; +} + +static int __msc_clear_file_list(GArray *path_array) +{ + if (path_array) { + while(path_array->len != 0) { + char *data = NULL; + data = g_array_index(path_array , char*, 0); + g_array_remove_index (path_array, 0); + MS_SAFE_FREE(data); + } + g_array_free(path_array, FALSE); + path_array = NULL; + } + + return MS_MEDIA_ERR_NONE; +} + +static int __msc_check_ignore_dir(const char *full_path) +{ + int ret = MS_MEDIA_ERR_NONE; + char *dir_path = NULL; + char *leaf_path = NULL; + + ret = __msc_check_file_path(full_path); + if (ret != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("invalid path : %s", full_path); + return MS_MEDIA_ERR_INVALID_PATH; + } + + dir_path = g_path_get_dirname(full_path); + if (strcmp(dir_path, ".") == 0) { + MSC_DBG_ERR("getting directory path is failed : %s", full_path); + MS_SAFE_FREE(dir_path); + return MS_MEDIA_ERR_INVALID_PATH; + } + + while(1) { + if(__msc_check_scan_ignore(dir_path)) { + ret = MS_MEDIA_ERR_INVALID_PATH; + break; + } + + /*If root path, Stop Scanning*/ + if(strcmp(dir_path, MEDIA_ROOT_PATH_INTERNAL) == 0) + break; + else if(strcmp(dir_path, MEDIA_ROOT_PATH_SDCARD) == 0) + break; + + leaf_path = strrchr(dir_path, '/'); + if(leaf_path != NULL) { + int seek_len = leaf_path -dir_path; + dir_path[seek_len] = '\0'; + } else { + MSC_DBG_ERR("Fail to find leaf path"); + ret = MS_MEDIA_ERR_INVALID_PATH; + break; + } + } + + MS_SAFE_FREE(dir_path); + + return ret; +} + +static int __msc_make_file_list(char *file_path, GArray **path_array) +{ + FILE *fp = NULL; + char buf[MS_FILE_PATH_LEN_MAX] = {0,}; + char *path = NULL; + int length; + int res = MS_MEDIA_ERR_NONE; + int ret = MS_MEDIA_ERR_NONE; + + /* load the file list from file */ + fp = fopen(file_path, "rt"); + if (fp == NULL) { + MSC_DBG_ERR("fopen failed [%s]", strerror(errno)); + res = MS_MEDIA_ERR_FILE_OPEN_FAIL; + goto FREE_RESOURCE; + } + + memset(buf, 0x0, MS_FILE_PATH_LEN_MAX); + /* This is an array for storing the path of insert datas*/ + *path_array = g_array_new (FALSE, FALSE, sizeof (char *)); + if (*path_array == NULL) { + MSC_DBG_ERR("g_array_new failed"); + res = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + goto FREE_RESOURCE; + } + + /* read registering file path from stored file */ + while(fgets(buf, MS_FILE_PATH_LEN_MAX, fp) != NULL) { + length = strlen(buf); /*the return value of function, strlen(), includes "\n" */ + path = strndup(buf, length - 1); /*copying except "\n" and strndup fuction adds "\0" at the end of the copying string */ + + /* check valid path */ + ret = __msc_check_ignore_dir(path); + if (ret != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("invalide path : %s", path); + MS_SAFE_FREE(path); + continue; + } + /* insert getted path to the list */ + if (g_array_append_val(*path_array, path) == NULL) { + MSC_DBG_ERR("g_array_append_val failed"); + res = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + goto FREE_RESOURCE; + } + } + + if(fp) fclose(fp); + fp = NULL; + + return MS_MEDIA_ERR_NONE; + +FREE_RESOURCE: + + __msc_clear_file_list(*path_array); + + if(fp) fclose(fp); + fp = NULL; + + return res; +} + +static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array) +{ + int err; + int i; + void **handle = NULL; + char *insert_path = NULL; + int (*insert_function)(void **, const char*) = NULL; + + insert_function = (current_msg == MS_MSG_BULK_INSERT) ? msc_insert_item_batch : msc_insert_burst_item; + + /* connect to media db, if conneting is failed, db updating is stopped */ + err = msc_connect_db(&handle); + if (err != MS_MEDIA_ERR_NONE) + return MS_MEDIA_ERR_DB_CONNECT_FAIL; + + /*start db updating */ + /*call for bundle commit*/ + __msc_bacth_commit_enable(handle, TRUE, FALSE, MS_NOTI_ENABLE, pid); + + MSC_DBG_ERR("BULK REGISTER START"); + + /* get the inserting file path from array and insert to db */ + for (i = 0; i < path_array->len; i++) { + + insert_path = g_array_index(path_array, char*, i); + + /* insert to db */ + err = insert_function(handle, insert_path); + + if (power_off) { + MSC_DBG_ERR("power off"); + /*call for bundle commit*/ + msc_register_end(handle); + break; + } + } + + /*call for bundle commit*/ + __msc_bacth_commit_disable(handle, TRUE, FALSE); + + /*disconnect form media db*/ + if (handle) msc_disconnect_db(&handle); + + return MS_MEDIA_ERR_NONE; +} + +static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data) +{ + int remain_request; + ms_comm_msg_s *insert_data = NULL; + + while (1) { + remain_request = g_async_queue_length(reg_queue); + + /*updating requests remain*/ + if (register_array->len != 0 && remain_request == 0) { + *register_data = g_array_index(register_array, ms_comm_msg_s*, 0); + g_array_remove_index (register_array, 0); + break; + } else if (remain_request != 0) { + insert_data = g_async_queue_pop(reg_queue); + __msc_insert_register_request(register_array, insert_data); + continue; + } else if (register_array->len == 0 && remain_request == 0) { + /*Threre is no request, Wait until pushung new request*/ + insert_data = g_async_queue_pop(reg_queue); + __msc_insert_register_request(register_array, insert_data); + continue; + } + } + + if(((*register_data)->msg_size <= 0) ||((*register_data)->msg_size > MS_FILE_PATH_LEN_MAX)) { + MSC_DBG_ERR("message size[%d] is wrong", (*register_data)->msg_size); + return MS_MEDIA_ERR_INVALID_IPC_MESSAGE; + } + + return MS_MEDIA_ERR_NONE; + +} + +gboolean msc_register_thread(void *data) +{ + ms_comm_msg_s *register_data = NULL; + GArray *register_array = NULL; + GArray *path_array = NULL; + char *file_path = NULL; + int ret; + int pid = 0; + ms_msg_type_e current_msg = MS_MSG_MAX; + + /*create array for processing overlay data*/ + register_array = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *)); + if (register_array == NULL) { + MSC_DBG_ERR("g_array_new error"); + return false; + } + + while (1) { + ret = __msc_pop_register_request(register_array, ®ister_data); + if (register_data->pid == POWEROFF) { + MSC_DBG_ERR("power off"); + goto _POWEROFF; + } + + if (ret != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("__msc_pop_register_request failed [%d]", ret); + goto FREE_RESOURCE; + } + + /* check current request */ + current_msg = register_data->msg_type; + pid = register_data->pid; + + if ((current_msg != MS_MSG_BULK_INSERT) && + (current_msg != MS_MSG_BURSTSHOT_INSERT)) { + MSC_DBG_ERR("wrong message type"); + goto FREE_RESOURCE; + } + + file_path = strndup(register_data->msg, register_data->msg_size); + + ret = __msc_make_file_list(file_path, &path_array); + if (ret != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("__msc_make_file_list failed [%d]", ret); + goto FREE_RESOURCE; + } + + ret = __msc_batch_insert(current_msg, pid, path_array); + +FREE_RESOURCE: + /*Active flush */ + malloc_trim(0); + + /* If register_files operation is stopped, there is no necessrty for sending result. */ + msc_send_result(ret, register_data); + + MSC_DBG_ERR("BULK REGISTER END [%d |%s]", ret, register_data->msg); + + __msc_clear_file_list(path_array); + + MS_SAFE_FREE(file_path); + MS_SAFE_FREE(register_data); + } /*thread while*/ + +_POWEROFF: + MS_SAFE_FREE(file_path); + MS_SAFE_FREE(register_data); + if (register_array) { + while(register_array->len != 0) { + ms_comm_msg_s *data = NULL; + data = g_array_index(register_array , ms_comm_msg_s*, 0); + g_array_remove_index (register_array, 0); + MS_SAFE_FREE(data); + } + g_array_free (register_array, FALSE); + register_array = NULL; + } + + __msc_clear_file_list(path_array); + + return false; +} + +static void __msc_bacth_commit_enable(void* handle, bool ins_status, bool valid_status, bool noti_enable, int pid) +{ + /*call for bundle commit*/ + if (ins_status) msc_register_start(handle, noti_enable, pid); + if (valid_status) msc_validate_start(handle); + + return; +} + +static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status) +{ + /*call for bundle commit*/ + if (ins_status) msc_register_end(handle); + if (valid_status) msc_validate_end(handle); + + return; +} + diff --git a/src/scanner/media-scanner-socket.c b/src/scanner/media-scanner-socket.c new file mode 100755 index 0000000..bedd40a --- /dev/null +++ b/src/scanner/media-scanner-socket.c @@ -0,0 +1,183 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-thumb.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-types.h" +#include "media-common-utils.h" +#include "media-scanner-dbg.h" +#include "media-scanner-db-svc.h" +#include "media-scanner-socket.h" + +extern GAsyncQueue *storage_queue; +extern GAsyncQueue *scan_queue; +extern GAsyncQueue *reg_queue; + +gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data) +{ + ms_comm_msg_s *recv_msg = NULL; + int sockfd = MS_SOCK_NOT_ALLOCATE; + int req_num = MS_MSG_MAX; + int err = -1; + + sockfd = g_io_channel_unix_get_fd(src); + if (sockfd < 0) { + MSC_DBG_ERR("sock fd is invalid!"); + return TRUE; + } + + MS_MALLOC(recv_msg, sizeof(ms_comm_msg_s)); + if (recv_msg == NULL) { + MSC_DBG_ERR("MS_MALLOC failed"); + return TRUE; + } + + /* read() is blocked until media scanner sends message */ + err = read(sockfd, recv_msg, sizeof(ms_comm_msg_s)); + if (err < 0) { + MSC_DBG_ERR("fifo read failed [%s]", strerror(errno)); + MS_SAFE_FREE(recv_msg); + return MS_MEDIA_ERR_FILE_READ_FAIL; + } + + MSC_DBG_SLOG("receive msg from [%d] %d, %s", recv_msg->pid, recv_msg->msg_type, recv_msg->msg); + + /* copy from recived data */ + req_num = recv_msg->msg_type; + + switch(req_num){ + case MS_MSG_BULK_INSERT: + case MS_MSG_BURSTSHOT_INSERT: + { + MSC_DBG_INFO("BULK INSERT"); + /* request bulk insert*/ + g_async_queue_push(reg_queue, GINT_TO_POINTER(recv_msg)); + } + break; + case MS_MSG_DIRECTORY_SCANNING: + case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE: + { + /* this request from another apps */ + /* set the scan data for scanning thread */ + g_async_queue_push(scan_queue, GINT_TO_POINTER(recv_msg)); + } + break; + case MS_MSG_STORAGE_ALL: + case MS_MSG_STORAGE_PARTIAL: + case MS_MSG_STORAGE_INVALID: + { + /* this request from media-server */ + g_async_queue_push(storage_queue, GINT_TO_POINTER(recv_msg)); + } + break; + default: + { + MSC_DBG_ERR("THIS REQUEST IS INVALID %d", req_num); + MS_SAFE_FREE(recv_msg); + } + break; + } + + /*Active flush */ + malloc_trim(0); + + return TRUE; +} + +int msc_send_ready(void) +{ + int res = MS_MEDIA_ERR_NONE; + ms_comm_msg_s send_msg; + int fd = -1; + int err = -1; + + fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY); + if (fd < 0) { + MSC_DBG_ERR("fifo open failed", strerror(errno)); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + + /* send ready message */ + memset(&send_msg, 0, sizeof(send_msg)); + send_msg.msg_type = MS_MSG_SCANNER_READY; + + /* send ready message */ + err = write(fd, &send_msg, sizeof(send_msg)); + if (err < 0) { + MSC_DBG_ERR("fifo write failed", strerror(errno)); + res = MS_MEDIA_ERR_FILE_READ_FAIL; + } + + close(fd); + + return res; +} + +int msc_send_result(int result, ms_comm_msg_s *res_data) +{ + int res = MS_MEDIA_ERR_NONE; + ms_comm_msg_s send_msg; + int fd = -1; + int err = -1; + + fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY); + if (fd < 0) { + MSC_DBG_ERR("fifo open failed", strerror(errno)); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + + /* send result message */ + memset(&send_msg, 0x0, sizeof(ms_comm_msg_s)); + send_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT; + send_msg.pid = res_data->pid; + send_msg.result = result; + send_msg.msg_size = res_data->msg_size; + strncpy(send_msg.msg, res_data->msg, send_msg.msg_size); + + /* send ready message */ + err = write(fd, &send_msg, sizeof(send_msg)); + if (err < 0) { + MSC_DBG_ERR("fifo write failed", strerror(errno)); + res = MS_MEDIA_ERR_FILE_READ_FAIL; + } + + close(fd); + + return res; +} + diff --git a/src/scanner/media-scanner.c b/src/scanner/media-scanner.c new file mode 100755 index 0000000..4560037 --- /dev/null +++ b/src/scanner/media-scanner.c @@ -0,0 +1,324 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-main.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#include +#include +#include +#include +#include +#include + +#include "media-common-utils.h" +#include "media-common-external-storage.h" + +#include "media-util.h" +#include "media-scanner-dbg.h" +#include "media-scanner-db-svc.h" +#include "media-scanner-scan.h" +#include "media-scanner-socket.h" + +#define APP_NAME "media-scanner" + +extern int mmc_state; + +extern GAsyncQueue *storage_queue; +extern GAsyncQueue *scan_queue; +extern GAsyncQueue *reg_queue; +extern GMutex *db_mutex; +bool power_off; /*If this is TRUE, poweroff notification received*/ + +static GMainLoop *scanner_mainloop = NULL; + +bool check_process(void) +{ + DIR *pdir; + struct dirent pinfo; + struct dirent *result = NULL; + bool ret = false; + int find_pid = 0; + pid_t current_pid = 0; + + current_pid = getpid(); + + pdir = opendir("/proc"); + if (pdir == NULL) { + MSC_DBG_ERR("err: NO_DIR\n"); + return 0; + } + + while (!readdir_r(pdir, &pinfo, &result)) { + if (result == NULL) + break; + + if (pinfo.d_type != 4 || pinfo.d_name[0] == '.' + || pinfo.d_name[0] > 57) + continue; + + FILE *fp; + char buff[128]; + char path[128]; + + ms_strcopy(path, sizeof(path), "/proc/%s/status", pinfo.d_name); + fp = fopen(path, "rt"); + if (fp) { + if (fgets(buff, 128, fp) == NULL) + MSC_DBG_ERR("fgets failed"); + fclose(fp); + + if (strstr(buff, APP_NAME)) { + find_pid = atoi(pinfo.d_name); + if (find_pid == current_pid) + ret = true; + else { + ret = false; + break; + } + } + } else { + MSC_DBG_ERR("Can't read file [%s]", path); + } + } + + closedir(pdir); + + return ret; +} + +void init_process() +{ + +} + +static void _power_off_cb(void* data) +{ + ms_comm_msg_s *scan_data; + ms_comm_msg_s *reg_data; + + MSC_DBG_INFO("++++++++++++++++++++++++++++++++++++++"); + MSC_DBG_INFO("POWER OFF"); + MSC_DBG_INFO("++++++++++++++++++++++++++++++++++++++"); + + power_off = true; + + if (scan_queue) { + /*notify to scannig thread*/ + MS_MALLOC(scan_data, sizeof(ms_comm_msg_s)); + scan_data->pid = POWEROFF; + g_async_queue_push(scan_queue, GINT_TO_POINTER(scan_data)); + } + + if (reg_queue) { + /*notify to register thread*/ + MS_MALLOC(reg_data, sizeof(ms_comm_msg_s)); + reg_data->pid = POWEROFF; + g_async_queue_push(reg_queue, GINT_TO_POINTER(reg_data)); + } + + if (storage_queue) { + /*notify to register thread*/ + MS_MALLOC(reg_data, sizeof(ms_comm_msg_s)); + reg_data->pid = POWEROFF; + g_async_queue_push(storage_queue, GINT_TO_POINTER(reg_data)); + } + + if (g_main_loop_is_running(scanner_mainloop)) g_main_loop_quit(scanner_mainloop); +} + +void +_msc_mmc_vconf_cb(keynode_t *key, void* data) +{ + int status = 0; +/* + ms_comm_msg_s *scan_msg; + ms_dir_scan_type_t scan_type = MS_SCAN_PART; +*/ + if (!ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) { + MSC_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed."); + } + + MSC_DBG_INFO("VCONFKEY_SYSMAN_MMC_STATUS :%d", status); + + mmc_state = status; +/* + MS_MALLOC(scan_msg, sizeof(ms_comm_msg_s)); + + if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED || + mmc_state == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) { + scan_type = MS_SCAN_INVALID; + } else if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) { + scan_type = ms_get_mmc_state(); + } + + switch (scan_type) { + case MS_SCAN_ALL: + scan_msg->msg_type = MS_MSG_STORAGE_ALL; + break; + case MS_SCAN_PART: + scan_msg->msg_type = MS_MSG_STORAGE_PARTIAL; + break; + case MS_SCAN_INVALID: + scan_msg->msg_type = MS_MSG_STORAGE_INVALID; + break; + } + + scan_msg->pid = 0; + scan_msg->msg_size = strlen(MEDIA_ROOT_PATH_SDCARD); + ms_strcopy(scan_msg->msg, scan_msg->msg_size+1, "%s", MEDIA_ROOT_PATH_SDCARD); + + MSC_DBG_INFO("ms_get_mmc_state is %d", scan_msg->msg_type); + + g_async_queue_push(storage_queue, GINT_TO_POINTER(scan_msg)); +*/ + return; +} + +int main(int argc, char **argv) +{ + GThread *storage_scan_thread = NULL; + GThread *scan_thread = NULL; + GThread *register_thread = NULL; + GSource *source = NULL; + GIOChannel *channel = NULL; + GMainContext *context = NULL; + + int err = -1; + int fd = -1; + +#if 0 /* temporary */ + check_result = check_process(); + if (check_result == false) + exit(0); +#endif + if (!g_thread_supported()) { + g_thread_init(NULL); + } + + /*Init main loop*/ + scanner_mainloop = g_main_loop_new(NULL, FALSE); + + /*set power off callback function*/ + ms_add_poweoff_event_receiver(_power_off_cb, NULL); + + /*load functions from plusin(s)*/ + err = msc_load_functions(); + if (err != MS_MEDIA_ERR_NONE) { + MSC_DBG_ERR("function load failed"); + exit(0); + } + + /*Init for register file*/ + /*These are a communicator for thread*/ + if (!scan_queue) scan_queue = g_async_queue_new(); + if (!reg_queue) reg_queue = g_async_queue_new(); + if (!storage_queue) storage_queue = g_async_queue_new(); + + /*Init mutex variable*/ + if (!db_mutex) db_mutex = g_mutex_new(); + + /* Create pipe */ + err = unlink(MS_SCANNER_FIFO_PATH_REQ); + if (err !=0) { + MSC_DBG_ERR("unlink failed [%s]", strerror(errno)); + } + + err = mkfifo(MS_SCANNER_FIFO_PATH_REQ, MS_SCANNER_FIFO_MODE); + if (err !=0) { + MSC_DBG_ERR("mkfifo failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_FIFO_MAKE_FAIL; + } + + fd = open(MS_SCANNER_FIFO_PATH_REQ, O_RDWR); + if (fd < 0) { + MSC_DBG_ERR("fifo open failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + + context = g_main_loop_get_context(scanner_mainloop); + + /* Create new channel to watch pipe */ + channel = g_io_channel_unix_new(fd); + source = g_io_create_watch(channel, G_IO_IN); + + /* Set callback to be called when pipe is readable */ + g_source_set_callback(source, (GSourceFunc)msc_receive_request, NULL, NULL); + g_source_attach(source, context); + g_source_unref(source); + + /*create each threads*/ + storage_scan_thread = g_thread_new("storage_scan_thread", (GThreadFunc)msc_storage_scan_thread, NULL); + scan_thread = g_thread_new("scanner_thread", (GThreadFunc)msc_directory_scan_thread, NULL); + register_thread = g_thread_new("register_thread", (GThreadFunc)msc_register_thread, NULL); + + /*set vconf callback function*/ + err = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, (vconf_callback_fn) _msc_mmc_vconf_cb, NULL); + if (err == -1) + MSC_DBG_ERR("add call back function for event %s fails", VCONFKEY_SYSMAN_MMC_STATUS); + + if (ms_is_mmc_inserted()) { + mmc_state = VCONFKEY_SYSMAN_MMC_MOUNTED; + } + + MSC_DBG_INFO("scanner is ready"); + + msc_send_ready(); + + MSC_DBG_ERR("*****************************************"); + MSC_DBG_ERR("*** Scanner is running ***"); + MSC_DBG_ERR("*****************************************"); + + g_main_loop_run(scanner_mainloop); + + g_thread_join (scan_thread); + g_thread_join (register_thread); + g_thread_join (storage_scan_thread); + + if (power_off) { + g_io_channel_shutdown(channel, FALSE, NULL); + g_io_channel_unref(channel); + } + + if (scan_queue) g_async_queue_unref(scan_queue); + if (reg_queue) g_async_queue_unref(reg_queue); + if (storage_queue) g_async_queue_unref(storage_queue); + + /*Clear db mutex variable*/ + if (db_mutex) g_mutex_free (db_mutex); + + /*close pipe*/ + close(fd); + + /*unload functions*/ + msc_unload_functions(); + + MSC_DBG_INFO("SCANNER IS END"); + + exit(0); +} + diff --git a/src/server/include/media-server-db-svc.h b/src/server/include/media-server-db-svc.h new file mode 100755 index 0000000..010de85 --- /dev/null +++ b/src/server/include/media-server-db-svc.h @@ -0,0 +1,58 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-db-svc.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_SERVER_DB_SVC_H_ +#define _MEDIA_SERVER_DB_SVC_H_ + +#include "media-common-types.h" + +typedef int (*CONNECT)(void**, char **); +typedef int (*DISCONNECT)(void*, char **); +typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, char **); +typedef int (*CHECK_DB)(void*, char **); + +int +ms_load_functions(void); + +void +ms_unload_functions(void); + +int +ms_connect_db(void ***handle); + +int +ms_disconnect_db(void ***handle); + +int +ms_invalidate_all_items(void **handle, ms_storage_type_t store_type); + +int +ms_check_db_upgrade(void **handle); + +#endif /*_MEDIA_SERVER_DB_SVC_H_*/ \ No newline at end of file diff --git a/src/server/include/media-server-db.h b/src/server/include/media-server-db.h new file mode 100755 index 0000000..87f0b00 --- /dev/null +++ b/src/server/include/media-server-db.h @@ -0,0 +1,31 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef _MEDIA_SERVER_DB_H_ +#define _MEDIA_SERVER_DB_H_ + +#include + +GMainLoop *ms_db_get_mainloop(void); +gboolean ms_db_get_thread_status(void); +gboolean ms_db_thread(void *data); + +#endif/* _MEDIA_SERVER_DB_H_ */ diff --git a/src/server/include/media-server-dbg.h b/src/server/include/media-server-dbg.h new file mode 100755 index 0000000..0f22e08 --- /dev/null +++ b/src/server/include/media-server-dbg.h @@ -0,0 +1,62 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-dbg.h + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#ifndef _MEDIA_SERVER_DBG_H_ +#define _MEDIA_SERVER_DBG_H_ + +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "MEDIA_SERVER" + +#define MS_DBG_SLOG(fmt, args...) do{ if (true) { \ + SECURE_LOGD(fmt "\n" , ##args); \ + }} while(false) + +#define MS_DBG(fmt, args...) do{ if (true) { \ + LOGD(fmt "\n" , ##args); \ + }} while(false) + +#define MS_DBG_INFO(fmt, args...) do{ if (true) { \ + LOGI(fmt "\n" , ##args); \ + }} while(false) + +#define MS_DBG_WARN(fmt, args...) do{ if (true) { \ + LOGW(fmt "\n", ##args); \ + }} while(false) + +#define MS_DBG_ERR(fmt, args...) do{ if (true) { \ + LOGE(fmt "\n", ##args); \ + }} while(false) + +#endif /*_MEDIA_SERVER_DBG_H_*/ diff --git a/src/server/include/media-server-scanner.h b/src/server/include/media-server-scanner.h new file mode 100755 index 0000000..735a5df --- /dev/null +++ b/src/server/include/media-server-scanner.h @@ -0,0 +1,28 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +int ms_scanner_start(void); + +bool ms_get_scanner_status(void); + +void ms_reset_scanner_status(void); + +int ms_get_scanner_pid(void); \ No newline at end of file diff --git a/src/server/include/media-server-socket.h b/src/server/include/media-server-socket.h new file mode 100755 index 0000000..a9d3a61 --- /dev/null +++ b/src/server/include/media-server-socket.h @@ -0,0 +1,48 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-thumb.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#ifndef _MEDIA_SERVER_SOCKET_H_ +#define _MEDIA_SERVER_SOCKET_H_ + +#include "media-common-types.h" +#include "media-server-ipc.h" + +gboolean ms_read_socket(gpointer user_data); + +gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data); + +gboolean ms_read_db_tcp_batch_socket(GIOChannel *src, GIOCondition condition, gpointer data); + +int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock); + +int ms_send_storage_scan_request(char *root_path, ms_dir_scan_type_t scan_type); + +gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition, gpointer data); + +#endif /*_MEDIA_SERVER_SOCKET_H_*/ diff --git a/src/server/include/media-server-thumb.h b/src/server/include/media-server-thumb.h new file mode 100755 index 0000000..a8558a9 --- /dev/null +++ b/src/server/include/media-server-thumb.h @@ -0,0 +1,46 @@ +/* + * media-thumbnail-server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Hyunjun Ko + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include "media-common-types.h" +#include "media-server-ipc.h" + +#ifndef _MEDIA_SERVER_THUMB_H_ +#define _MEDIA_SERVER_THUMB_H_ + +#define MAX_THUMB_REQUEST 100 + +GMainLoop * +ms_get_thumb_thread_mainloop(void); + +int +ms_thumb_get_server_pid(); + +void +ms_thumb_reset_server_status(); + +gpointer +ms_thumb_agent_start_thread(gpointer data); + +#endif /*_MEDIA_SERVER_THUMB_H_*/ + diff --git a/src/server/media-server-db-svc.c b/src/server/media-server-db-svc.c new file mode 100755 index 0000000..b99ba42 --- /dev/null +++ b/src/server/media-server-db-svc.c @@ -0,0 +1,304 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-db-svc.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief This file implements main database operation. + */ + +#include + +#include "media-util.h" + +#include "media-common-utils.h" +#include "media-server-dbg.h" +#include "media-server-db-svc.h" + +#define CONFIG_PATH "/usr/etc/media-server-plugin" +#define EXT ".so" +#define EXT_LEN 3 + +GArray *so_array; +void ***func_array; +int lib_num; + +void **func_handle = NULL; /*dlopen handle*/ + +enum func_list { + eCONNECT, + eDISCONNECT, + eSET_ALL_VALIDITY, + eCHECK_DB, + eFUNC_MAX +}; + +static int +_ms_token_data(char *buf, char **name) +{ + int len; + char* pos = NULL; + + pos = strstr(buf, EXT); + if (pos == NULL) { + MS_DBG_ERR("This is not shared object library."); + name = NULL; + return -1; + } else { + len = pos - buf + EXT_LEN; + *name = strndup(buf, len); + MS_DBG("%s", *name); + } + + return 0; +} + +static bool +_ms_load_config() +{ + int ret; + FILE *fp; + char *so_name = NULL; + char buf[256] = {0}; + + fp = fopen(CONFIG_PATH, "rt"); + if (fp == NULL) { + MS_DBG_ERR("fp is NULL"); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + while(1) { + if (fgets(buf, 256, fp) == NULL) { + MS_DBG_ERR("fgets failed"); + break; + } + + ret = _ms_token_data(buf, &so_name); + if (ret == 0) { + /*add to array*/ + g_array_append_val(so_array, so_name); + so_name = NULL; + } + } + + fclose(fp); + + return MS_MEDIA_ERR_NONE; +} + +int +ms_load_functions(void) +{ + int lib_index = 0, func_index; + char func_list[eFUNC_MAX][40] = { + "connect_db", + "disconnect_db", + "set_all_storage_items_validity", + "check_db", + }; + /*init array for adding name of so*/ + so_array = g_array_new(FALSE, FALSE, sizeof(char*)); + + /*load information of so*/ + _ms_load_config(); + + if(so_array->len == 0) { + MS_DBG("There is no information for functions"); + return MS_MEDIA_ERR_DYNAMIC_LINK; + } + + /*the number of functions*/ + lib_num = so_array->len; + + MS_DBG("The number of information of so : %d", lib_num); + MS_MALLOC(func_handle, sizeof(void*) * lib_num); + if (func_handle == NULL) { + MS_DBG_ERR("malloc failed"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + + while(lib_index < lib_num) { + /*get handle*/ + MS_DBG("[name of so : %s]", g_array_index(so_array, char*, lib_index)); + func_handle[lib_index] = dlopen(g_array_index(so_array, char*, lib_index), RTLD_LAZY); + if (!func_handle[lib_index]) { + MS_DBG_ERR("%s", dlerror()); + MS_SAFE_FREE(func_handle); + return MS_MEDIA_ERR_DYNAMIC_LINK; + } + lib_index++; + } + + dlerror(); /* Clear any existing error */ + + /*allocate for array of functions*/ + MS_MALLOC(func_array, sizeof(void*) * lib_num); + if (func_array == NULL) { + MS_DBG_ERR("malloc failed"); + MS_SAFE_FREE(func_handle); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + + for(lib_index = 0 ; lib_index < lib_num; lib_index ++) { + MS_MALLOC(func_array[lib_index], sizeof(void*) * eFUNC_MAX); + if (func_array[lib_index] == NULL) { + int index; + + for (index = 0; index < lib_index; index ++) { + MS_SAFE_FREE(func_array[index]); + } + MS_SAFE_FREE(func_array); + MS_SAFE_FREE(func_handle); + + MS_DBG_ERR("malloc failed"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + } + + /*add functions to array */ + for (lib_index = 0; lib_index < lib_num; lib_index++) { + for (func_index = 0; func_index < eFUNC_MAX ; func_index++) { + func_array[lib_index][func_index] = dlsym(func_handle[lib_index], func_list[func_index]); + if (func_array[lib_index][func_index] == NULL) { + int index; + + for (index = 0; index < lib_index; index ++) { + MS_SAFE_FREE(func_array[index]); + } + MS_SAFE_FREE(func_array); + MS_SAFE_FREE(func_handle); + + MS_DBG_ERR("dlsym failed"); + return MS_MEDIA_ERR_DYNAMIC_LINK; + } + } + } + + return MS_MEDIA_ERR_NONE; +} + +void +ms_unload_functions(void) +{ + int lib_index; + + for (lib_index = 0; lib_index < lib_num; lib_index ++) + dlclose(func_handle[lib_index]); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + if (func_array[lib_index]) { + MS_SAFE_FREE(func_array[lib_index]); + } + } + + MS_SAFE_FREE (func_array); + MS_SAFE_FREE (func_handle); + if (so_array) g_array_free(so_array, TRUE); +} + +int +ms_connect_db(void ***handle) +{ + int lib_index; + int ret; + char * err_msg = NULL; + + MS_MALLOC(*handle, sizeof (void*) * lib_num); + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), &err_msg); /*dlopen*/ + if (ret != 0) { + MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_DB_CONNECT_FAIL; + } + } + + MS_DBG("connect Media DB"); + + return MS_MEDIA_ERR_NONE; +} + +int +ms_disconnect_db(void ***handle) +{ + int lib_index; + int ret; + char * err_msg = NULL; + + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((DISCONNECT)func_array[lib_index][eDISCONNECT])((*handle)[lib_index], &err_msg); /*dlopen*/ + if (ret != 0) { + MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + return MS_MEDIA_ERR_DB_DISCONNECT_FAIL; + } + } + + MS_SAFE_FREE(*handle); + + MS_DBG("Disconnect Media DB"); + + return MS_MEDIA_ERR_NONE; +} + +int +ms_invalidate_all_items(void **handle, ms_storage_type_t store_type) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + MS_DBG(""); + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, &err_msg); /*dlopen*/ + if (ret != 0) { + MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + } + MS_DBG(""); + return res; +} + +int +ms_check_db_upgrade(void **handle) +{ + int lib_index; + int res = MS_MEDIA_ERR_NONE; + int ret; + char *err_msg = NULL; + MS_DBG(""); + for (lib_index = 0; lib_index < lib_num; lib_index++) { + ret = ((CHECK_DB)func_array[lib_index][eCHECK_DB])(handle[lib_index], &err_msg); /*dlopen*/ + if (ret != 0) { + MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); + MS_SAFE_FREE(err_msg); + res = MS_MEDIA_ERR_DB_UPDATE_FAIL; + } + } + MS_DBG(""); + return res; +} + diff --git a/src/server/media-server-db.c b/src/server/media-server-db.c new file mode 100755 index 0000000..1b1b282 --- /dev/null +++ b/src/server/media-server-db.c @@ -0,0 +1,138 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of media db write. + * + * @file media-server-db.c + * @author Haejeong Kim(backto.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#include + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-types.h" + +#include "media-server-dbg.h" +#include "media-server-socket.h" +#include "media-server-db.h" + +static GMainLoop *g_db_mainloop = NULL; +static bool db_thread_ready = FALSE; + +GMainLoop *ms_db_get_mainloop(void) +{ + return g_db_mainloop; +} +gboolean ms_db_get_thread_status(void) +{ + return db_thread_ready; +} + +gboolean ms_db_thread(void *data) +{ + int sockfd = -1; + int tcp_sockfd = -1; + int ret = MS_MEDIA_ERR_NONE; + GSource *source = NULL; + GIOChannel *channel = NULL; + GSource *tcp_source = NULL; + GIOChannel *tcp_channel = NULL; + GMainContext *context = NULL; + MediaDBHandle *db_handle = NULL; + + /* Connect Media DB*/ + if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("Failed to connect DB"); + return FALSE; + } + + /* Create TCP Socket*/ + ret = ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_DB_UPDATE_PORT, &sockfd); + if(ret != MS_MEDIA_ERR_NONE) { + /* Disconnect DB*/ + media_db_disconnect(db_handle); + MS_DBG_ERR("Failed to create socket"); + return FALSE; + } + + /* Create TCP Socket for batch query*/ + ret = ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_DB_BATCH_UPDATE_PORT, &tcp_sockfd); + if(ret != MS_MEDIA_ERR_NONE) { + /* Disconnect DB*/ + media_db_disconnect(db_handle); + close(sockfd); + MS_DBG_ERR("Failed to create socket"); + return FALSE; + } + + context = g_main_context_new(); + /*Init main loop*/ + g_db_mainloop = g_main_loop_new(context, FALSE); + //context = g_main_loop_get_context(g_db_mainloop); + + /* Create new channel to watch UDP socket */ + channel = g_io_channel_unix_new(sockfd); + source = g_io_create_watch(channel, G_IO_IN); + + /* Set callback to be called when socket is readable */ + g_source_set_callback(source, (GSourceFunc)ms_read_db_tcp_socket, db_handle, NULL); + g_source_attach(source, context); + + /* Create new channel to watch TCP socket */ + tcp_channel = g_io_channel_unix_new(tcp_sockfd); + tcp_source = g_io_create_watch(tcp_channel, G_IO_IN); + + /* Set callback to be called when socket is readable */ + g_source_set_callback(tcp_source, (GSourceFunc)ms_read_db_tcp_batch_socket, db_handle, NULL); + g_source_attach(tcp_source, context); + + g_main_context_push_thread_default(context); + + MS_DBG("*******************************************"); + MS_DBG("*** Media Server DB thread is running ***"); + MS_DBG("*******************************************"); + + db_thread_ready = TRUE; + + g_main_loop_run(g_db_mainloop); + + MS_DBG("*** Media Server DB thread will be closed ***"); + + db_thread_ready = FALSE; + + g_io_channel_shutdown(channel, FALSE, NULL); + g_io_channel_unref(channel); + + /* Disconnect DB*/ + media_db_disconnect(db_handle); + + /*close socket*/ + close(sockfd); + close(tcp_sockfd); + + g_main_loop_unref(g_db_mainloop); + + return FALSE; +} diff --git a/src/server/media-server-main.c b/src/server/media-server-main.c new file mode 100755 index 0000000..4cd5c38 --- /dev/null +++ b/src/server/media-server-main.c @@ -0,0 +1,440 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-main.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ + +#include +#include +#include +#include +#include +#include + +#include "media-util.h" +#include "media-common-utils.h" +#include "media-common-external-storage.h" +#include "media-server-dbg.h" +#include "media-server-db-svc.h" +#include "media-server-socket.h" +#include "media-server-db.h" +#include "media-server-thumb.h" +#include "media-server-scanner.h" + +#define APP_NAME "media-server" + +extern GMutex *scanner_mutex; + +GMainLoop *mainloop = NULL; +bool power_off; /*If this is TRUE, poweroff notification received*/ + +static void __ms_check_mediadb(void); +static void __ms_add_signal_handler(void); +static void __ms_remove_event_receiver(void); +static void __ms_add_event_receiver(GIOChannel *channel); +static void __ms_remove_requst_receiver(GIOChannel *channel); +static void __ms_add_requst_receiver(GMainLoop *mainloop, GIOChannel **channel); + +bool check_process() +{ + DIR *pdir; + struct dirent pinfo; + struct dirent *result = NULL; + bool ret = false; + int find_pid = 0; + pid_t current_pid = 0; + + current_pid = getpid(); + + pdir = opendir("/proc"); + if (pdir == NULL) { + MS_DBG_ERR("err: NO_DIR"); + return 0; + } + + while (!readdir_r(pdir, &pinfo, &result)) { + if (result == NULL) + break; + + if (pinfo.d_type != 4 || pinfo.d_name[0] == '.' + || pinfo.d_name[0] > 57) + continue; + + FILE *fp; + char buff[128]; + char path[128]; + + ms_strcopy(path, sizeof(path), "/proc/%s/status", pinfo.d_name); + fp = fopen(path, "rt"); + if (fp) { + if (fgets(buff, 128, fp) == NULL) + MS_DBG_ERR("fgets failed"); + fclose(fp); + + if (strstr(buff, APP_NAME)) { + find_pid = atoi(pinfo.d_name); + if (find_pid == current_pid) + ret = true; + else { + ret = false; + break; + } + } + } else { + MS_DBG_ERR("Can't read file [%s]", path); + } + } + + closedir(pdir); + + return ret; +} + +void _power_off_cb(void* data) +{ + MS_DBG_ERR("POWER OFF"); + + GIOChannel *channel = (GIOChannel *)data; + + power_off = TRUE; + + /*Quit Thumbnail Thread*/ + GMainLoop *thumb_mainloop = ms_get_thumb_thread_mainloop(); + if (thumb_mainloop && g_main_is_running(thumb_mainloop)) { + g_main_loop_quit(thumb_mainloop); + } + + /*Quit DB Thread*/ + GMainLoop *db_mainloop = ms_db_get_mainloop(); + if(db_mainloop && g_main_loop_is_running(db_mainloop)) { + g_main_loop_quit(db_mainloop); + } + + __ms_remove_requst_receiver(channel); + + __ms_remove_event_receiver(); + + return; +} + +static void _db_clear(void) +{ + int err = MS_MEDIA_ERR_NONE; + void **handle = NULL; + + /*load functions from plusin(s)*/ + err = ms_load_functions(); + if (err != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("function load failed"); + exit(0); + } + + /*connect to media db, if conneting is failed, db updating is stopped*/ + ms_connect_db(&handle); + + /* check schema of db is chaged and need upgrade */ + MS_DBG_WARN("Check DB upgrade start"); + if (ms_check_db_upgrade(handle) != MS_MEDIA_ERR_NONE) + MS_DBG_ERR("ms_check_db_upgrade fail"); + MS_DBG_WARN("Check DB upgrade end"); + + /*update just valid type*/ + if (ms_invalidate_all_items(handle, MS_STORAGE_EXTERNAL) != MS_MEDIA_ERR_NONE) + MS_DBG_ERR("ms_change_valid_type fail"); + + /*disconnect form media db*/ + if (handle) ms_disconnect_db(&handle); + + /*unload functions*/ + ms_unload_functions(); +} + +static void _db_backup(void) +{ + int err = MS_MEDIA_ERR_NONE; + void **handle = NULL; + + /*load functions from plusin(s)*/ + err = ms_load_functions(); + if (err != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("function load failed"); + exit(0); + } + + ms_connect_db(&handle); + + /*disconnect form media db*/ + if (handle) ms_disconnect_db(&handle); + + /*unload functions*/ + ms_unload_functions(); +} + +void _ms_signal_handler(int n) +{ + int stat, pid, thumb_pid; + int scanner_pid; + + thumb_pid = ms_thumb_get_server_pid(); + scanner_pid = ms_get_scanner_pid(); + + while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) { + /* check pid of child process of thumbnail thread */ + if (pid == thumb_pid) { + MS_DBG_ERR("[%d] Thumbnail server is stopped by media-server", pid); + ms_thumb_reset_server_status(); + } else if (pid == scanner_pid) { + MS_DBG_ERR("[%d] Scanner is stopped by media-server", pid); + ms_reset_scanner_status(); + } else { + MS_DBG_ERR("[%d] is stopped", pid); + } +/* + if (WIFEXITED(stat)) { + MS_DBG_ERR("normal termination , exit status : %d", WEXITSTATUS(stat)); + } else if (WIFSIGNALED(stat)) { + MS_DBG_ERR("abnormal termination , signal number : %d", WTERMSIG(stat)); + } else if (WIFSTOPPED(stat)) { + MS_DBG_ERR("child process is stoped, signal number : %d", WSTOPSIG(stat)); + } +*/ + } + + return; +} + +static void _ms_new_global_variable(void) +{ + /*Init mutex variable*/ + /*media scanner stop/start mutex*/ + if (!scanner_mutex) scanner_mutex = g_mutex_new(); +} + +static void _ms_free_global_variable(void) +{ + /*Clear mutex variable*/ + if (scanner_mutex) g_mutex_free(scanner_mutex); +} + +void +_ms_mmc_vconf_cb(keynode_t *key, void* data) +{ + int status = 0; + + if (!ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) { + MS_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed."); + } + + MS_DBG_ERR("CURRENT STATUS OF SD CARD[%d]", status); + + /* If scanner is not working, media server executes media scanner and sends request. */ + /* If scanner is working, it detects changing status of SD card. */ + if (status == VCONFKEY_SYSMAN_MMC_REMOVED || + status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) { + + /*remove added watch descriptors */ + ms_present_mmc_status(MS_SDCARD_REMOVED); + + ms_send_storage_scan_request(MEDIA_ROOT_PATH_SDCARD, MS_SCAN_INVALID); + } else if (status == VCONFKEY_SYSMAN_MMC_MOUNTED) { + + ms_make_default_path_mmc(); + + ms_present_mmc_status(MS_SDCARD_INSERTED); + + ms_send_storage_scan_request(MEDIA_ROOT_PATH_SDCARD, ms_get_mmc_state()); + } + + return; +} + +static void _ms_check_pw_status(void) +{ + int pw_status = 0; + + if (!ms_config_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &pw_status)) { + MS_DBG_ERR("Get VCONFKEY_SYSMAN_POWER_OFF_STATUS failed."); + } + + if (pw_status == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || + pw_status == VCONFKEY_SYSMAN_POWER_OFF_RESTART) { + power_off = TRUE; + + while(1) { + MS_DBG_WARN("wait power off"); + sleep(3); + } + } + + return; +} + +int main(int argc, char **argv) +{ + GThread *db_thread = NULL; + GThread *thumb_thread = NULL; + GIOChannel *channel = NULL; + bool check_result = false; + + power_off = FALSE; + + _ms_check_pw_status(); + + check_result = check_process(); + if (check_result == false) + exit(0); + + if (!g_thread_supported()) { + g_thread_init(NULL); + } + + /*Init main loop*/ + mainloop = g_main_loop_new(NULL, FALSE); + + _ms_new_global_variable(); + + __ms_add_requst_receiver(mainloop, &channel); + + /* recevie event from other modules */ + __ms_add_event_receiver(channel); + + __ms_add_signal_handler(); + + /*create each threads*/ + db_thread = g_thread_new("db_thread", (GThreadFunc)ms_db_thread, NULL); + thumb_thread = g_thread_new("thumb_agent_thread", (GThreadFunc)ms_thumb_agent_start_thread, NULL); + + /*clear previous data of sdcard on media database and check db status for updating*/ + while(!ms_db_get_thread_status()) { + MS_DBG_ERR("wait db thread"); + sleep(1); + } + + /* update media DB */ + __ms_check_mediadb(); + + /*Active flush */ + malloc_trim(0); + + MS_DBG_ERR("*** Media Server is running ***"); + + g_main_loop_run(mainloop); + + g_thread_join(db_thread); + g_thread_join(thumb_thread); + + _ms_free_global_variable(); + + MS_DBG_ERR("*** Media Server is stopped ***"); + + exit(0); +} + +static void __ms_add_requst_receiver(GMainLoop *mainloop, GIOChannel **channel) +{ + + int sockfd = -1; + GSource *source = NULL; + GMainContext *context = NULL; + + /*prepare socket*/ + /* Create and bind new UDP socket */ + if (ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_SCANNER_PORT, &sockfd) + != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("Failed to create socket"); + exit(0); + } else { + context = g_main_loop_get_context(mainloop); + + /* Create new channel to watch udp socket */ + *channel = g_io_channel_unix_new(sockfd); + source = g_io_create_watch(*channel, G_IO_IN); + + /* Set callback to be called when socket is readable */ + g_source_set_callback(source, (GSourceFunc)ms_read_socket, *channel, NULL); + g_source_attach(source, context); + g_source_unref(source); + } +} + +static void __ms_remove_requst_receiver(GIOChannel *channel) +{ + /*close an IO channel*/ + g_io_channel_unix_get_fd(channel); + g_io_channel_shutdown(channel, FALSE, NULL); + g_io_channel_unref(channel); +} + +static void __ms_add_event_receiver(GIOChannel *channel) +{ + int err; + + /*set power off callback function*/ + ms_add_poweoff_event_receiver(_power_off_cb,channel); + + /*add noti receiver for SD card event */ + err = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, (vconf_callback_fn) _ms_mmc_vconf_cb, NULL); + if (err == -1) + MS_DBG_ERR("add call back function for event %s fails", VCONFKEY_SYSMAN_MMC_STATUS); + +} + +static void __ms_remove_event_receiver(void) +{ + vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, + (vconf_callback_fn) _ms_mmc_vconf_cb); +} + +static void __ms_add_signal_handler(void) +{ + struct sigaction sigset; + + /* Add signal handler */ + sigemptyset(&sigset.sa_mask); + sigaddset(&sigset.sa_mask, SIGCHLD); + sigset.sa_flags = SA_RESTART |SA_NODEFER; + sigset.sa_handler = _ms_signal_handler; + + if (sigaction(SIGCHLD, &sigset, NULL) < 0) { + MS_DBG_ERR("sigaction failed [%s]", strerror(errno)); + } + + signal(SIGPIPE,SIG_IGN); +} +static void __ms_check_mediadb(void) +{ + _db_clear(); + + /* update external storage */ + if (ms_is_mmc_inserted()) { + ms_make_default_path_mmc(); + ms_present_mmc_status(MS_SDCARD_INSERTED); + + ms_send_storage_scan_request(MEDIA_ROOT_PATH_SDCARD, ms_get_mmc_state()); + } +} + diff --git a/src/server/media-server-scanner.c b/src/server/media-server-scanner.c new file mode 100755 index 0000000..986dbd2 --- /dev/null +++ b/src/server/media-server-scanner.c @@ -0,0 +1,246 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-types.h" +#include "media-common-utils.h" +#include "media-server-dbg.h" +#include "media-server-socket.h" +#include "media-server-scanner.h" + +#define MS_NO_REMAIN_TASK 0 + +extern GMainLoop *mainloop; +extern GArray *owner_list; +GMutex *scanner_mutex; + +static bool scanner_ready; +static int alarm_id; +static int child_pid; +static int receive_id; + +static int _ms_check_remain_task(void) +{ + int remain_task; + + if (owner_list != NULL) + remain_task = owner_list->len; + else + remain_task = MS_NO_REMAIN_TASK; + + return remain_task; +} + +ms_db_status_type_t ms_check_scanning_status(void) +{ + int status; + + if(ms_config_get_int(VCONFKEY_FILEMANAGER_DB_STATUS, &status)) { + if (status == VCONFKEY_FILEMANAGER_DB_UPDATING) { + return MS_DB_UPDATING; + } + } + + return MS_DB_UPDATED; +} + +static gboolean _ms_stop_scanner (gpointer user_data) +{ + int task_num = MS_NO_REMAIN_TASK; + + g_mutex_lock(scanner_mutex); + + /* check status of scanner */ + /* If some task remain or scanner is running, scanner must not stop*/ + task_num = _ms_check_remain_task(); + if (task_num != MS_NO_REMAIN_TASK) { + MS_DBG("[%d] task(s) remains", task_num); + g_mutex_unlock(scanner_mutex); + return TRUE; + } + + if (ms_check_scanning_status() == MS_DB_UPDATING) { + MS_DBG("DB is updating"); + g_mutex_unlock(scanner_mutex); + return TRUE; + } else { + MS_DBG("DB updating is not working"); + } + + /* stop media scanner */ + if (child_pid >0 ) { + if (kill(child_pid, SIGKILL) < 0) { + MS_DBG_ERR("kill failed : %s", strerror(errno)); + g_mutex_unlock(scanner_mutex); + return TRUE; + } + } + MS_DBG("KILL SCANNER"); + + /* close & delete FIFO */ + int res_fd = -1; + GIOChannel *res_channel = user_data; + + res_fd = g_io_channel_unix_get_fd(res_channel); + close(res_fd); + + unlink(MS_SCANNER_FIFO_PATH_RES); + unlink(MS_SCANNER_FIFO_PATH_REQ); + +// ms_reset_scanner_status(); + + g_source_destroy(g_main_context_find_source_by_id(g_main_loop_get_context (mainloop), alarm_id)); + g_source_destroy(g_main_context_find_source_by_id(g_main_loop_get_context (mainloop), receive_id)); + + return FALSE; +} + +static void _ms_add_timeout(guint interval, GSourceFunc func, gpointer data) +{ + MS_DBG(""); + GSource *src; + + src = g_timeout_source_new_seconds(interval); + g_source_set_callback(src, func, data, NULL); + alarm_id = g_source_attach(src, g_main_loop_get_context (mainloop)); + g_source_unref(src); +} + +int +ms_scanner_start(void) +{ + int pid; + + g_mutex_lock(scanner_mutex); + + if (child_pid > 0) { + MS_DBG_ERR("media scanner is already started"); + g_mutex_unlock(scanner_mutex); + return MS_MEDIA_ERR_NONE; + } + + if((pid = fork()) < 0) { + MS_DBG_ERR("Fork error\n"); + g_mutex_unlock(scanner_mutex); + } else if (pid > 0) { + /* parent process */ + /* wait until scanner is ready*/ + int ret = MS_MEDIA_ERR_NONE; + int err = -1; + int fd = -1; + ms_comm_msg_s recv_msg; + int scanner_status = -1; + + err = unlink(MS_SCANNER_FIFO_PATH_RES); + if (err !=0) { + MS_DBG_ERR("unlink failed [%s]", strerror(errno)); + } + err = mkfifo(MS_SCANNER_FIFO_PATH_RES, MS_SCANNER_FIFO_MODE); + if (err !=0) { + MS_DBG_ERR("mkfifo failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_FIFO_MAKE_FAIL; + } + + fd = open(MS_SCANNER_FIFO_PATH_RES, O_RDWR); + if (fd < 0) { + MS_DBG_ERR("fifo open failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + + /* read() is blocked until media scanner sends message */ + err = read(fd, &recv_msg, sizeof(recv_msg)); + if (err < 0) { + MS_DBG_ERR("fifo read failed [%s]", strerror(errno)); + close(fd); + return MS_MEDIA_ERR_FILE_READ_FAIL; + } + + scanner_status = recv_msg.msg_type; + if (scanner_status == MS_MSG_SCANNER_READY) { + GSource *res_source = NULL; + GIOChannel *res_channel = NULL; + GMainContext *res_context = NULL; + + MS_DBG_ERR("SCANNER is ready"); + scanner_ready = true; + child_pid = pid; + /* attach result receiving socket to mainloop */ + res_context = g_main_loop_get_context(mainloop); + + /* Create new channel to watch udp socket */ + res_channel = g_io_channel_unix_new(fd); + res_source = g_io_create_watch(res_channel, G_IO_IN); + + /* Set callback to be called when socket is readable */ + g_source_set_callback(res_source, (GSourceFunc)ms_receive_message_from_scanner, NULL, NULL); + receive_id = g_source_attach(res_source, res_context); + g_source_unref(res_source); + + _ms_add_timeout(30, (GSourceFunc)_ms_stop_scanner, res_channel); + + ret = MS_MEDIA_ERR_NONE; + } else { + MS_DBG_ERR("SCANNER is not ready"); + ret = MS_MEDIA_ERR_SCANNER_NOT_READY; + close(fd); + } + + g_mutex_unlock(scanner_mutex); + + return ret; + /* attach socket receive message callback */ + } else if(pid == 0) { + /* child process */ + MS_DBG_ERR("CHILD PROCESS"); + MS_DBG("EXECUTE MEDIA SCANNER"); + execl("/usr/bin/media-scanner", "media-scanner", NULL); + g_mutex_unlock(scanner_mutex); + } + + return MS_MEDIA_ERR_NONE; +} + +bool ms_get_scanner_status(void) +{ + return scanner_ready; +} + +void ms_reset_scanner_status(void) +{ + child_pid = 0; + scanner_ready = false; + + g_mutex_unlock(scanner_mutex); +} + +int ms_get_scanner_pid(void) +{ + return child_pid; +} diff --git a/src/server/media-server-socket.c b/src/server/media-server-socket.c new file mode 100755 index 0000000..7600a6b --- /dev/null +++ b/src/server/media-server-socket.c @@ -0,0 +1,718 @@ +/* + * Media Server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Yong Yeon Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This file defines api utilities of contents manager engines. + * + * @file media-server-thumb.c + * @author Yong Yeon Kim(yy9875.kim@samsung.com) + * @version 1.0 + * @brief + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "media-util.h" +#include "media-util-internal.h" +#include "media-server-ipc.h" +#include "media-common-utils.h" +#include "media-server-dbg.h" +#include "media-server-db-svc.h" +#include "media-server-scanner.h" +#include "media-server-socket.h" + +extern GAsyncQueue *scan_queue; +GAsyncQueue* ret_queue; +GArray *owner_list; +extern GMutex *scanner_mutex; +gint cur_running_task; + +extern bool power_off; + +typedef struct ms_req_owner_data +{ + int pid; + int index; + int client_sockfd; +}ms_req_owner_data; + +static int __ms_add_owner(int pid, int client_sock) +{ + if (pid != 0) { + ms_req_owner_data *owner_data = NULL; + + /* If owner list is NULL, create it */ + /* pid and client address are stored in ower list */ + /* These are used for sending result of scanning */ + if (owner_list == NULL) { + /*create array for processing overlay data*/ + owner_list = g_array_new (FALSE, FALSE, sizeof (ms_req_owner_data *)); + if (owner_list == NULL) { + MS_DBG_ERR("g_array_new error"); + return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + } + + /* store pid and client address */ + MS_MALLOC(owner_data, sizeof(ms_req_owner_data)); + owner_data->pid = pid; + owner_data->client_sockfd = client_sock; + // MS_DBG("the length of array : %d", owner_list->len); + // MS_DBG("pid : %d", owner_data->pid); + // MS_DBG("client_addr : %p", owner_data->client_addr); + + owner_data->index = -1; + g_array_append_val(owner_list, owner_data); + } + + return MS_MEDIA_ERR_NONE; +} + +static int __ms_find_owner(int pid, ms_req_owner_data **owner_data) +{ + int i; + int len = owner_list->len; + ms_req_owner_data *data = NULL; + + *owner_data = NULL; + + MS_DBG("length list : %d", len); + + for (i=0; i < len; i++) { + data = g_array_index(owner_list, ms_req_owner_data*, i); + MS_DBG("%d %d", data->pid, pid); + if (data->pid == pid) { + data->index = i; + *owner_data = data; + MS_DBG("FIND OWNER"); + break; + } + } + + return MS_MEDIA_ERR_NONE; +} + +static int __ms_delete_owner(ms_req_owner_data *owner_data) +{ + if (owner_data->index != -1) { + g_array_remove_index(owner_list, owner_data->index); + MS_SAFE_FREE(owner_data); + MS_DBG("DELETE OWNER"); + } + + return MS_MEDIA_ERR_NONE; +} + +static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg) +{ + if (owner_list != NULL) { + /* If the owner of result message is not media-server, media-server notify to the owner */ + /* The owner of message is distingushied by pid in received message*/ + /* find owner data */ + ms_req_owner_data *owner_data = NULL; + + __ms_find_owner(pid, &owner_data); + if (owner_data != NULL) { + MS_DBG("PID : %d", owner_data->pid); + /* owner data exists */ + /* send result to the owner of request */ + ms_ipc_send_msg_to_client_tcp(owner_data->client_sockfd, recv_msg, NULL); + close(owner_data->client_sockfd); + + /* free owner data*/ + __ms_delete_owner(owner_data); + } else { + MS_DBG_ERR("Not found Owner"); + return MS_MEDIA_ERR_INTERNAL; + } + } else { + /* owner data does not exist*/ + /* this is result of request of media server*/ + MS_DBG_ERR("There is no request, Owner list is NULL"); + return MS_MEDIA_ERR_INTERNAL; + } + + return MS_MEDIA_ERR_NONE; + +} + +static int __ms_privilege_check(const char *msg, gboolean *privilege) +{ +#define operation_cnt 3 +#define db_table_cnt 5 + + int o_idx = 0; + int t_idx = 0; + gboolean is_privilege = TRUE; + + char *operation[operation_cnt] = { + "INSERT INTO ", + "DELETE FROM ", + "UPDATE " + }; + + char *db_table[db_table_cnt] = { + "playlist_map", + "playlist", + "tag_map", + "tag", + "bookmark" + }; + + if(strlen(msg) < 10) { + MS_DBG_ERR("msg is too short!!"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + for(o_idx = 0; o_idx < operation_cnt; o_idx++) { + if(strncmp(operation[o_idx], msg, strlen(operation[o_idx])) == 0) { + for(t_idx = 0; t_idx < db_table_cnt; t_idx++) { + if(strncmp(db_table[t_idx], msg+strlen(operation[o_idx]), strlen(db_table[t_idx])) == 0) { + MS_DBG("Non privilege [%s][%s]", operation[o_idx], db_table[t_idx]); + is_privilege = FALSE; + break; + } + } + break; + } + } + + *privilege = is_privilege; + + return MS_MEDIA_ERR_NONE; +} + +static int __ms_privilege_ask(int client_sockfd) +{ + int ret = 0; + int res = MS_MEDIA_ERR_NONE; + + ret = security_server_check_privilege_by_sockfd(client_sockfd, "media-data::db", "w"); + if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) { + MS_DBG_ERR("You do not have permission for this operation."); + res = MS_MEDIA_ERR_PERMISSION_DENIED; + } else { + MS_DBG("PERMISSION OK"); + } + + return res; +} +gboolean ms_read_socket(gpointer user_data) +{ + ms_comm_msg_s recv_msg; + int sockfd = MS_SOCK_NOT_ALLOCATE; + int ret; + int res; + int req_num = -1; + int client_sock = -1; + GIOChannel *src = user_data; + + sockfd = g_io_channel_unix_get_fd(src); + if (sockfd < 0) { + MS_DBG_ERR("sock fd is invalid!"); + return TRUE; + } + + /* get client socket fd */ + ret = ms_ipc_accept_client_tcp(sockfd, &client_sock); + if (ret != MS_MEDIA_ERR_NONE) { + return TRUE; + } + + ret = ms_ipc_receive_message_tcp(client_sock, &recv_msg); + if (ret != MS_MEDIA_ERR_NONE) { + res = ret; + goto ERROR; + } + + ret = __ms_privilege_ask(client_sock); + if (ret == MS_MEDIA_ERR_PERMISSION_DENIED) { + res = MS_MEDIA_ERR_PERMISSION_DENIED; + goto ERROR; + } + + /* copy received data */ + req_num = recv_msg.msg_type; + + /* register file request + * media server inserts the meta data of one file into media db */ + if (req_num == MS_MSG_DIRECTORY_SCANNING + ||req_num == MS_MSG_BULK_INSERT + ||req_num == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE + || req_num == MS_MSG_BURSTSHOT_INSERT) { + if ((ret = ms_send_scan_request(&recv_msg, client_sock)) != MS_MEDIA_ERR_NONE) { + res = ret; + goto ERROR; + } + } else { + /* NEED IMPLEMENTATION */ + close(client_sock); + } + + /*Active flush */ + malloc_trim(0); + + return TRUE; +ERROR: + { + ms_comm_msg_s res_msg; + + memset(&res_msg, 0x0, sizeof(ms_comm_msg_s)); + + switch(req_num) { + case MS_MSG_DIRECTORY_SCANNING: + case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE: + res_msg.msg_type = MS_MSG_SCANNER_RESULT; + break; + case MS_MSG_BULK_INSERT: + case MS_MSG_BURSTSHOT_INSERT: + res_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT; + break; + } + + res_msg.result = res; + + ms_ipc_send_msg_to_client_tcp(client_sock, &res_msg, NULL); + close(client_sock); + } + + return TRUE; +} + +static int __ms_send_request(ms_comm_msg_s *send_msg) +{ + int res = MS_MEDIA_ERR_NONE; + int fd = -1; + int err = -1; + + fd = open(MS_SCANNER_FIFO_PATH_REQ, O_WRONLY); + if (fd < 0) { + MS_DBG_ERR("fifo open failed [%s]", strerror(errno)); + return MS_MEDIA_ERR_FILE_OPEN_FAIL; + } + + /* send message */ + err = write(fd, send_msg, sizeof(ms_comm_msg_s)); + if (err < 0) { + MS_DBG_ERR("fifo write failed [%s]", strerror(errno)); + close(fd); + return MS_MEDIA_ERR_FILE_READ_FAIL; + } + + close(fd); + + return res; +} + +int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock) +{ + int res = MS_MEDIA_ERR_NONE; + int err = MS_MEDIA_ERR_NONE; + int pid = send_msg->pid; + + g_mutex_lock(scanner_mutex); + + if (ms_get_scanner_status()) { + MS_DBG_WARN("Scanner is ready"); + __ms_send_request(send_msg); + + g_mutex_unlock(scanner_mutex); + } else { + MS_DBG_WARN("Scanner starts"); + g_mutex_unlock(scanner_mutex); + + err = ms_scanner_start(); + if(err == MS_MEDIA_ERR_NONE) { + err = __ms_send_request(send_msg); + if(err != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("__ms_send_request failed", err); + } + } else { + MS_DBG_ERR("Scanner starting failed. %d", err); + res = err; + } + } + + if (res == MS_MEDIA_ERR_NONE) { + /* this request process in media scanner */ + if ((err = __ms_add_owner(pid, client_sock)) != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("__ms_add_owner failed. %d", err); + res = err; + } + } + + return res; +} + +int ms_send_storage_scan_request(char *root_path, ms_dir_scan_type_t scan_type) +{ + int ret = MS_MEDIA_ERR_NONE; + ms_comm_msg_s scan_msg = { + .msg_type = MS_MSG_STORAGE_INVALID, + .pid = 0, /* pid 0 means media-server */ + .result = -1, + .msg_size = 0, + .msg = {0}, + }; + + /* msg_type */ + switch (scan_type) { + case MS_SCAN_PART: + scan_msg.msg_type = MS_MSG_STORAGE_PARTIAL; + break; + case MS_SCAN_ALL: + scan_msg.msg_type = MS_MSG_STORAGE_ALL; + break; + case MS_SCAN_INVALID: + scan_msg.msg_type = MS_MSG_STORAGE_INVALID; + break; + default : + ret = MS_MEDIA_ERR_INVALID_PARAMETER; + MS_DBG_ERR("ms_send_storage_scan_request invalid parameter"); + goto ERROR; + break; + } + + /* msg_size & msg */ + scan_msg.msg_size = strlen(root_path); + strncpy(scan_msg.msg, root_path, scan_msg.msg_size ); + + ret = ms_send_scan_request(&scan_msg, -1); + +ERROR: + + return ret; +} + +gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data) +{ + int sock = -1; + int client_sock = -1; + char * sql_query = NULL; + ms_comm_msg_s recv_msg; + int ret = MS_MEDIA_ERR_NONE; + MediaDBHandle *db_handle = (MediaDBHandle *)data; + int send_msg = MS_MEDIA_ERR_NONE; + gboolean privilege = TRUE; + + if (power_off == TRUE) { + MS_DBG_WARN("in the power off sequence"); + return TRUE; + } + + sock = g_io_channel_unix_get_fd(src); + if (sock < 0) { + MS_DBG_ERR("sock fd is invalid!"); + return TRUE; + } + + /* get client socket fd */ + ret = ms_ipc_accept_client_tcp(sock, &client_sock); + if (ret != MS_MEDIA_ERR_NONE) { + return TRUE; + } + + ret = ms_ipc_receive_message_tcp(client_sock, &recv_msg); + if (ret != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("ms_ipc_receive_message_tcp failed [%d]", ret); + send_msg = ret; + goto ERROR; + } + + /* check privileage */ + if(__ms_privilege_check(recv_msg.msg, &privilege) != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("invalid query. size[%d]", recv_msg.msg_size); + send_msg = MS_MEDIA_ERR_SOCKET_RECEIVE; + goto ERROR; + } + + if (privilege == TRUE) { + ret = __ms_privilege_ask(client_sock); + if (ret == MS_MEDIA_ERR_PERMISSION_DENIED) { + send_msg = MS_MEDIA_ERR_PERMISSION_DENIED; + goto ERROR; + } + } + + sql_query = strndup(recv_msg.msg, recv_msg.msg_size); + if (sql_query != NULL) { + ret = media_db_update_db(db_handle, sql_query); + if (ret != MS_MEDIA_ERR_NONE) + MS_DBG_ERR("media_db_update_db error : %d", ret); + + send_msg = ret; + MS_SAFE_FREE(sql_query); + } else { + send_msg = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + } + +ERROR: + if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { + MS_DBG_ERR("send failed : %s", strerror(errno)); + } else { + MS_DBG("Sent successfully"); + } + + if (close(client_sock) <0) { + MS_DBG_ERR("close failed [%s]", strerror(errno)); + } + + return TRUE; +} + + +void _ms_process_tcp_message(gpointer data, gpointer user_data) +{ + int ret = MS_MEDIA_ERR_NONE; + char * sql_query = NULL; + ms_comm_msg_s recv_msg; + int client_sock = GPOINTER_TO_INT (data); + int send_msg = MS_MEDIA_ERR_NONE; + MediaDBHandle *db_handle = NULL; + + memset((void *)&recv_msg, 0, sizeof(ms_comm_msg_s)); + + /* Connect Media DB*/ + if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("Failed to connect DB"); + send_msg = MS_MEDIA_ERR_DB_CONNECT_FAIL; + goto ERROR; + } + +// MS_DBG_ERR("client sokcet : %d", client_sock); + + while(1) { + if (power_off == TRUE) { + MS_DBG_WARN("in the power off sequence"); + break; + } + + ret = ms_ipc_receive_message_tcp(client_sock, &recv_msg); + if (ret != MS_MEDIA_ERR_NONE) { + media_db_request_update_db_batch_clear(); + MS_DBG_ERR("ms_ipc_receive_message_tcp failed [%d]", ret); + send_msg = ret; + goto ERROR; + } + + sql_query = strndup(recv_msg.msg, recv_msg.msg_size); + if (sql_query != NULL) { + if (recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_START) { + ret = media_db_update_db_batch_start(sql_query); + } else if(recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_END) { + ret = media_db_update_db_batch_end(db_handle, sql_query); + } else if(recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH) { + ret = media_db_update_db_batch(sql_query); + } else { + + } + + MS_SAFE_FREE(sql_query); + send_msg = ret; + + if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { + MS_DBG_ERR("send failed : %s", strerror(errno)); + } + +// MS_DBG_ERR("client sokcet : %d", client_sock); + if (recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_END) { + MS_DBG_WARN("Batch job is successfull!client sockfd [%d]", client_sock); + break; + } + + if (ret < MS_MEDIA_ERR_NONE && recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_START) { + MS_DBG_ERR("Batch job start is failed!client sockfd [%d]", client_sock); + media_db_request_update_db_batch_clear(); + break; + } + + memset((void *)&recv_msg, 0, sizeof(ms_comm_msg_s)); + } else { + MS_DBG_ERR("MS_MALLOC failed"); + media_db_request_update_db_batch_clear(); + /* send error to client */ + send_msg = MS_MEDIA_ERR_SOCKET_RECEIVE; + goto ERROR; + } + } + + if (close(client_sock) <0) { + MS_DBG_ERR("close failed [%s]", strerror(errno)); + } + + /* Disconnect DB*/ + media_db_disconnect(db_handle); + MS_DBG_ERR("END"); + if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE) + MS_DBG_ERR("g_atomic_int_dec_and_test failed"); + + return; + +ERROR: + + /* send error to client */ + if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { + MS_DBG_ERR("send failed : %s", strerror(errno)); + } else { + MS_DBG("Sent successfully"); + } + + if (close(client_sock) <0) { + MS_DBG_ERR("close failed [%s]", strerror(errno)); + } + + /* Disconnect DB*/ + media_db_disconnect(db_handle); + MS_DBG_ERR("END"); + if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE) + MS_DBG_ERR("g_atomic_int_dec_and_test failed"); + + return; +} + +gboolean ms_read_db_tcp_batch_socket(GIOChannel *src, GIOCondition condition, gpointer data) +{ +#define MAX_THREAD_NUM 3 + + static GThreadPool *gtp = NULL; + GError *error = NULL; + int ret = MS_MEDIA_ERR_NONE; + int res = MS_MEDIA_ERR_NONE; + int sock = -1; + int client_sock = -1; + + sock = g_io_channel_unix_get_fd(src); + if (sock < 0) { + MS_DBG_ERR("sock fd is invalid!"); + return TRUE; + } + + /* get client socket fd */ + ret = ms_ipc_accept_client_tcp(sock, &client_sock); + if (ret != MS_MEDIA_ERR_NONE) { + media_db_request_update_db_batch_clear(); + return TRUE; + } + + MS_DBG_SLOG("Client[%d] is accepted", client_sock); + + if (gtp == NULL) { + MS_DBG_SLOG("Create New Thread Pool %d", client_sock); + gtp = g_thread_pool_new((GFunc)_ms_process_tcp_message, NULL, MAX_THREAD_NUM, TRUE, &error); + if (error != NULL) { + res = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL; + goto ERROR; + } + } + + /*check number of running thread */ + if (g_atomic_int_get(&cur_running_task) < MAX_THREAD_NUM) { + MS_DBG_SLOG("CURRENT RUNNING TASK %d", cur_running_task); + g_atomic_int_inc(&cur_running_task); + g_thread_pool_push(gtp, GINT_TO_POINTER(client_sock), &error); + + if (error != NULL) { + res = MS_MEDIA_ERR_INTERNAL; + goto ERROR; + } + } else { + /* all thread is working, return busy error */ + res = MS_MEDIA_ERR_DB_BATCH_UPDATE_BUSY; + goto ERROR; + } + + return TRUE; + +ERROR: + { + int send_msg = MS_MEDIA_ERR_NONE; + + if (error != NULL) { + MS_DBG_SLOG("g_thread_pool_push failed [%d]", error->message); + g_error_free(error); + error = NULL; + } + + /* send error to clinet*/ + send_msg = res; + if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { + MS_DBG_ERR("send failed : %s", strerror(errno)); + } else { + MS_DBG("Sent successfully"); + } + + if (close(client_sock) <0) { + MS_DBG_ERR("close failed [%s]", strerror(errno)); + } + } + + return TRUE; +} + +gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition, gpointer data) +{ + ms_comm_msg_s recv_msg; + int fd = MS_SOCK_NOT_ALLOCATE; + int msg_type; + int pid = -1; + int err; + + fd = g_io_channel_unix_get_fd(src); + if (fd < 0) { + MS_DBG_ERR("fd is invalid!"); + return TRUE; + } + + /* read() is blocked until media scanner sends message */ + err = read(fd, &recv_msg, sizeof(recv_msg)); + if (err < 0) { + MS_DBG_ERR("fifo read failed [%s]", strerror(errno)); + close(fd); + return MS_MEDIA_ERR_FILE_READ_FAIL; + } + + MS_DBG_SLOG("receive result from scanner [%d] %d, %s", recv_msg.pid, recv_msg.result, recv_msg.msg); + + msg_type = recv_msg.msg_type; + pid = recv_msg.pid; + if ((msg_type == MS_MSG_SCANNER_RESULT) || + (msg_type == MS_MSG_SCANNER_BULK_RESULT)) { + MS_DBG_WARN("DB UPDATING IS DONE[%d]", msg_type); + + if (pid != 0) { + __ms_send_result_to_client(pid, &recv_msg); + } else { + MS_DBG_SLOG("This request is from media-server"); + } + } else { + MS_DBG_ERR("This result message is wrong : %d", recv_msg.msg_type ); + } + + return TRUE; +} + diff --git a/src/server/media-server-thumb.c b/src/server/media-server-thumb.c new file mode 100755 index 0000000..7981f93 --- /dev/null +++ b/src/server/media-server-thumb.c @@ -0,0 +1,997 @@ +/* + * media-thumbnail-server + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Hyunjun Ko + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +#include "media-util.h" +#include "media-common-utils.h" +#include "media-server-dbg.h" +#include "media-server-thumb.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "MEDIA_SERVER_THUMB" + +#define THUMB_SERVER_NAME "media-thumbnail" + +static GMainLoop *g_thumb_agent_loop = NULL; +static GIOChannel *g_udp_channel = NULL; +static gboolean g_folk_thumb_server = FALSE; +static gboolean g_thumb_server_extracting = FALSE; +static gboolean g_shutdowning_thumb_server = FALSE; +static gboolean g_thumb_server_queued_all_extracting_request = FALSE; +static int g_communicate_sock = 0; +static int g_timer_id = 0; +static int g_server_pid = 0; + +static GQueue *g_request_queue = NULL; +static int g_queue_work = 0; + +typedef struct { + int client_sock; + thumbMsg *recv_msg; +} thumbRequest; + +extern char MEDIA_IPC_PATH[][50]; + +int _ms_thumb_create_socket(int sock_type, int *sock); +int _ms_thumb_create_udp_socket(int *sock); +int _ms_thumb_recv_msg(int sock, int header_size, thumbMsg *msg); +int _ms_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size); +int _ms_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size); +gboolean _ms_thumb_check_queued_request(gpointer data); +gboolean _ms_thumb_agent_prepare_udp_socket(); +gboolean _ms_thumb_agent_timer(); + + +gboolean _ms_thumb_agent_start_jobs(gpointer data) +{ + MS_DBG(""); + + return FALSE; +} + +void _ms_thumb_agent_finish_jobs() +{ + MS_DBG(""); + + return; +} + +GMainLoop* ms_get_thumb_thread_mainloop(void) +{ + return g_thumb_agent_loop; +} + +int ms_thumb_get_server_pid() +{ + return g_server_pid; +} + +void ms_thumb_reset_server_status() +{ + g_folk_thumb_server = FALSE; + g_shutdowning_thumb_server = FALSE; + + if (g_timer_id > 0) { + g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_timer_id)); + g_timer_id = 0; + } + + if (g_thumb_server_extracting) { + /* Need to inplement when crash happens */ + MS_DBG_ERR("Thumbnail server is dead when processing all-thumbs extraction"); + g_thumb_server_extracting = FALSE; + g_server_pid = 0; + } else { + g_thumb_server_extracting = FALSE; + g_server_pid = 0; + } + + return; +} + +void _ms_thumb_create_timer(int id) +{ + if (id > 0) + g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id)); + + GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20); + g_source_set_callback (timer_src, _ms_thumb_agent_timer, NULL, NULL); + g_timer_id = g_source_attach (timer_src, g_main_context_get_thread_default()); + +} + +/* This checks if thumbnail server is running */ +bool _ms_thumb_check_process() +{ + DIR *pdir; + struct dirent pinfo; + struct dirent *result = NULL; + bool ret = FALSE; + + pdir = opendir("/proc"); + if (pdir == NULL) { + MS_DBG_ERR("err: NO_DIR"); + return 0; + } + + while (!readdir_r(pdir, &pinfo, &result)) { + if (result == NULL) + break; + + if (pinfo.d_type != 4 || pinfo.d_name[0] == '.' + || pinfo.d_name[0] > 57) + continue; + + FILE *fp; + char buff[128]; + char path[128]; + + ms_strcopy(path, sizeof(path), "/proc/%s/status", pinfo.d_name); + fp = fopen(path, "rt"); + if (fp) { + if (fgets(buff, 128, fp) == NULL) + MS_DBG_ERR("fgets failed"); + fclose(fp); + + if (strstr(buff, THUMB_SERVER_NAME)) { + ret = TRUE; + break; + } + } else { + MS_DBG_ERR("Can't read file [%s]", path); + } + } + + closedir(pdir); + + return ret; +} + +int _ms_thumb_create_socket(int sock_type, int *sock) +{ + int sock_fd = 0; + + if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + MS_DBG_ERR("socket failed: %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + if (sock_type == CLIENT_SOCKET) { + + struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 }; + + if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) { + MS_DBG_ERR("setsockopt failed: %s", strerror(errno)); + close(sock_fd); + return MS_MEDIA_ERR_SOCKET_INTERNAL; + } + } else if (sock_type == SERVER_SOCKET) { + + int n_reuse = 1; + + if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) { + MS_DBG_ERR("setsockopt failed: %s", strerror(errno)); + close(sock_fd); + return MS_MEDIA_ERR_SOCKET_INTERNAL; + } + } + + *sock = sock_fd; + + return MS_MEDIA_ERR_NONE; +} + + +int _ms_thumb_create_udp_socket(int *sock) +{ + int sock_fd = 0; + + if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { + MS_DBG_ERR("socket failed: %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_CONN; + } + + struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 }; + + if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) { + MS_DBG_ERR("setsockopt failed: %s", strerror(errno)); + close(sock_fd); + return MS_MEDIA_ERR_SOCKET_INTERNAL; + } + + *sock = sock_fd; + + return MS_MEDIA_ERR_NONE; +} + +int _media_thumb_get_error() +{ + if (errno == EWOULDBLOCK) { + MS_DBG_ERR("Timeout. Can't try any more"); + if (!_ms_thumb_check_process()) { + MS_DBG_ERR("Thumbnail server is not running!. Reset info for thumb server to execute"); + ms_thumb_reset_server_status(); + } + + return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; + } else { + MS_DBG_ERR("recvfrom failed : %s", strerror(errno)); + return MS_MEDIA_ERR_SOCKET_RECEIVE; + } +} + +int _ms_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) +{ + int recv_msg_len = 0; + unsigned char *buf = NULL; + + buf = (unsigned char*)malloc(header_size); + + if ((recv_msg_len = recv(sock, buf, header_size, 0)) < 0) { + MS_DBG_ERR("recv failed : %s", strerror(errno)); + MS_SAFE_FREE(buf); + return _media_thumb_get_error(); + } + + memcpy(msg, buf, header_size); + //MS_DBG("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size); + + MS_SAFE_FREE(buf); + + if (msg->origin_path_size <= 0 || msg->origin_path_size > MS_FILE_PATH_LEN_MAX) { + MS_SAFE_FREE(buf); + MS_DBG_ERR("msg->origin_path_size is invalid %d", msg->origin_path_size ); + return MS_MEDIA_ERR_DATA_TAINTED; + } + + buf = (unsigned char*)malloc(msg->origin_path_size); + + if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) { + MS_DBG_ERR("recv failed : %s", strerror(errno)); + MS_SAFE_FREE(buf); + return _media_thumb_get_error(); + } + + strncpy(msg->org_path, (char*)buf, msg->origin_path_size); + //MS_DBG("original path : %s", msg->org_path); + + MS_SAFE_FREE(buf); + + if (msg->dest_path_size <= 0 || msg->dest_path_size > MS_FILE_PATH_LEN_MAX) { + MS_SAFE_FREE(buf); + MS_DBG_ERR("msg->dest_path_size is invalid %d", msg->dest_path_size ); + return MS_MEDIA_ERR_DATA_TAINTED; + } + + buf = (unsigned char*)malloc(msg->dest_path_size); + + if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) { + MS_DBG_ERR("recv failed : %s", strerror(errno)); + MS_SAFE_FREE(buf); + return _media_thumb_get_error(); + } + + strncpy(msg->dst_path, (char*)buf, msg->dest_path_size); + //MS_DBG("destination path : %s", msg->dst_path); + + MS_SAFE_FREE(buf); + return MS_MEDIA_ERR_NONE; +} + +int _ms_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size) +{ + int recv_msg_len = 0; + unsigned int from_addr_size = sizeof(struct sockaddr_un); + unsigned char *buf = NULL; + + buf = (unsigned char*)malloc(sizeof(thumbMsg)); + + recv_msg_len = ms_ipc_wait_message(sock, buf, sizeof(thumbMsg), from_addr, &from_addr_size); + if (recv_msg_len != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("ms_ipc_wait_message failed : %s", strerror(errno)); + MS_SAFE_FREE(buf); + return _media_thumb_get_error(); + } + + memcpy(msg, buf, header_size); + //MS_DBG("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size); + + if (msg->origin_path_size <= 0 || msg->origin_path_size > MS_FILE_PATH_LEN_MAX) { + MS_SAFE_FREE(buf); + MS_DBG_ERR("msg->origin_path_size is invalid %d", msg->origin_path_size ); + return MS_MEDIA_ERR_DATA_TAINTED; + } + + strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size); + //MS_DBG("original path : %s", msg->org_path); + + if (msg->dest_path_size <= 0 || msg->dest_path_size > MS_FILE_PATH_LEN_MAX) { + MS_SAFE_FREE(buf); + MS_DBG_ERR("msg->origin_path_size is invalid %d", msg->dest_path_size ); + return MS_MEDIA_ERR_DATA_TAINTED; + } + + strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size); + //MS_DBG("destination path : %s", msg->dst_path); + + MS_SAFE_FREE(buf); + *from_size = from_addr_size; + + return MS_MEDIA_ERR_NONE; +} + +int _ms_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size) +{ + if (req_msg == NULL || buf == NULL) { + return -1; + } + + int org_path_len = 0; + int dst_path_len = 0; + int size = 0; + int header_size = 0; + + header_size = sizeof(thumbMsg) - MAX_FILEPATH_LEN*2; + org_path_len = strlen(req_msg->org_path) + 1; + dst_path_len = strlen(req_msg->dst_path) + 1; + + MS_DBG_SLOG("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len); + + size = header_size + org_path_len + dst_path_len; + *buf = malloc(size); + memcpy(*buf, req_msg, header_size); + memcpy((*buf)+header_size, req_msg->org_path, org_path_len); + memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len); + + *buf_size = size; + + return 0; +} + +gboolean _ms_thumb_agent_recv_msg_from_server() +{ + if (g_communicate_sock <= 0) { + _ms_thumb_agent_prepare_udp_socket(); + } + + ms_thumb_server_msg recv_msg; + int recv_msg_size = 0; + + recv_msg_size = ms_ipc_receive_message(g_communicate_sock, & recv_msg, sizeof(ms_thumb_server_msg), NULL, NULL); + if (recv_msg_size != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("ms_ipc_receive_message failed : %s\n", strerror(errno)); + return FALSE; + } + + //MS_DBG("Receive : %d(%d)", recv_msg.msg_type, recv_msg_size); + if (recv_msg.msg_type == MS_MSG_THUMB_SERVER_READY) { + MS_DBG("Thumbnail server is ready"); + } + + return TRUE; +} + +gboolean _ms_thumb_agent_recv_thumb_done_from_server(GIOChannel *src, GIOCondition condition, gpointer data) +{ + int sockfd = -1; + + /* Once all-thumb extraction is done, check if there is queued all-thumb request */ + GSource *check_queued_all_thumb_request = NULL; + check_queued_all_thumb_request = g_idle_source_new (); + g_source_set_callback (check_queued_all_thumb_request, _ms_thumb_check_queued_request, NULL, NULL); + g_source_attach (check_queued_all_thumb_request, g_main_context_get_thread_default()); + + if (g_thumb_server_extracting == FALSE) { + MS_DBG_WARN("Recv thumb server extracting done already"); + return FALSE; + } + + sockfd = g_io_channel_unix_get_fd(src); + if (sockfd < 0) { + MS_DBG_ERR("sock fd is invalid!"); + return FALSE; + } + + ms_thumb_server_msg recv_msg; + int recv_msg_size = 0; + + MS_DBG_ERR("THUMB SERVER SOCKET %d", sockfd); + + recv_msg_size = ms_ipc_receive_message(sockfd, &recv_msg, sizeof(ms_thumb_server_msg), NULL, NULL); + if (recv_msg_size != MS_MEDIA_ERR_NONE) { + MS_DBG_ERR("ms_ipc_receive_message failed : %s\n", strerror(errno)); + return FALSE; + } + + MS_DBG("Receive : %d(%d)", recv_msg.msg_type, recv_msg_size); + if (recv_msg.msg_type == MS_MSG_THUMB_EXTRACT_ALL_DONE) { + MS_DBG("Thumbnail extracting done"); + g_thumb_server_extracting = FALSE; + + return FALSE; + } + + return FALSE; +} + +gboolean _ms_thumb_agent_execute_server() +{ + int pid; + pid = fork(); + + if (pid < 0) { + return FALSE; + } else if (pid == 0) { + execl("/usr/bin/media-thumbnail-server", "media-thumbnail-server", NULL); + } else { + MS_DBG("Child process is %d", pid); + g_folk_thumb_server = TRUE; + } + + g_server_pid = pid; + + if (!_ms_thumb_agent_recv_msg_from_server()) { + MS_DBG_ERR("_ms_thumb_agent_recv_msg_from_server is failed"); + return FALSE; + } + + return TRUE; +} + +gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *res_msg) +{ + int sock; + ms_sock_info_s sock_info; + struct sockaddr_un serv_addr; + int send_str_len = strlen(recv_msg->org_path); + + if (send_str_len > MAX_FILEPATH_LEN) { + MS_DBG_ERR("original path's length exceeds %d(max packet size)", MAX_FILEPATH_LEN); + return FALSE; + } + + if (ms_ipc_create_client_socket(MS_PROTOCOL_UDP, MS_TIMEOUT_SEC_10, &sock_info) < 0) { + MS_DBG_ERR("ms_ipc_create_client_socket failed"); + return FALSE; + } + + memset(&serv_addr, 0, sizeof(serv_addr)); + sock = sock_info.sock_fd; + serv_addr.sun_family = AF_UNIX; +// MS_DBG_SLOG("%s", MEDIA_IPC_PATH[MS_THUMB_DAEMON_PORT]); + strcpy(serv_addr.sun_path, MEDIA_IPC_PATH[MS_THUMB_DAEMON_PORT]); + + int buf_size = 0; + int header_size = 0; + unsigned char *buf = NULL; + _ms_thumb_set_buffer(recv_msg, &buf, &buf_size); + + //MS_DBG("buffer size : %d", buf_size); + if (sendto(sock, buf, buf_size, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != buf_size) { + MS_DBG_ERR("sendto failed: %s", strerror(errno)); + MS_SAFE_FREE(buf); + ms_ipc_delete_client_socket(&sock_info); + return FALSE; + } + + MS_SAFE_FREE(buf); + MS_DBG_SLOG("Sending msg to thumbnail server is successful"); + + struct sockaddr_un client_addr; + unsigned int client_addr_len; + header_size = sizeof(thumbMsg) - MAX_FILEPATH_LEN*2; + + if (_ms_thumb_recv_udp_msg(sock, header_size, res_msg, &client_addr, &client_addr_len) < 0) { + MS_DBG_ERR("_ms_thumb_recv_udp_msg failed"); + ms_ipc_delete_client_socket(&sock_info); + return FALSE; + } + + MS_DBG_SLOG("recv %s from thumb daemon is successful", res_msg->dst_path); + ms_ipc_delete_client_socket(&sock_info); + + if (res_msg->msg_type == 2 && g_communicate_sock > 0) { // THUMB_REQUEST_ALL_MEDIA + /* Create new channel to watch udp socket */ + GSource *source = NULL; + if (g_udp_channel == NULL) + g_udp_channel = g_io_channel_unix_new(g_communicate_sock); + source = g_io_create_watch(g_udp_channel, G_IO_IN); + + /* Set callback to be called when socket is readable */ + g_source_set_callback(source, (GSourceFunc)_ms_thumb_agent_recv_thumb_done_from_server, NULL, NULL); + g_source_attach(source, g_main_context_get_thread_default()); + + g_thumb_server_extracting = TRUE; + } + + return TRUE; +} + +gboolean _ms_thumb_check_queued_request(gpointer data) +{ + if (g_thumb_server_queued_all_extracting_request) { + MS_DBG_WARN("There is queued request"); + + /* request all-thumb extraction to thumbnail server */ + thumbMsg msg; + thumbMsg recv_msg; + memset((void *)&msg, 0, sizeof(msg)); + memset((void *)&recv_msg, 0, sizeof(recv_msg)); + + msg.msg_type = 2; // THUMB_REQUEST_ALL_MEDIA + msg.org_path[0] = '\0'; + msg.origin_path_size = 1; + msg.dst_path[0] = '\0'; + msg.dest_path_size = 1; + + /* Command All-thumb extraction to thumbnail server */ + if (!_ms_thumb_agent_send_msg_to_thumb_server(&msg, &recv_msg)) { + MS_DBG_ERR("_ms_thumb_agent_send_msg_to_thumb_server is failed"); + } + + g_thumb_server_queued_all_extracting_request = FALSE; + } else { + MS_DBG("There is no queued request"); + return FALSE; + } + + return FALSE; +} + +gboolean _ms_thumb_agent_timer() +{ + if (g_thumb_server_extracting) { + MS_DBG("Timer is called.. But media-thumbnail-server[%d] is busy.. so timer is recreated", g_server_pid); + + _ms_thumb_create_timer(g_timer_id); + return FALSE; + } + + g_timer_id = 0; + MS_DBG("Timer is called.. Now killing media-thumbnail-server[%d]", g_server_pid); + + if (g_server_pid > 0) { + /* Kill thumbnail server */ + thumbMsg msg; + thumbMsg recv_msg; + memset((void *)&msg, 0, sizeof(msg)); + memset((void *)&recv_msg, 0, sizeof(recv_msg)); + + msg.msg_type = 5; // THUMB_REQUEST_KILL_SERVER + msg.org_path[0] = '\0'; + msg.origin_path_size = 1; + msg.dst_path[0] = '\0'; + msg.dest_path_size = 1; + + /* Command Kill to thumbnail server */ + g_shutdowning_thumb_server = TRUE; + if (!_ms_thumb_agent_send_msg_to_thumb_server(&msg, &recv_msg)) { + MS_DBG_ERR("_ms_thumb_agent_send_msg_to_thumb_server is failed"); + g_shutdowning_thumb_server = FALSE; + } + usleep(200000); + + } else { + MS_DBG_ERR("g_server_pid is %d. Maybe there's problem in thumbnail-server", g_server_pid); + } + + return FALSE; +} + +int _ms_thumb_cancel_media(const char *path, int pid) +{ + int ret = -1; + int i = 0; + int req_len = 0; + + req_len = g_queue_get_length(g_request_queue); + + MS_DBG("Queue length : %d", req_len); + + for (i = 0; i < req_len; i++) { + thumbRequest *req = NULL; + req = (thumbRequest *)g_queue_peek_nth(g_request_queue, i); + if (req == NULL) continue; + + if ((req->recv_msg->pid) == pid && (strncmp(path, req->recv_msg->org_path, strlen(path))) == 0) { + MS_DBG("Remove %s from queue", req->recv_msg->org_path); + g_queue_pop_nth(g_request_queue, i); + + close(req->client_sock); + MS_SAFE_FREE(req->recv_msg); + MS_SAFE_FREE(req); + ret = 0; + + break; + } + } + + return ret; +} + +int _ms_thumb_cancel_all(int pid) +{ + int ret = -1; + int i = 0; + int req_len = 0; + + req_len = g_queue_get_length(g_request_queue); + + MS_DBG("Queue length : %d", req_len); + + for (i = 0; i < req_len; i++) { + thumbRequest *req = NULL; + req = (thumbRequest *)g_queue_peek_nth(g_request_queue, i); + if (req == NULL) continue; + + if (req->recv_msg->pid == pid) { + MS_DBG("Remove [%d] %s from queue", req->recv_msg->pid, req->recv_msg->org_path); + g_queue_pop_nth(g_request_queue, i); + i--; + req_len--; + + close(req->client_sock); + MS_SAFE_FREE(req->recv_msg); + MS_SAFE_FREE(req); + ret = 0; + } + } + + return ret; +} + +void _ms_thumb_cancle_request(thumbRequest *thumb_req) +{ + MS_DBG(""); + int ret = -1; + + if (thumb_req == NULL) return; + + thumbMsg *recv_msg = thumb_req->recv_msg; + if (recv_msg == NULL) { + MS_SAFE_FREE(thumb_req); + return; + } + + if (recv_msg->msg_type == 3) + ret = _ms_thumb_cancel_media(recv_msg->org_path, recv_msg->pid); + else if (recv_msg->msg_type == 4) + ret = _ms_thumb_cancel_all(recv_msg->pid); + + if (ret == 0) { + recv_msg->status = 0; // THUMB_SUCCESS + } else { + recv_msg->status = 0; // THUMB_SUCCESS + } + + if (recv_msg->origin_path_size <= 0 || recv_msg->origin_path_size > MS_FILE_PATH_LEN_MAX) { + MS_DBG_ERR("recv_msg->origin_path_size is invalid %d", recv_msg->origin_path_size ); + return; + } + + recv_msg->dest_path_size = recv_msg->origin_path_size; + strncpy(recv_msg->dst_path, recv_msg->org_path, recv_msg->dest_path_size); + + close(thumb_req->client_sock); + + MS_SAFE_FREE(thumb_req->recv_msg); + MS_SAFE_FREE(thumb_req); + + return; +} + +gboolean _ms_thumb_request_to_server(gpointer data) +{ + int req_len = 0; + + req_len = g_queue_get_length(g_request_queue); + + MS_DBG("Queue length : %d", req_len); + + if (req_len <= 0) { + MS_DBG("There is no request job in the queue"); + g_queue_work = 0; + return FALSE; + } + + if (g_shutdowning_thumb_server) { + MS_DBG_ERR("Thumb server is shutting down... wait for complete"); + usleep(10000); + return TRUE; + } + + if (g_folk_thumb_server == FALSE && g_thumb_server_extracting == FALSE) { + if(_ms_thumb_check_process() == FALSE) { // This logic is temporary + MS_DBG_WARN("Thumb server is not running.. so start it"); + if (!_ms_thumb_agent_execute_server()) { + MS_DBG_ERR("_ms_thumb_agent_execute_server is failed"); + g_queue_work = 0; + return FALSE; + } else { + _ms_thumb_create_timer(g_timer_id); + } + } + } else { + /* Timer is re-created*/ + _ms_thumb_create_timer(g_timer_id); + } + + thumbRequest *req = NULL; + req = (thumbRequest *)g_queue_pop_head(g_request_queue); + + if (req == NULL) { + MS_DBG_ERR("Failed to get a request job from queue"); + return TRUE; + } + + int client_sock = -1; + thumbMsg *recv_msg = NULL; + thumbMsg res_msg; + memset((void *)&res_msg, 0, sizeof(res_msg)); + + client_sock = req->client_sock; + recv_msg = req->recv_msg; + + if (req->client_sock <=0 || req->recv_msg == NULL) { + MS_DBG_ERR("client sock is below 0 or recv msg is NULL"); + MS_SAFE_FREE(req->recv_msg); + MS_SAFE_FREE(req); + return TRUE; + } + + if (recv_msg) { + if (recv_msg->msg_type == 2 && g_thumb_server_extracting) { // THUMB_REQUEST_ALL_MEDIA + MS_DBG_WARN("Thumbnail server is already extracting..This request is queued."); + g_thumb_server_queued_all_extracting_request = TRUE; + } else { + if (!_ms_thumb_agent_send_msg_to_thumb_server(recv_msg, &res_msg)) { + MS_DBG_ERR("_ms_thumb_agent_send_msg_to_thumb_server is failed"); + + close(client_sock); + MS_SAFE_FREE(req->recv_msg); + MS_SAFE_FREE(req); + return TRUE; + } + } + } else { + MS_DBG_ERR("recv_msg is NULL from queue request"); + } + + strncpy(res_msg.org_path, recv_msg->org_path, recv_msg->origin_path_size); + res_msg.origin_path_size = recv_msg->origin_path_size; + res_msg.dest_path_size = strlen(res_msg.dst_path) + 1; + + int buf_size = 0; + unsigned char *buf = NULL; + _ms_thumb_set_buffer(&res_msg, &buf, &buf_size); + + if (send(client_sock, buf, buf_size, 0) != buf_size) { + MS_DBG_ERR("sendto failed : %s", strerror(errno)); + } else { + MS_DBG_SLOG("Sent %s(%d) from %s", res_msg.dst_path, strlen(res_msg.dst_path), res_msg.org_path); + } + + close(client_sock); + MS_SAFE_FREE(buf); + MS_SAFE_FREE(req->recv_msg); + MS_SAFE_FREE(req); + + return TRUE; +} + +gboolean _ms_thumb_agent_read_socket(GIOChannel *src, + GIOCondition condition, + gpointer data) +{ + thumbMsg *recv_msg = NULL; + int header_size = 0; + int sock = -1; + int client_sock = -1; + + sock = g_io_channel_unix_get_fd(src); + if (sock < 0) { + MS_DBG_ERR("sock fd is invalid!"); + return TRUE; + } + + header_size = sizeof(thumbMsg) - MAX_FILEPATH_LEN*2; + + if (ms_ipc_accept_client_tcp(sock, &client_sock) < 0) { + MS_DBG_ERR("accept failed : %s", strerror(errno)); + return TRUE; + } + + recv_msg = calloc(1, sizeof(thumbMsg)); + if (recv_msg == NULL) { + MS_DBG_ERR("Failed to allocate memory"); + close(client_sock); + return TRUE; + } + + if (_ms_thumb_recv_msg(client_sock, header_size, recv_msg) < 0) { + MS_DBG_ERR("_ms_thumb_recv_msg failed "); + close(client_sock); + MS_SAFE_FREE(recv_msg); + return TRUE; + } + + MS_DBG_SLOG("Received [%d] %s(%d) from PID(%d)", recv_msg->msg_type, recv_msg->org_path, strlen(recv_msg->org_path), recv_msg->pid); + + thumbRequest *thumb_req = NULL; + thumb_req = calloc(1, sizeof(thumbRequest)); + if (thumb_req == NULL) { + MS_DBG_ERR("Failed to create request element"); + close(client_sock); + MS_SAFE_FREE(recv_msg); + return TRUE; + } + + thumb_req->client_sock = client_sock; + thumb_req->recv_msg = recv_msg; + + if (recv_msg->msg_type == 3 || recv_msg->msg_type == 4) { // THUMB_REQUEST_CANCEL_MEDIA || THUMB_REQUEST_CANCEL_ALL + _ms_thumb_cancle_request(thumb_req); + return TRUE; + } + + if (g_request_queue == NULL) { + MS_DBG_WARN("queue is init"); + g_request_queue = g_queue_new(); + } + + if (g_queue_get_length(g_request_queue) >= MAX_THUMB_REQUEST) { + MS_DBG_WARN("Request Queue is full"); + thumbMsg res_msg; + memset((void *)&res_msg, 0, sizeof(res_msg)); + + res_msg.msg_type = 6; // THUMB_RESPONSE + res_msg.status = 1; //THUMB_FAIL + res_msg.org_path[0] = '\0'; + res_msg.origin_path_size = 0; + res_msg.dst_path[0] = '\0'; + res_msg.dest_path_size = 0; + + int buf_size = 0; + unsigned char *buf = NULL; + _ms_thumb_set_buffer(&res_msg, &buf, &buf_size); + + if (send(client_sock, buf, buf_size, 0) != buf_size) { + MS_DBG_ERR("sendto failed : %s", strerror(errno)); + } else { + MS_DBG("Sent Refuse msg from %s", recv_msg->org_path); + } + + close(client_sock); + MS_SAFE_FREE(buf); + MS_SAFE_FREE(thumb_req->recv_msg); + MS_SAFE_FREE(thumb_req); + + return TRUE; + } + + MS_DBG_SLOG("%s is queued", recv_msg->org_path); + g_queue_push_tail(g_request_queue, (gpointer)thumb_req); + + if (!g_queue_work) { + GSource *src_request = NULL; + src_request = g_idle_source_new (); + g_source_set_callback (src_request, _ms_thumb_request_to_server, NULL, NULL); + //g_source_set_priority(src_request, G_PRIORITY_LOW); + g_source_attach (src_request, g_main_context_get_thread_default()); + g_queue_work = 1; + } + + return TRUE; +} + +gboolean _ms_thumb_agent_prepare_tcp_socket(int *sock_fd) +{ + int sock; + unsigned short serv_port; + + serv_port = MS_THUMB_CREATOR_PORT; + + if (ms_ipc_create_server_socket(MS_PROTOCOL_TCP, serv_port, &sock) < 0) { + MS_DBG_ERR("_ms_thumb_create_socket failed"); + return FALSE; + } + + *sock_fd = sock; + + return TRUE; +} + +gboolean _ms_thumb_agent_prepare_udp_socket() +{ + int sock; + unsigned short serv_port; + + serv_port = MS_THUMB_COMM_PORT; + + if (ms_ipc_create_server_socket(MS_PROTOCOL_UDP, serv_port, &sock) < 0) { + MS_DBG_ERR("ms_ipc_create_server_socket failed"); + return FALSE; + } + + g_communicate_sock = sock; + + return TRUE; +} + +gpointer ms_thumb_agent_start_thread(gpointer data) +{ + MS_DBG(""); + int sockfd = -1; + + GSource *source = NULL; + GIOChannel *channel = NULL; + GMainContext *context = NULL; + + /* Create and bind new TCP socket */ + if (!_ms_thumb_agent_prepare_tcp_socket(&sockfd)) { + MS_DBG_ERR("Failed to create socket"); + return NULL; + } + + context = g_main_context_new(); + + if (context == NULL) { + MS_DBG_ERR("g_main_context_new failed"); + } else { + MS_DBG("g_main_context_new success"); + } + + g_thumb_agent_loop = g_main_loop_new(context, FALSE); + g_main_context_push_thread_default(context); + + /* Create new channel to watch udp socket */ + channel = g_io_channel_unix_new(sockfd); + source = g_io_create_watch(channel, G_IO_IN); + + /* Set callback to be called when socket is readable */ + g_source_set_callback(source, (GSourceFunc)_ms_thumb_agent_read_socket, NULL, NULL); + g_source_attach(source, context); + + MS_DBG("************************************"); + MS_DBG("*** Thumbnail Agent thread is running ***"); + MS_DBG("************************************"); + + g_main_loop_run(g_thumb_agent_loop); + + MS_DBG("Thumbnail Agent thread is shutting down..."); + _ms_thumb_agent_finish_jobs(); + + /*close an IO channel*/ + g_io_channel_shutdown(channel, FALSE, NULL); + g_io_channel_shutdown(g_udp_channel, FALSE, NULL); + g_io_channel_unref(channel); + close(g_communicate_sock); + + g_main_loop_unref(g_thumb_agent_loop); + + return NULL; +} + diff --git a/wearable/media-server.manifest b/wearable/media-server.manifest new file mode 100644 index 0000000..bac7cc6 --- /dev/null +++ b/wearable/media-server.manifest @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.7.4