2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <sys/sendfile.h>
24 #include <sys/types.h>
28 #include <glib/gprintf.h>
32 #include "mtp_support.h"
33 #include "ptp_datacodes.h"
40 * mtp_uint32 _util_file_open(const mtp_char *filename,
41 * file_mode_t mode, mtp_int32 *error)
42 * This function opens the file in specific mode.
44 * @param[in] filename Specifies the name of file to open.
45 * @param[in] mode Specifies the mode of file to open.
46 * @param[out] error Specifies the type of error
47 * @return This function returns the file handle on success,
48 or INVALID_FILE on failure
50 FILE* _util_file_open(const mtp_char *filename, file_mode_t mode,
71 case MTP_FILE_READ | MTP_FILE_WRITE:
75 case MTP_FILE_READ | MTP_FILE_WRITE | MTP_FILE_APPEND:
80 ERR("Invalid mode : %d\n", mode);
86 fhandle = fopen(filename, fmode);
87 if (fhandle == NULL) {
88 ERR("File open Fail:mode[0x%x], errno [%d]\n", mode, errno);
89 ERR_SECURE("filename[%s]\n", filename);
95 fcntl(fileno(fhandle), F_SETFL, O_NOATIME);
99 #else /* __USE_STDIO__ */
101 mtp_int32 fhandle = 0;
111 flags = O_WRONLY | O_CREAT | O_TRUNC;
115 case MTP_FILE_APPEND:
116 flags = O_WRONLY | O_APPEND | O_CREAT;
120 case MTP_FILE_READ | MTP_FILE_WRITE:
124 case MTP_FILE_READ | MTP_FILE_WRITE | MTP_FILE_APPEND:
125 flags = O_RDWR | O_APPEND | O_CREAT;
130 ERR("Invalid mode : %d\n", mode);
136 fhandle = open(filename, flags, perm);
138 fhandle = open(filename, flags);
141 ERR("File open Fail:mode[0x%x], errno [%d]\n", mode, errno);
142 ERR_SECURE("filename[%s]\n", filename);
148 #endif /* __USE_STDIO__ */
152 * void _util_file_read(mtp_uint32 handle, void *bufptr, mtp_uint32 size,
153 * mtp_uint32 *preadcount)
155 * This function reads data from the file handle into the data buffer.
157 * @param[in] handle Specifies the handle of file to read.
158 * @param[out] bufptr Points to buff where data is to be read.
159 * @param[in] size Specifies the num bytes to be read.
160 * @param[out] preadcount Will store the actual num bytes read.
163 void _util_file_read(FILE* fhandle, void *bufptr, mtp_uint32 size,
164 mtp_uint32 *read_count)
166 mtp_uint32 bytes_read = 0;
169 bytes_read = fread_unlocked(bufptr, sizeof(mtp_char), size, fhandle);
170 #else /* __USE_STDIO__ */
171 bytes_read = read(fhandle, bufptr, size);
172 #endif /* __USE_STDIO__ */
174 *read_count = bytes_read;
177 * mtp_uint32 _util_file_write(mtp_uint32 fhandle, void *bufptr, mtp_uint32 size)
179 * This function writes data to the file using the data buffer passed.
181 * @param[in] handle Specifies the handle of file to write.
182 * @param[in] bufptr Points the buffer which holds the data.
183 * @param[in] size Specifies num bytes to be written.
184 * @return This function returns num bytes written.
187 mtp_uint32 _util_file_write(FILE* fhandle, void *bufptr, mtp_uint32 size)
189 mtp_uint32 bytes_written = 0;
192 bytes_written = fwrite_unlocked(bufptr, sizeof(mtp_char), size, fhandle);
193 #else /* __USE_STDIO__ */
196 ret = write(fhandle, bufptr, size);
201 #endif /* __USE_STDIO__ */
203 return bytes_written;
207 * mtp_int32 _util_file_close(mtp_uint32 fhandle)
208 * This function closes the file.
210 * @param[in] handle Specifies the handle of file to close.
211 * @return 0 in case of success or EOF on failure.
213 mtp_int32 _util_file_close(FILE* fhandle)
216 return fclose(fhandle);
217 #else /* __USE_STDIO__ */
218 return close(fhandle);
219 #endif /* __USE_STDIO__ */
223 * This function seeks to a particular location in a file.
225 * @param[in] handle Specifies the handle of file to seek.
226 * @param[in] offset Specifies the starting point.
227 * @param[in] whence Specifies the setting value
228 * @return Returns TRUE in case of success or FALSE on Failure.
230 mtp_bool _util_file_seek(FILE* handle, off_t offset, mtp_int32 whence)
232 mtp_int64 ret_val = 0;
235 ret_val = fseek(handle, offset, whence);
236 #else /* __USE_STDIO__ */
237 ret_val = lseek(handle, offset, whence);
240 #endif /* __USE_STDIO__ */
242 ERR(" _util_file_seek error errno [%d]\n", errno);
249 mtp_bool _util_file_copy(const mtp_char *origpath, const mtp_char *newpath,
257 mtp_char buf[BUFSIZ] = { 0 };
259 if ((fold = fopen(origpath, "rb")) == NULL) {
260 ERR("In-file open Fail errno [%d]\n", errno);
265 if ((fnew = fopen(newpath, "wb")) == NULL) {
266 ERR("Out-file open Fail errno [%d]\n", errno);
273 nmemb = fread(buf, sizeof(mtp_char), BUFSIZ, fold);
274 if (nmemb < BUFSIZ && ferror(fold)) {
275 ERR("fread Fail errno [%d] \n", errno);
279 if (remove(newpath) < 0)
284 ret = fwrite(buf, sizeof(mtp_char), nmemb, fnew);
285 if (ret < nmemb && ferror(fnew)) {
286 ERR("fwrite Fail errno [%d]\n", errno);
290 if (remove(newpath) < 0)
294 } while (!feof(fold));
298 #else /* __USE_STDIO__ */
300 mtp_int32 out_fd = 0;
304 if ((in_fd = open(origpath, O_RDONLY)) < 0) {
305 ERR("In-file open Fail, errno [%d]\n", errno);
310 if ((out_fd = open(newpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
311 ERR("Out-file open Fail errno [%d] \n", errno);
318 ret = sendfile(out_fd, in_fd, &offset, BUFSIZ);
320 ERR("sendfile Fail errno [%d]\n", errno);
324 if (remove(newpath) < 0)
328 } while (ret == BUFSIZ);
332 #endif /* __USE_STDIO__ */
337 mtp_bool _util_copy_dir_children_recursive(const mtp_char *origpath,
338 const mtp_char *newpath, mtp_int32 *error)
341 struct dirent entry = { 0 };
342 struct dirent *entryptr = NULL;
343 mtp_int32 retval = 0;
344 mtp_char old_pathname[MTP_MAX_PATHNAME_SIZE + 1] = { 0 };
345 mtp_char new_pathname[MTP_MAX_PATHNAME_SIZE + 1] = { 0 };
346 struct stat entryinfo;
348 retv_if(origpath == NULL, FALSE);
349 retv_if(newpath == NULL, FALSE);
351 /* Open the given directory */
352 dir = opendir(origpath);
354 ERR("opendir(%s) Fail", origpath);
359 retval = readdir_r(dir, &entry, &entryptr);
361 while (retval == 0 && entryptr != NULL) {
362 /* Skip the names "." and ".." as we don't want to recurse on them. */
363 if (!g_strcmp0(entry.d_name, ".") ||
364 !g_strcmp0(entry.d_name, "..")) {
365 retval = readdir_r(dir, &entry, &entryptr);
368 g_snprintf(old_pathname, MTP_MAX_PATHNAME_SIZE + 1,
369 "%s/%s", origpath, entry.d_name);
370 g_snprintf(new_pathname, MTP_MAX_PATHNAME_SIZE + 1,
371 "%s/%s", newpath, entry.d_name);
373 if (stat(old_pathname, &entryinfo) != 0) {
374 ERR("Error statting [%s] errno [%d]\n", old_pathname, errno);
379 if (S_ISDIR(entryinfo.st_mode)) {
380 if (FALSE == _util_dir_create(new_pathname, error)) {
381 /* dir already exists
382 merge the contents */
383 if (EEXIST != *error) {
384 ERR("directory[%s] create Fail errno [%d]\n", new_pathname, errno);
389 if (FALSE == _util_copy_dir_children_recursive(old_pathname,
390 new_pathname, error)) {
391 ERR("Recursive Copy of Children Fail\
392 [%s]->[%s], errno [%d]\n", old_pathname, new_pathname, errno);
397 if (FALSE == _util_file_copy(old_pathname, new_pathname, error)) {
398 ERR("file copy fail [%s]->[%s]\n",
399 old_pathname, new_pathname);
400 /* Cannot overwrite a read-only file,
401 Skip copy and retain the read-only file
403 if (EACCES == *error)
408 #ifdef MTP_SUPPORT_SET_PROTECTION
409 mtp_bool ret = FALSE;
411 if (!((S_IWUSR & entryInfo.st_mode) ||
412 (S_IWGRP & entryInfo.st_mode) ||
413 (S_IWOTH & entryInfo.st_mode))) {
414 ret = _util_set_file_attrs(newPathName,
415 MTP_FILE_ATTR_MODE_REG |
416 MTP_FILE_ATTR_MODE_READ_ONLY);
418 ERR("Failed to set directory attributes errno [%d]\n", errno);
423 #endif /* MTP_SUPPORT_SET_PROTECTION */
426 retval = readdir_r(dir, &entry, &entryptr);
430 return (retval == 0) ? TRUE : FALSE;
433 mtp_bool _util_file_move(const mtp_char *origpath, const mtp_char *newpath,
438 ret = rename(origpath, newpath);
440 if (errno == EXDEV) {
441 DBG("oldpath and newpath are not on the same\
442 mounted file system.");
443 if (_util_file_copy(origpath, newpath, error) == FALSE) {
444 ERR("_util_file_copy Fail errno [%d]\n", errno);
447 if (remove(origpath) < 0) {
448 ERR("remove Fail : %d\n", errno);
452 ERR("rename Fail : %d\n", errno);
461 mtp_bool _util_is_file_opened(const mtp_char *fullpath)
465 ret = rename(fullpath, fullpath);
469 mtp_bool _util_dir_create(const mtp_char *dirname, mtp_int32 *error)
472 if (mkdir(dirname, S_IRWXU | S_IRGRP |
473 S_IXGRP | S_IROTH | S_IXOTH) < 0) {
480 mtp_int32 _util_remove_dir_children_recursive(const mtp_char *dirname,
481 mtp_uint32 *num_of_deleted_file, mtp_uint32 *num_of_file, mtp_bool breadonly)
483 retv_if(dirname == NULL, FALSE);
486 struct dirent entry = { 0 };
487 struct dirent *entryptr = NULL;
488 mtp_int32 retval = 0;
489 mtp_char pathname[MTP_MAX_PATHNAME_SIZE + 1] = { 0 };
490 struct stat entryinfo;
491 mtp_int32 ret = MTP_ERROR_NONE;
493 /* Open the given directory */
494 dir = opendir(dirname);
496 ERR("Open directory Fail[%s], errno [%d]", dirname, errno);
497 return MTP_ERROR_GENERAL;
500 retval = readdir_r(dir, &entry, &entryptr);
502 while (retval == 0 && entryptr != NULL) {
503 /* Skip the names "." and ".."
504 as we don't want to recurse on them. */
505 if (!g_strcmp0(entry.d_name, ".") ||
506 !g_strcmp0(entry.d_name, "..")) {
507 retval = readdir_r(dir, &entry, &entryptr);
510 g_snprintf(pathname, MTP_MAX_PATHNAME_SIZE + 1,
511 "%s/%s", dirname, entry.d_name);
512 if (stat(pathname, &entryinfo) != 0) {
513 ERR("Error statting %s errno [%d]\n", pathname, errno);
515 return MTP_ERROR_GENERAL;
518 if (S_ISDIR(entryinfo.st_mode)) {
519 ret = _util_remove_dir_children_recursive(pathname,
520 num_of_deleted_file, num_of_file, breadonly);
521 if (MTP_ERROR_GENERAL == ret || MTP_ERROR_ACCESS_DENIED == ret) {
522 ERR("deletion fail [%s]\n", pathname);
526 if (MTP_ERROR_OBJECT_WRITE_PROTECTED == ret) {
527 DBG("Folder[%s] contains read-only files,hence\
528 folder is not deleted\n", pathname);
529 /* Read the next entry */
532 if (rmdir(pathname) < 0) {
533 ERR("deletion fail [%s], errno [%d]\n", pathname, errno);
536 return MTP_ERROR_ACCESS_DENIED;
537 return MTP_ERROR_GENERAL;
539 *num_of_deleted_file += 1;
541 /* Only during Deleteobject, bReadOnly(TRUE)
542 do not delete read-only files */
543 #ifdef MTP_SUPPORT_SET_PROTECTION
545 /* check for file attributes */
546 if (!((S_IWUSR & entryinfo.st_mode) ||
547 (S_IWGRP & entryinfo.st_mode) ||
548 (S_IWOTH & entryinfo.st_mode))) {
549 ret = MTP_ERROR_OBJECT_WRITE_PROTECTED;
550 DBG("File [%s] is readOnly:Deletion Fail\n", pathname);
554 #endif /* MTP_SUPPORT_SET_PROTECTION */
555 if (unlink(pathname) < 0) {
556 ERR("deletion fail [%s], errno [%d]\n", pathname, errno);
559 return MTP_ERROR_ACCESS_DENIED;
560 return MTP_ERROR_GENERAL;
562 *num_of_deleted_file += 1;
565 retval = readdir_r(dir, &entry, &entryptr);
568 return MTP_ERROR_GENERAL;
578 * mtp_bool _util_get_file_attrs(const mtp_char *filename, file_attr_t *attrs)
579 * This function gets the file attributes.
581 * @param[in] filename Specifies the name of file to find.
582 * @param[out] attrs Points the file Attributes.
583 * @return This function returns TRUE if gets the attributes
584 * successfully, otherwise FALSE.
586 mtp_bool _util_get_file_attrs(const mtp_char *filename, file_attr_t *attrs)
588 struct stat fileinfo = { 0 };
590 if (stat(filename, &fileinfo) < 0) {
591 ERR_SECURE("%s : stat Fail errno [%d]\n", filename, errno);
595 memset(attrs, 0, sizeof(file_attr_t));
596 attrs->fsize = (mtp_uint64)fileinfo.st_size;
597 attrs->ctime = fileinfo.st_ctime;
598 attrs->mtime = fileinfo.st_mtime;
600 /*Reset attribute mode */
601 attrs->attribute = MTP_FILE_ATTR_MODE_NONE;
602 if (S_ISREG(fileinfo.st_mode)) {
603 /* LCOV_EXCL_START */
604 attrs->attribute |= MTP_FILE_ATTR_MODE_REG;
605 if (!((S_IWUSR & fileinfo.st_mode) ||
606 (S_IWGRP & fileinfo.st_mode) ||
607 (S_IWOTH & fileinfo.st_mode))) {
608 attrs->attribute |= MTP_FILE_ATTR_MODE_READ_ONLY;
610 } else if (S_ISDIR(fileinfo.st_mode)) {
611 attrs->attribute |= MTP_FILE_ATTR_MODE_DIR;
612 } else if (S_ISBLK(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) ||
613 S_ISLNK(fileinfo.st_mode) || S_ISSOCK(fileinfo.st_mode)) {
614 attrs->attribute |= MTP_FILE_ATTR_MODE_SYSTEM;
619 mtp_bool _util_set_file_attrs(const mtp_char *filename, mtp_dword attrib)
623 if (MTP_FILE_ATTR_MODE_REG & attrib) {
625 if (MTP_FILE_ATTR_MODE_READ_ONLY & attrib)
626 attrs |= (S_IRUSR | S_IRGRP | S_IROTH);
628 attrs |= (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
630 /* do nothing for files other than File/Folder */
631 DBG("entered here nothing");
635 if (0 != chmod(filename, attrs)) {
638 ERR_SECURE("Change mode of [File : %s] Fail\n", filename);
647 * mtp_bool _util_ifind_first(mtp_char *dirname, DIR **dirp,
648 * dir_entry_t *dir_info)
649 * This function finds the first file in the directory stream.
651 * @param[in] dirname specifies the name of directory.
652 * @param[out] dirp pointer to the directory stream.
653 * @param[in] dir_info pointer to the file information.
654 * @return This function returns TRUE on success, otherwise FALSE.
656 mtp_bool _util_ifind_first(mtp_char *dirname, DIR **dirp, dir_entry_t *dir_info)
660 retv_if(dirp == NULL, FALSE);
661 retv_if(dirname == NULL, FALSE);
662 retv_if(dir_info == NULL, FALSE);
664 dir = opendir(dirname);
666 /* LCOV_EXCL_START */
667 ERR("opendir(%s) Fail", dirname);
673 if (_util_ifind_next(dirname, dir, dir_info) == FALSE) {
674 DBG("Stop enumeration");
687 * mtp_bool _util_ifind_next(mtp_char *dirname, DIR *dirp, dir_entry_t *dir_info)
688 * This function finds the next successive file in the directory stream.
690 * @param[in] dirname name of the directory.
691 * @param[in] dirp pointer to the directory stream.
692 * @param[out] dir_info Points the file information.
693 * @return This function returns TRUE on success, otherwise FALSE.
695 mtp_bool _util_ifind_next(mtp_char *dir_name, DIR *dirp, dir_entry_t *dir_info)
698 struct dirent entry = {0};
699 struct stat stat_buf = {0};
700 struct dirent *result = NULL;
701 mtp_char path_name[MTP_MAX_PATHNAME_SIZE + 1] = { 0 };
703 retv_if(dir_name == NULL, FALSE);
704 retv_if(dir_info == NULL, FALSE);
707 ret = readdir_r(dirp, &entry, &result);
709 ERR("readdir_r Fail : %d\n", ret);
711 } else if (result == NULL) {
712 DBG("There is no more entry");
716 if (_util_create_path(path_name, sizeof(path_name),
717 dir_name, entry.d_name) == FALSE) {
721 if (stat(path_name, &stat_buf) < 0) {
722 ERR_SECURE("stat Fail, skip [%s]\n", path_name);
728 g_strlcpy(dir_info->filename, path_name, sizeof(dir_info->filename));
729 dir_info->attrs.attribute = MTP_FILE_ATTR_MODE_NONE;
731 switch (stat_buf.st_mode & S_IFMT) {
733 dir_info->type = MTP_FILE_TYPE;
734 if (!(stat_buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
735 dir_info->attrs.attribute |= MTP_FILE_ATTR_MODE_READ_ONLY;
739 dir_info->type = MTP_DIR_TYPE;
740 dir_info->attrs.attribute = MTP_FILE_ATTR_MODE_DIR;
748 /* LCOV_EXCL_START */
749 dir_info->attrs.attribute |= MTP_FILE_ATTR_MODE_SYSTEM;
753 dir_info->attrs.attribute |= MTP_FILE_ATTR_MODE_SYSTEM;
754 ERR_SECURE("%s has unknown type. mode[0x%x]\n",
755 dir_info->filename, stat_buf.st_mode);
760 /* Directory Information */
761 dir_info->attrs.mtime = stat_buf.st_mtime;
762 dir_info->attrs.fsize = (mtp_uint64)stat_buf.st_size;
767 /* LCOV_EXCL_START */
768 mtp_bool _util_get_filesystem_info_ext(mtp_char *storepath,
771 struct statfs buf = { 0 };
772 mtp_uint64 avail_size = 0;
773 mtp_uint64 capacity = 0;
774 mtp_uint64 used_size = 0;
776 if (statfs(storepath, &buf) != 0) {
777 ERR("statfs is failed\n");
781 capacity = used_size = avail_size = (mtp_uint64)buf.f_bsize;
782 DBG("Block size : %d\n", buf.f_bsize);
783 capacity *= buf.f_blocks;
784 used_size *= (buf.f_blocks - buf.f_bavail);
785 avail_size *= buf.f_bavail;
787 fs_info->disk_size = capacity;
788 fs_info->reserved_size = used_size;
789 fs_info->avail_size = avail_size;
795 mtp_bool _util_get_filesystem_info_int(mtp_char *storepath, fs_info_t *fs_info)
800 mtp_uint64 avail_size = 0;
801 mtp_uint64 capacity = 0;
802 mtp_uint64 used_size = 0;
804 ret = storage_get_internal_memory_size(&s);
806 ERR("storage_get_internal_memory_size : ret = %d", ret);
810 capacity = (mtp_uint64)s.f_frsize*s.f_blocks;
811 avail_size = (mtp_uint64)s.f_bsize*s.f_bavail;
812 used_size = (capacity - avail_size);
814 DBG("total : %llu , avail %llu , used %llu", capacity, avail_size, used_size);
816 fs_info->disk_size = capacity;
817 fs_info->reserved_size = used_size;
818 fs_info->avail_size = avail_size;
823 mtp_bool _util_get_filesystem_info(mtp_char *storepath,
826 if (!g_strcmp0(storepath, MTP_EXTERNAL_PATH_CHAR))
827 return _util_get_filesystem_info_ext(storepath, fs_info);
829 return _util_get_filesystem_info_int(storepath, fs_info);
834 /* LCOV_EXCL_START */
835 void _util_count_num_lines(FILE* fhandle, mtp_uint32 *num_lines)
840 mtp_uint32 line_count = 0;
841 mtp_char *buffer = NULL;
842 mtp_int32 read_bytes;
843 mtp_uint32 line_max_length;
845 line_max_length = LINUX_MAX_PATHNAME_LENGTH + 2;
846 buffer = (mtp_char *)g_malloc(line_max_length);
847 if (buffer == NULL) {
853 while ((read_bytes = getline(&buffer,
854 (size_t *)&line_max_length, fhandle)) != -1) {
855 if (read_bytes > MTP_MAX_PATHNAME_SIZE + 1)
859 #else /* __USE_STDIO__ */
862 mtp_uint32 prev_pos = -1;
864 mtp_uint32 filename_len = 0;
865 while ((read_bytes = read(fhandle, buffer, LINUX_MAX_PATHNAME_LENGTH)) > 0) {
866 for (ii = 0; ii < read_bytes; ii++) {
867 if (buffer[ii] != '\n')
870 filename_len = new_pos - prev_pos -1;
872 if (filename_len > MTP_MAX_PATHNAME_SIZE)
876 if (buffer[read_bytes - 1] != '\n')
877 _util_file_seek(fhandle, prev_pos + 1 - read_bytes, SEEK_CUR);
881 #endif /* __USE_STDIO__ */
883 *num_lines = line_count;
888 void _util_fill_guid_array(void *guidarray, mtp_uint32 start_index,
889 FILE* fhandle, mtp_uint32 size)
891 ptp_array_t *pguidarray = NULL;
892 mtp_uint32 *guidptr = NULL;
893 mtp_uint32 num_lines = 0;
894 mtp_wchar objfullpath[MTP_MAX_PATHNAME_SIZE * 2 + 1] = { 0 };
895 mtp_char guid[16] = { 0 };
896 mtp_char *buffer = NULL;
897 mtp_uint32 line_max_length;
900 line_max_length = LINUX_MAX_PATHNAME_LENGTH + 2;
901 pguidarray = (ptp_array_t *)guidarray;
902 guidptr = (mtp_uint32 *)(pguidarray->array_entry);
904 buffer = (mtp_char *)g_malloc(line_max_length);
905 if (buffer == NULL) {
911 while ((len = getline(&buffer, (size_t *)&line_max_length, fhandle)) != -1 &&
912 (num_lines - start_index) <= size) {
913 if (len > MTP_MAX_PATHNAME_SIZE + 1)
916 if (num_lines < start_index)
918 buffer[len - 1] = '\0';
919 _util_utf8_to_utf16(objfullpath,
920 sizeof(objfullpath) / WCHAR_SIZ, buffer);
921 _util_conv_wstr_to_guid(objfullpath, (mtp_uint64 *)guid);
922 memcpy(&(guidptr[pguidarray->num_ele]),
924 pguidarray->num_ele += sizeof(mtp_uint32);
926 #else /* __USE_STDIO__ */
928 mtp_uint32 prev_pos = -1;
930 mtp_char file_name[MTP_MAX_PATHNAME_SIZE + 1];
931 mtp_int32 read_bytes;
933 while ((read_bytes = read(fhandle, buffer,
934 LINUX_MAX_PATHNAME_LENGTH)) > 0 &&
935 (num_lines - start_index) <= size) {
937 for (ii = 0; ii < read_bytes; ii++) {
938 if (buffer[ii] != '\n')
941 len = new_pos - prev_pos - 1;
943 if (len > MTP_MAX_PATHNAME_SIZE)
946 if (num_lines < start_index)
948 strncpy(file_name, &buffer[new_pos - len], len);
949 file_name[len] = '\0';
950 _util_utf8_to_utf16(objfullpath,
951 sizeof(objfullpath) / WCHAR_SIZ, file_name);
952 _util_conv_wstr_to_guid(objfullpath, (mtp_uint64 *)guid);
953 memcpy(&(guidptr[pguidarray->num_elements]),
955 pguidarray->num_elements += sizeof(mtp_uint32);
958 if (buffer[read_bytes - 1] != '\n')
959 _util_file_seek(fhandle, prev_pos + 1 - read_bytes, SEEK_CUR);
963 #endif /* __USE_STDIO__ */
971 * void FLOGD(const char *fmt, ...)
972 * This function writes MTP debug message to MTP log file
974 * @param[in] fmt Formatted debug message.
978 void FLOGD(const char *fmt, ...)
980 static mtp_int64 written_bytes = 0;
984 if (written_bytes == 0 || written_bytes > MTP_LOG_MAX_SIZE) {
985 fp = fopen(MTP_LOG_FILE, "w");
988 fp = fopen(MTP_LOG_FILE, "a+");
994 written_bytes += fprintf(fp, "%s ", __FILE__);
996 written_bytes += vfprintf(fp, fmt, ap);
1002 /* LCOV_EXCL_STOP */