5 * Copyright (C) 2009-2010 Intel Corporation
6 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <sys/types.h>
38 #include <sys/statvfs.h>
39 #include <sys/sendfile.h>
50 #include "filesystem.h"
52 #define EOL_CHARS "\n"
54 #define FL_VERSION "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" EOL_CHARS
56 #define FL_TYPE "<!DOCTYPE folder-listing SYSTEM \"obex-folder-listing.dtd\">" EOL_CHARS
58 #define FL_TYPE_PCSUITE "<!DOCTYPE folder-listing SYSTEM \"obex-folder-listing.dtd\"" EOL_CHARS \
59 " [ <!ATTLIST folder mem-type CDATA #IMPLIED> ]>" EOL_CHARS
61 #define FL_BODY_BEGIN "<folder-listing version=\"1.0\">" EOL_CHARS
63 #define FL_BODY_END "</folder-listing>" EOL_CHARS
65 #define FL_PARENT_FOLDER_ELEMENT "<parent-folder/>" EOL_CHARS
67 #define FL_FILE_ELEMENT "<file name=\"%s\" size=\"%" PRIu64 "\"" \
68 " %s accessed=\"%s\" " \
69 "modified=\"%s\" created=\"%s\"/>" EOL_CHARS
71 #define FL_FOLDER_ELEMENT "<folder name=\"%s\" %s accessed=\"%s\" " \
72 "modified=\"%s\" created=\"%s\"/>" EOL_CHARS
74 #define FL_FOLDER_ELEMENT_PCSUITE "<folder name=\"%s\" %s accessed=\"%s\"" \
75 " modified=\"%s\" mem-type=\"DEV\"" \
76 " created=\"%s\"/>" EOL_CHARS
78 #define FTP_TARGET_SIZE 16
80 static const uint8_t FTP_TARGET[FTP_TARGET_SIZE] = {
81 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2,
82 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 };
84 #define PCSUITE_WHO_SIZE 8
86 static const uint8_t PCSUITE_WHO[PCSUITE_WHO_SIZE] = {
87 'P', 'C', ' ', 'S', 'u', 'i', 't', 'e' };
89 gboolean is_filename(const char *name)
91 if (strchr(name, '/'))
94 if (strcmp(name, ".") == 0)
97 if (strcmp(name, "..") == 0)
103 int verify_path(const char *path)
108 if (obex_option_symlinks())
111 t = realpath(path, NULL);
115 if (!g_str_has_prefix(t, obex_option_root_folder()))
123 static char *file_stat_line(char *filename, struct stat *fstat,
124 struct stat *dstat, gboolean root,
127 char perm[51], atime[18], ctime[18], mtime[18];
128 char *escaped, *ret = NULL;
130 snprintf(perm, 50, "user-perm=\"%s%s%s\" group-perm=\"%s%s%s\" "
131 "other-perm=\"%s%s%s\"",
132 (fstat->st_mode & S_IRUSR ? "R" : ""),
133 (fstat->st_mode & S_IWUSR ? "W" : ""),
134 (dstat->st_mode & S_IWUSR ? "D" : ""),
135 (fstat->st_mode & S_IRGRP ? "R" : ""),
136 (fstat->st_mode & S_IWGRP ? "W" : ""),
137 (dstat->st_mode & S_IWGRP ? "D" : ""),
138 (fstat->st_mode & S_IROTH ? "R" : ""),
139 (fstat->st_mode & S_IWOTH ? "W" : ""),
140 (dstat->st_mode & S_IWOTH ? "D" : ""));
142 strftime(atime, 17, "%Y%m%dT%H%M%SZ", gmtime(&fstat->st_atime));
143 strftime(ctime, 17, "%Y%m%dT%H%M%SZ", gmtime(&fstat->st_ctime));
144 strftime(mtime, 17, "%Y%m%dT%H%M%SZ", gmtime(&fstat->st_mtime));
146 escaped = g_markup_escape_text(filename, -1);
148 if (S_ISDIR(fstat->st_mode)) {
149 if (pcsuite && root && g_str_equal(filename, "Data"))
150 ret = g_strdup_printf(FL_FOLDER_ELEMENT_PCSUITE,
151 escaped, perm, atime,
154 ret = g_strdup_printf(FL_FOLDER_ELEMENT, escaped, perm,
155 atime, mtime, ctime);
156 } else if (S_ISREG(fstat->st_mode))
157 ret = g_strdup_printf(FL_FILE_ELEMENT, escaped,
158 (uint64_t) fstat->st_size,
159 perm, atime, mtime, ctime);
166 static void *filesystem_open(const char *name, int oflag, mode_t mode,
167 void *context, size_t *size, int *err)
174 fd = open(name, oflag, mode);
181 if (fstat(fd, &stats) < 0) {
187 ret = verify_path(name);
194 if (oflag == O_RDONLY) {
196 *size = stats.st_size;
200 if (fstatvfs(fd, &buf) < 0) {
209 avail = (uint64_t) buf.f_bsize * buf.f_bavail;
220 return GINT_TO_POINTER(fd);
227 static int filesystem_close(void *object)
229 if (close(GPOINTER_TO_INT(object)) < 0)
235 static ssize_t filesystem_read(void *object, void *buf, size_t count)
239 ret = read(GPOINTER_TO_INT(object), buf, count);
246 static ssize_t filesystem_write(void *object, const void *buf, size_t count)
250 ret = write(GPOINTER_TO_INT(object), buf, count);
257 static int filesystem_rename(const char *name, const char *destname)
261 ret = rename(name, destname);
263 error("rename(%s, %s): %s (%d)", name, destname,
264 strerror(errno), errno);
271 static int sendfile_async(int out_fd, int in_fd, off_t *offset, size_t count)
275 /* Run sendfile on child process */
281 error("fork() %s (%d)", strerror(errno), errno);
284 DBG("child %d forked", pid);
289 if (sendfile(out_fd, in_fd, offset, count) < 0)
290 error("sendfile(): %s (%d)", strerror(errno), errno);
298 static int filesystem_copy(const char *name, const char *destname)
304 int in_fd, out_fd, err;
306 in = filesystem_open(name, O_RDONLY, 0, NULL, &size, &err);
308 error("open(%s): %s (%d)", name, strerror(-err), -err);
312 in_fd = GPOINTER_TO_INT(in);
313 ret = fstat(in_fd, &st);
315 error("stat(%s): %s (%d)", name, strerror(errno), errno);
319 out = filesystem_open(destname, O_WRONLY | O_CREAT | O_TRUNC,
320 st.st_mode, NULL, &size, &err);
322 error("open(%s): %s (%d)", destname, strerror(-err), -err);
323 filesystem_close(in);
327 out_fd = GPOINTER_TO_INT(out);
329 /* Check if sendfile is supported */
330 ret = sendfile(out_fd, in_fd, NULL, 0);
333 error("sendfile: %s (%zd)", strerror(-ret), -ret);
337 ret = sendfile_async(out_fd, in_fd, NULL, st.st_size);
344 filesystem_close(in);
345 filesystem_close(out);
350 struct capability_object {
358 static void script_exited(GPid pid, int status, void *data)
360 struct capability_object *object = data;
365 DBG("pid: %d status: %d", pid, status);
367 g_spawn_close_pid(pid);
369 /* free the object if aborted */
370 if (object->aborted) {
371 if (object->buffer != NULL)
372 g_string_free(object->buffer, TRUE);
378 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
379 memset(buf, 0, sizeof(buf));
380 if (read(object->err, buf, sizeof(buf)) > 0)
382 obex_object_set_io_flags(data, G_IO_ERR, -EPERM);
384 obex_object_set_io_flags(data, G_IO_IN, 0);
387 static int capability_exec(const char **argv, int *output, int *err)
391 GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH;
393 if (!g_spawn_async_with_pipes(NULL, (char **) argv, NULL, flags, NULL,
394 NULL, &pid, NULL, output, err, &gerr)) {
395 error("%s", gerr->message);
400 DBG("executing %s pid %d", argv[0], pid);
405 static void *capability_open(const char *name, int oflag, mode_t mode,
406 void *context, size_t *size, int *err)
408 struct capability_object *object = NULL;
412 if (oflag != O_RDONLY)
415 object = g_new0(struct capability_object, 1);
420 if (name[0] != '!') {
424 ret = g_file_get_contents(name, &buf, NULL, &gerr);
426 error("%s", gerr->message);
431 object->buffer = g_string_new(buf);
434 *size = object->buffer->len;
442 object->pid = capability_exec(argv, &object->output, &object->err);
446 /* Watch cannot be removed while the process is still running */
447 g_child_watch_add(object->pid, script_exited, object);
463 static GString *append_pcsuite_preamble(GString *object)
465 return g_string_append(object, FL_TYPE_PCSUITE);
468 static GString *append_folder_preamble(GString *object)
470 return g_string_append(object, FL_TYPE);
473 static GString *append_listing(GString *object, const char *name,
474 gboolean pcsuite, size_t *size, int *err)
476 struct stat fstat, dstat;
482 root = g_str_equal(name, obex_option_root_folder());
492 object = g_string_append(object, FL_PARENT_FOLDER_ELEMENT);
494 ret = verify_path(name);
500 ret = stat(name, &dstat);
507 while ((ep = readdir(dp))) {
512 if (ep->d_name[0] == '.')
515 filename = g_filename_to_utf8(ep->d_name, -1, NULL, NULL, NULL);
516 if (filename == NULL) {
517 error("g_filename_to_utf8: invalid filename");
521 fullname = g_build_filename(name, ep->d_name, NULL);
523 ret = stat(fullname, &fstat);
525 DBG("stat: %s(%d)", strerror(errno), errno);
533 line = file_stat_line(filename, &fstat, &dstat, root, FALSE);
541 object = g_string_append(object, line);
547 object = g_string_append(object, FL_BODY_END);
560 g_string_free(object, TRUE);
564 static void *folder_open(const char *name, int oflag, mode_t mode,
565 void *context, size_t *size, int *err)
569 object = g_string_new(FL_VERSION);
570 object = append_folder_preamble(object);
571 object = g_string_append(object, FL_BODY_BEGIN);
573 return append_listing(object, name, FALSE, size, err);
576 static void *pcsuite_open(const char *name, int oflag, mode_t mode,
577 void *context, size_t *size, int *err)
581 object = g_string_new(FL_VERSION);
582 object = append_pcsuite_preamble(object);
583 object = g_string_append(object, FL_BODY_BEGIN);
585 return append_listing(object, name, TRUE, size, err);
588 static int string_free(void *object)
590 GString *string = object;
592 g_string_free(string, TRUE);
597 ssize_t string_read(void *object, void *buf, size_t count)
599 GString *string = object;
602 if (string->len == 0)
605 len = MIN(string->len, count);
606 memcpy(buf, string->str, len);
607 g_string_erase(string, 0, len);
612 static ssize_t folder_read(void *object, void *buf, size_t count)
614 return string_read(object, buf, count);
617 static ssize_t capability_read(void *object, void *buf, size_t count)
619 struct capability_object *obj = object;
622 return string_read(obj->buffer, buf, count);
627 return read(obj->output, buf, count);
630 static int capability_close(void *object)
632 struct capability_object *obj = object;
638 DBG("kill: pid %d", obj->pid);
639 err = kill(obj->pid, SIGTERM);
642 error("kill: %s (%d)", strerror(-err), -err);
650 if (obj->buffer != NULL)
651 g_string_free(obj->buffer, TRUE);
658 static struct obex_mime_type_driver file = {
659 .open = filesystem_open,
660 .close = filesystem_close,
661 .read = filesystem_read,
662 .write = filesystem_write,
664 .move = filesystem_rename,
665 .copy = filesystem_copy,
668 static struct obex_mime_type_driver capability = {
669 .target = FTP_TARGET,
670 .target_size = FTP_TARGET_SIZE,
671 .mimetype = "x-obex/capability",
672 .open = capability_open,
673 .close = capability_close,
674 .read = capability_read,
677 static struct obex_mime_type_driver folder = {
678 .target = FTP_TARGET,
679 .target_size = FTP_TARGET_SIZE,
680 .mimetype = "x-obex/folder-listing",
682 .close = string_free,
686 static struct obex_mime_type_driver pcsuite = {
687 .target = FTP_TARGET,
688 .target_size = FTP_TARGET_SIZE,
690 .who_size = PCSUITE_WHO_SIZE,
691 .mimetype = "x-obex/folder-listing",
692 .open = pcsuite_open,
693 .close = string_free,
697 static int filesystem_init(void)
701 err = obex_mime_type_driver_register(&folder);
705 err = obex_mime_type_driver_register(&capability);
709 err = obex_mime_type_driver_register(&pcsuite);
713 return obex_mime_type_driver_register(&file);
716 static void filesystem_exit(void)
718 obex_mime_type_driver_unregister(&folder);
719 obex_mime_type_driver_unregister(&capability);
720 obex_mime_type_driver_unregister(&file);
723 OBEX_PLUGIN_DEFINE(filesystem, filesystem_init, filesystem_exit)