Skip to content

Added support for read_file() to read lines from end of file#797

Merged
thefallentree merged 2 commits intofluffos:masterfrom
jalbright015:master
Apr 19, 2021
Merged

Added support for read_file() to read lines from end of file#797
thefallentree merged 2 commits intofluffos:masterfrom
jalbright015:master

Conversation

@jalbright015
Copy link
Copy Markdown
Contributor

No description provided.

@jalbright015
Copy link
Copy Markdown
Contributor Author

Added support for read_file() to read lines from end of file using a negative number for the second argument.

Example: read_file("/some_file", -10)

The above example would read the file starting 10 lines from the end of the file.

if (*ptr_start == '\0') {
debug(file, "read_file: file contains '\\0': %s.\n", file);
return nullptr;
const char *ptr_start;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you still want const char *ptr_start = theBuff;

otherwise read_file(0, 1) will fail

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have ptr_start = theBuff; below that for if the line parameter is a positive number, and ptr_start = theBuff + total_bytes_read; if it is negative.

@gesslar
Copy link
Copy Markdown
Contributor

gesslar commented Apr 18, 2021

I will update the docs once this has merged.

mid = implode(explode(foo, "\n")[<14..<5], "\n") + "\n";
ASSERT_EQ(mid, read_file("/testfile", -15, 10));
// if we start too many lines before the end of the file, start at the beginning
ASSERT(read_file("/testfile", -10000, 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you want ASSERT_EQ(implode(explode(foo, "\n")[0..0], read_file(...) )

@thefallentree
Copy link
Copy Markdown
Member

and here is the crash logs:

2021-04-18T22:03:57.1802814Z #17   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 322, in safe_apply [0x55d4c81d188a]
2021-04-18T22:03:57.1803386Z         320:   svalue_t *ret = nullptr;
2021-04-18T22:03:57.1803679Z         321:   try {
2021-04-18T22:03:57.1804005Z       > 322:     ret = apply(fun, ob, num_arg, where);
2021-04-18T22:03:57.1804351Z         323:   } catch (const char *) {
2021-04-18T22:03:57.1804689Z         324:     restore_context(&econ);
2021-04-18T22:03:57.1805026Z         325:     pop_n_elems(num_arg);
2021-04-18T22:03:57.1805554Z #16   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 293, in apply [0x55d4c81d151d]
2021-04-18T22:03:57.1806046Z         290: #ifdef DEBUG
2021-04-18T22:03:57.1806548Z         291:   expected_sp = sp - num_arg;
2021-04-18T22:03:57.1806859Z         292: #endif
2021-04-18T22:03:57.1807175Z       > 293:   if (apply_low(fun, ob, num_arg) == 0) {
2021-04-18T22:03:57.1807509Z         294:     return nullptr;
2021-04-18T22:03:57.1807898Z         295:   }
2021-04-18T22:03:57.1808241Z         296:   free_svalue(&apply_ret_value, "sapply");
2021-04-18T22:03:57.1808825Z #15   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 267, in apply_low [0x55d4c81d122a]
2021-04-18T22:03:57.1809332Z         264: #ifdef DEBUG
2021-04-18T22:03:57.1809622Z         265:     save_csp = csp;
2021-04-18T22:03:57.1809909Z         266: #endif
2021-04-18T22:03:57.1810453Z       > 267:     call_program(current_prog, funp->address);
2021-04-18T22:03:57.1811169Z         268:     DEBUG_CHECK(save_csp - 1 != csp, "Bad csp after execution in apply_low.\n");
2021-04-18T22:03:57.1811595Z         269:     return 1;
2021-04-18T22:03:57.1811850Z         270:   }
2021-04-18T22:03:57.1812396Z #14   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3665, in eval_instruction [0x55d4c80fa312]
2021-04-18T22:03:57.1813005Z        3662:         LOAD_SHORT(offset, pc);
2021-04-18T22:03:57.1813584Z        3663:         offset = (pc - 2) + offset - current_prog->program;
2021-04-18T22:03:57.1814048Z        3664: 
2021-04-18T22:03:57.1814520Z       >3665:         do_catch(pc, offset);
2021-04-18T22:03:57.1815510Z        3666:         if ((csp[1].framekind & (FRAME_EXTERNAL | FRAME_RETURNED_FROM_CATCH)) ==
2021-04-18T22:03:57.1816548Z        3667:             (FRAME_EXTERNAL | FRAME_RETURNED_FROM_CATCH)) {
2021-04-18T22:03:57.1817066Z        3668:           return;
2021-04-18T22:03:57.1817799Z #13   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3824, in do_catch [0x55d4c810f4ff]
2021-04-18T22:03:57.1818975Z        3821:   try {
2021-04-18T22:03:57.1819767Z        3822:     ScopedTracer _tracer("Catch", EventCategory::LPC_CATCH);
2021-04-18T22:03:57.1820908Z        3823:     /* note, this will work, since csp->extern_call won't be used */
2021-04-18T22:03:57.1821363Z       >3824:     eval_instruction(pc);
2021-04-18T22:03:57.1821698Z        3825:   } catch (const char *) {
2021-04-18T22:03:57.1821981Z        3826:     /*
2021-04-18T22:03:57.1822359Z        3827:      * They did a throw() or error. That means that the control stack
2021-04-18T22:03:57.1823060Z #12   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3779, in eval_instruction [0x55d4c80fecd2]
2021-04-18T22:03:57.1823895Z        3777:           ScopedTracer _efun_tracer(instrs[instruction].name, EventCategory::LPC_EFUN,
2021-04-18T22:03:57.1824483Z        3778:                                     std::move(trace_context));
2021-04-18T22:03:57.1825069Z       >3779:           (*efun_table[instruction - EFUN_BASE])();
2021-04-18T22:03:57.1825418Z        3780:         }
2021-04-18T22:03:57.1825646Z        3781: 
2021-04-18T22:03:57.1825913Z        3782: #ifdef DEBUG
2021-04-18T22:03:57.1826447Z #11   Source "/home/runner/work/fluffos/fluffos/src/packages/core/efuns_main.cc", line 226, in f__call_other [0x55d4c84ffa08]
2021-04-18T22:03:57.1826950Z         223:     }
2021-04-18T22:03:57.1827176Z         224:   }
2021-04-18T22:03:57.1827560Z         225:   /* Send the remaining arguments to the function. */
2021-04-18T22:03:57.1828324Z       > 226:   if (apply(funcname, ob, num_arg - 2, ORIGIN_CALL_OTHER) == nullptr) { /* Function not found */
2021-04-18T22:03:57.1828802Z         227:     pop_2_elems();
2021-04-18T22:03:57.1829110Z         228:     push_undefined();
2021-04-18T22:03:57.1829417Z         229:     return;
2021-04-18T22:03:57.1829909Z #10   Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 293, in apply [0x55d4c81d151d]
2021-04-18T22:03:57.1830411Z         290: #ifdef DEBUG
2021-04-18T22:03:57.1830877Z         291:   expected_sp = sp - num_arg;
2021-04-18T22:03:57.1831187Z         292: #endif
2021-04-18T22:03:57.1831506Z       > 293:   if (apply_low(fun, ob, num_arg) == 0) {
2021-04-18T22:03:57.1831862Z         294:     return nullptr;
2021-04-18T22:03:57.1832142Z         295:   }
2021-04-18T22:03:57.1832469Z         296:   free_svalue(&apply_ret_value, "sapply");
2021-04-18T22:03:57.1833177Z #9    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 267, in apply_low [0x55d4c81d122a]
2021-04-18T22:03:57.1833684Z         264: #ifdef DEBUG
2021-04-18T22:03:57.1833978Z         265:     save_csp = csp;
2021-04-18T22:03:57.1834248Z         266: #endif
2021-04-18T22:03:57.1834801Z       > 267:     call_program(current_prog, funp->address);
2021-04-18T22:03:57.1835483Z         268:     DEBUG_CHECK(save_csp - 1 != csp, "Bad csp after execution in apply_low.\n");
2021-04-18T22:03:57.1835920Z         269:     return 1;
2021-04-18T22:03:57.1836166Z         270:   }
2021-04-18T22:03:57.1836723Z #8    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3665, in eval_instruction [0x55d4c80fa312]
2021-04-18T22:03:57.1837319Z        3662:         LOAD_SHORT(offset, pc);
2021-04-18T22:03:57.1837903Z        3663:         offset = (pc - 2) + offset - current_prog->program;
2021-04-18T22:03:57.1838251Z        3664: 
2021-04-18T22:03:57.1838537Z       >3665:         do_catch(pc, offset);
2021-04-18T22:03:57.1839208Z        3666:         if ((csp[1].framekind & (FRAME_EXTERNAL | FRAME_RETURNED_FROM_CATCH)) ==
2021-04-18T22:03:57.1839742Z        3667:             (FRAME_EXTERNAL | FRAME_RETURNED_FROM_CATCH)) {
2021-04-18T22:03:57.1840125Z        3668:           return;
2021-04-18T22:03:57.1840658Z #7    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3824, in do_catch [0x55d4c810f4ff]
2021-04-18T22:03:57.1841179Z        3821:   try {
2021-04-18T22:03:57.1841649Z        3822:     ScopedTracer _tracer("Catch", EventCategory::LPC_CATCH);
2021-04-18T22:03:57.1842437Z        3823:     /* note, this will work, since csp->extern_call won't be used */
2021-04-18T22:03:57.1842874Z       >3824:     eval_instruction(pc);
2021-04-18T22:03:57.1843221Z        3825:   } catch (const char *) {
2021-04-18T22:03:57.1843490Z        3826:     /*
2021-04-18T22:03:57.1843874Z        3827:      * They did a throw() or error. That means that the control stack
2021-04-18T22:03:57.1844577Z #6    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3779, in eval_instruction [0x55d4c80fecd2]
2021-04-18T22:03:57.1845420Z        3777:           ScopedTracer _efun_tracer(instrs[instruction].name, EventCategory::LPC_EFUN,
2021-04-18T22:03:57.1845994Z        3778:                                     std::move(trace_context));
2021-04-18T22:03:57.1846586Z       >3779:           (*efun_table[instruction - EFUN_BASE])();
2021-04-18T22:03:57.1846922Z        3780:         }
2021-04-18T22:03:57.1847157Z        3781: 
2021-04-18T22:03:57.1847406Z        3782: #ifdef DEBUG
2021-04-18T22:03:57.1847948Z #5    Source "/home/runner/work/fluffos/fluffos/src/packages/core/efuns_main.cc", line 226, in f__call_other [0x55d4c84ffa08]
2021-04-18T22:03:57.1848445Z         223:     }
2021-04-18T22:03:57.1848796Z         224:   }
2021-04-18T22:03:57.1849159Z         225:   /* Send the remaining arguments to the function. */
2021-04-18T22:03:57.1849933Z       > 226:   if (apply(funcname, ob, num_arg - 2, ORIGIN_CALL_OTHER) == nullptr) { /* Function not found */
2021-04-18T22:03:57.1850408Z         227:     pop_2_elems();
2021-04-18T22:03:57.1850730Z         228:     push_undefined();
2021-04-18T22:03:57.1851032Z         229:     return;
2021-04-18T22:03:57.1851531Z #4    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 293, in apply [0x55d4c81d151d]
2021-04-18T22:03:57.1852026Z         290: #ifdef DEBUG
2021-04-18T22:03:57.1852490Z         291:   expected_sp = sp - num_arg;
2021-04-18T22:03:57.1852812Z         292: #endif
2021-04-18T22:03:57.1853122Z       > 293:   if (apply_low(fun, ob, num_arg) == 0) {
2021-04-18T22:03:57.1853465Z         294:     return nullptr;
2021-04-18T22:03:57.1853735Z         295:   }
2021-04-18T22:03:57.1854062Z         296:   free_svalue(&apply_ret_value, "sapply");
2021-04-18T22:03:57.1854634Z #3    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/apply.cc", line 267, in apply_low [0x55d4c81d122a]
2021-04-18T22:03:57.1855262Z         264: #ifdef DEBUG
2021-04-18T22:03:57.1855549Z         265:     save_csp = csp;
2021-04-18T22:03:57.1855827Z         266: #endif
2021-04-18T22:03:57.1856378Z       > 267:     call_program(current_prog, funp->address);
2021-04-18T22:03:57.1857066Z         268:     DEBUG_CHECK(save_csp - 1 != csp, "Bad csp after execution in apply_low.\n");
2021-04-18T22:03:57.1857487Z         269:     return 1;
2021-04-18T22:03:57.1857740Z         270:   }
2021-04-18T22:03:57.1858287Z #2    Source "/home/runner/work/fluffos/fluffos/src/vm/internal/base/interpret.cc", line 3779, in eval_instruction [0x55d4c80fecd2]
2021-04-18T22:03:57.1859129Z        3777:           ScopedTracer _efun_tracer(instrs[instruction].name, EventCategory::LPC_EFUN,
2021-04-18T22:03:57.1859702Z        3778:                                     std::move(trace_context));
2021-04-18T22:03:57.1860277Z       >3779:           (*efun_table[instruction - EFUN_BASE])();
2021-04-18T22:03:57.1860614Z        3780:         }
2021-04-18T22:03:57.1860857Z        3781: 
2021-04-18T22:03:57.1861193Z        3782: #ifdef DEBUG
2021-04-18T22:03:57.1861737Z #1    Source "/home/runner/work/fluffos/fluffos/src/packages/core/efuns_main.cc", line 1740, in f_read_file [0x55d4c8523071]
2021-04-18T22:03:57.1862258Z        1737:     default:
2021-04-18T22:03:57.1862604Z        1738:       error("Wrong number of arguments!");
2021-04-18T22:03:57.1862928Z        1739:   }
2021-04-18T22:03:57.1863441Z       >1740:   str = read_file(sp->u.string, start, len);
2021-04-18T22:03:57.1863807Z        1741:   pop_stack();
2021-04-18T22:03:57.1864075Z        1742:   if (!str) {
2021-04-18T22:03:57.1864379Z        1743:     push_svalue(&const0);
2021-04-18T22:03:57.1864915Z #0    Source "/home/runner/work/fluffos/fluffos/src/packages/core/file.cc", line 442, in read_file [0x55d4c855fd25]
2021-04-18T22:03:57.1865501Z         439:     ptr_end = (char *)ptr_start + read_file_max_size;
2021-04-18T22:03:57.1865826Z         440:   }
2021-04-18T22:03:57.1866058Z         441: 
2021-04-18T22:03:57.1866465Z       > 442:   *ptr_end = '\0';
2021-04-18T22:03:57.1866731Z         443: 
2021-04-18T22:03:57.1867283Z         444:   bool found_crlf = strchr(ptr_start, '\r') != nullptr;
2021-04-18T22:03:57.1867653Z         445:   if (found_crlf) {
2021-04-18T22:03:57.1868123Z Segmentation fault (Invalid permissions for mapped object [0x55d4c7e6c632])
2021-04-18T22:03:57.1869247Z /home/runner/work/_temp/5b7ed94d-c17b-403a-93e8-e474a9ca6a2a.sh: line 1:  5814 Segmentation fault      (core dumped) ../build/bin/driver etc/config.test -ftest

@jalbright015
Copy link
Copy Markdown
Contributor Author

Thanks for the crash log. It looks like checks are passing now.

@thefallentree thefallentree merged commit cbf390f into fluffos:master Apr 19, 2021
Amirani-al pushed a commit to Amirani-al/fluffos that referenced this pull request May 30, 2023
…#797)

* Added support for read_file() to read lines from end of file

* changed out ptr_start was assigned in read_file()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants