{"action":"opened","issue":{"url":"https://api.github.com/repos/ARMmbed/littlefs/issues/110","repository_url":"https://api.github.com/repos/ARMmbed/littlefs","labels_url":"https://api.github.com/repos/ARMmbed/littlefs/issues/110/labels{/name}","comments_url":"https://api.github.com/repos/ARMmbed/littlefs/issues/110/comments","events_url":"https://api.github.com/repos/ARMmbed/littlefs/issues/110/events","html_url":"https://github.com/ARMmbed/littlefs/issues/110","id":365426073,"node_id":"MDU6SXNzdWUzNjU0MjYwNzM=","number":110,"title":"lfs_rename does not always work","user":{"login":"ohoc","id":37809460,"node_id":"MDQ6VXNlcjM3ODA5NDYw","avatar_url":"https://avatars2.githubusercontent.com/u/37809460?v=4","gravatar_id":"","url":"https://api.github.com/users/ohoc","html_url":"https://github.com/ohoc","followers_url":"https://api.github.com/users/ohoc/followers","following_url":"https://api.github.com/users/ohoc/following{/other_user}","gists_url":"https://api.github.com/users/ohoc/gists{/gist_id}","starred_url":"https://api.github.com/users/ohoc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ohoc/subscriptions","organizations_url":"https://api.github.com/users/ohoc/orgs","repos_url":"https://api.github.com/users/ohoc/repos","events_url":"https://api.github.com/users/ohoc/events{/privacy}","received_events_url":"https://api.github.com/users/ohoc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-01T11:52:35Z","updated_at":"2018-10-01T11:52:35Z","closed_at":null,"author_association":"NONE","body":"Hi all,\r\nI am having problems with lfs_rename(): in certain cases, the file is not renamed, and I have traced it back to when lfs_dir_append() needs to allocate a new block.\r\n\r\nI have attached a small test program below that reproduces the problem.\r\nI would expect to have only \".txt\" files listed, but each 7th file is not renamed and is still a \".tmp\".\r\nThe problem occurs more often if the block size is smaller, and less often when the block size is bigger.\r\n\r\nAny idea where the problem can be?\r\n\r\nThanks.\r\n\r\n\r\n```C\r\n#include <string.h>\r\n#include <stdio.h>\r\n\r\n#include \"lfs.h\"\r\n\r\n#define TOTAL_SIZE   (1024*1024)\r\n#define BLOCK_SIZE   256\r\n#define BLOCK_COUNT  (TOTAL_SIZE/BLOCK_SIZE)\r\n\r\nint ram_read(const struct lfs_config *c, lfs_block_t block,\r\n        lfs_off_t off, void *buffer, lfs_size_t size);\r\nint ram_prog(const struct lfs_config *c, lfs_block_t block,\r\n        lfs_off_t off, const void *buffer, lfs_size_t size);\r\nint ram_erase(const struct lfs_config *c, lfs_block_t block);\r\nint ram_sync(const struct lfs_config *c);\r\n\r\nuint8_t ramfs[TOTAL_SIZE];\r\nlfs_t lfs;\r\nlfs_file_t file;\r\n\r\nconst struct lfs_config cfg = {\r\n    .read  = ram_read,\r\n    .prog  = ram_prog,\r\n    .erase = ram_erase,\r\n    .sync  = ram_sync,\r\n\r\n    .read_size = 16,\r\n    .prog_size = 16,\r\n    .block_size = BLOCK_SIZE,\r\n    .block_count = BLOCK_COUNT,\r\n    .lookahead = 128,\r\n};\r\n\r\nint main(void) {\r\n\r\n    int err;\r\n    lfs_format(&lfs, &cfg);\r\n    lfs_mount(&lfs, &cfg);\r\n\r\n    char filename1[64];\r\n    char filename2[64];\r\n    for (unsigned int i = 0; i < 32; ++i) {\r\n        sprintf(filename1, \"testfile_%08X.tmp\", i);\r\n        sprintf(filename2, \"testfile_%08X.txt\", i);\r\n        lfs_file_open(&lfs, &file, filename1, LFS_O_RDWR | LFS_O_CREAT);\r\n        lfs_file_write(&lfs, &file, \"asdf\\n\", 5);\r\n        lfs_file_close(&lfs, &file);\r\n        lfs_rename(&lfs, filename1, filename2);\r\n    }\r\n\r\n    lfs_dir_t dir;\r\n    lfs_dir_open(&lfs, &dir, \".\");\r\n\r\n    struct lfs_info info;\r\n    while (true) {\r\n        int res = lfs_dir_read(&lfs, &dir, &info);\r\n        if (res < 0) {\r\n            break;\r\n        }\r\n\r\n        if (res == 0) {\r\n            break;\r\n        }\r\n\r\n        switch (info.type) {\r\n            case LFS_TYPE_REG: printf(\"reg \"); break;\r\n            case LFS_TYPE_DIR: printf(\"dir \"); break;\r\n            default:           printf(\"?   \"); break;\r\n        }\r\n\r\n        static const char *prefixes[] = {\"\", \"K\", \"M\", \"G\"};\r\n        for (int i = sizeof(prefixes)/sizeof(prefixes[0])-1; i >= 0; i--) {\r\n            if (info.size >= (1 << 10*i)-1) {\r\n                printf(\"%*u%sB \", 4-(i != 0), info.size >> 10*i, prefixes[i]);\r\n                break;\r\n            }\r\n        }\r\n\r\n        printf(\"%s\\n\", info.name);\r\n    }\r\n\r\n    lfs_dir_close(&lfs, &dir);\r\n\r\n    lfs_unmount(&lfs);\r\n}\r\n\r\nint ram_read(const struct lfs_config *c, lfs_block_t block,\r\n        lfs_off_t off, void *buffer, lfs_size_t size)\r\n{\r\n    memcpy(buffer, ramfs + block*c->block_size + off, size);\r\n    return 0;\r\n}\r\n\r\nint ram_prog(const struct lfs_config *c, lfs_block_t block,\r\n        lfs_off_t off, const void *buffer, lfs_size_t size)\r\n{\r\n    memcpy(ramfs + block*c->block_size + off, buffer, size);\r\n    return 0;\r\n}\r\n\r\nint ram_erase(const struct lfs_config *c, lfs_block_t block)\r\n{\r\n    return 0;\r\n}\r\n\r\nint ram_sync(const struct lfs_config *c)\r\n{\r\n    return 0;\r\n}\r\n```\r\n"}}
{"actor":{"display_login":"ohoc"}}